好吧,我会尽量让这件事变得简单(当我试图问这个问题时,我似乎在 chat.stackoveflow.com 上到处乱跑)。我想将文本从一个视图控制器中的文本字段传递到另一个视图控制器。
我应该使用Model类并将 textfield.text 存储在 Model.[h/m] 文件中,然后让第二个视图控制器访问存储在模型中的数据吗?
基本上这就是我所拥有的,
ViewControllerWelcome.h
@interface ViewControllerWelcome : UIViewController { }
@property (weak, nonatomic) IBOutlet UITextField *textFieldUsername;
ViewControllerWelcome.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewControllerHome *home = (ViewControllerHome *)[storyboard instantiateViewControllerWithIdentifier:@"Home"];
NSString *username = _textFieldUsername.text;
home.lblUSERNAME.text=username;
[self presentModalViewController:home animated:YES];
ViewControllerHome.h
@interface ViewControllerHome : UIViewController {
NSString *username;
UILabel *lblUSERNAME;
}
@property (weak, nonatomic) IBOutlet UILabel *lblUSERNAME;
ViewControllerHome.m
- (void)changeUSERNAME {
// get username from welcome tf
ViewControllerWelcome *welcome = [[ViewControllerWelcome alloc] init];
welcome.username = [self lblUSERNAME.text];
// _lblUSERNAME.text = welcome._textFieldUsername.text;
//welcome.textFieldUsername.text = _username;
// username = welcome.textFieldUsername.text;
NSLog(@"username = %@",username);
// welcome.textFieldUsername.text = _lblUSERNAME.text;
// NSLog(@"username = %@",welcome.textFieldUsername.text);
}
如您所见,我尝试了几种不同的方法,但无法提出可行的解决方案:-l