Ios 从UITextField获取用户输入并在ViewController中保存为字符串,而不使用segues
我在互联网上到处寻找这个问题的答案,我尝试了多种不同的建议方法,但仍然不起作用 我有一个带有Ios 从UITextField获取用户输入并在ViewController中保存为字符串,而不使用segues,ios,xcode,cocoa-touch,uitextfield,Ios,Xcode,Cocoa Touch,Uitextfield,我在互联网上到处寻找这个问题的答案,我尝试了多种不同的建议方法,但仍然不起作用 我有一个带有UITextField和两个ui按钮的UIViewController。一个用于将输入保存为字符串,另一个用于切换到另一个视图。在另一个视图中,我只有一个ui标签。我所要做的就是将用户输入的文本作为字符串保存在第二个UIViewController中,然后能够显示它。诀窍是我不想使用segues 这是我的代码: ViewController.m #import "ViewController.h" #im
UITextField
和两个ui按钮的UIViewController
。一个用于将输入保存为字符串,另一个用于切换到另一个视图。在另一个视图中,我只有一个ui标签
。我所要做的就是将用户输入的文本作为字符串保存在第二个UIViewController
中,然后能够显示它。诀窍是我不想使用segues
这是我的代码:
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)save:(id)sender {
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
detailViewController.str = _TextField.text;
[self.TextField resignFirstResponder];
}
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[_Label setText:_str];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondViewController.h
@interface SecondViewController : UIViewController
@property (nonatomic, retain) NSString *str;
@property (weak, nonatomic) IBOutlet UILabel *Label;
@end
SecondViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)save:(id)sender {
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
detailViewController.str = _TextField.text;
[self.TextField resignFirstResponder];
}
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[_Label setText:_str];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
如果有人能告诉我代码出了什么问题,那将非常有帮助。
当我运行应用程序并键入文本后,转到第二视图,标签将显示为空。在该功能中,您也可以使用pushView控制器
- (IBAction)save:(id)sender {
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
detailViewController.str = _TextField.text;
[self.navigationController pushViewController:detailViewController animated:NO];
[self.TextField resignFirstResponder];
}
试试这个可能会有帮助。问题在于你的segue
不需要在其中一个操作中分配,该操作不对segue负责
只要重写这个方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
detailViewController.str = _TextField.text;
}
}
诀窍是我不想使用segues
那就用--
我删除了这个代码
[self.navigationController pushViewController:detailViewController动画:否];
此代码提示您按下视图控制器
希望这有帮助:)我在代码中添加了pushViewController,但没有任何区别。当我按下保存按钮时,键盘被关闭,然后当我按下按钮转到下一个视图时,它仍然显示为空。因此,我在ViewController中添加了以下内容:NSLog(@“%@”,detailViewController.str);在SecondViewController中:NSLog(@“%@”,self.str);在ViewController中,它显示为“我的字符串”,但在SecondViewController中,它显示为空,请尝试清理项目并再次生成。。因为代码是正确的。我做了一个干净的构建,但它不起作用。如果我的视图嵌入到导航控制器中,这有关系吗?我如何设置segue有关系吗?segue only有关系吗,self.Label.text=self.str
SecondViewController仍然出现了一个空白标签,这可能是因为您的按钮出现了问题。你将在一个动作中分配任务,然后继续另一个动作,对吗。。是的,我有一个按钮链接到iAction,那就是save按钮,然后我链接了另一个按钮,接下来,切换到Second viewController这就是原因,实际上当你要切换viewController时,它会被重新初始化,所以str可能是空的如果我想将TextField.text保存到ViewController,而不是我正在设置segue的那个,这还能工作吗?在最初的问题中,我说过我想尝试避免通过Segue发送,然后使用NSUserDefaults
有没有可能让我看看它是什么样子的?非常感谢!我刚开始工作!我已经为此工作了将近一个星期了!