好吧,我會盡可能簡單地做到這一點(當我試着問這個問題時,我似乎在chat.stackoveflow.com上運行)。我想將文本從一個viewcontroller中的文本傳遞給另一個viewcontroller。在視圖控制器之間傳遞數據 - iOS Xcode - 什麼是好方法?
我應該使用模型類,並將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
如果您使用故事板,爲什麼不使用segues? – tomidelucca 2012-07-09 09:41:58
@ tomi2711你能否詳細說明一下? – Chris 2012-07-09 09:47:10