2012-04-25 36 views
0

我有一个导航控制器和2个视图控制器。第一个View Controller与一个名为ViewController的UIViewController相关联。第二个连接到名为BookVC的UIViewController。 BookVC具有的UITextField并经由出口连接:在MVC中连接数据

@property (strong, nonatomic) IBOutlet UITextField *textFieldContent; 

的连接是与使用赛格瑞按钮制成。我想在二者之间传递一些数据,现在用的是下面的代码失败:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
BookVC* nextPage = [[BookVC alloc] init]; 
nextPage = [segue destinationViewController]; 
[email protected]"Some content"; 
} 

我应该如何通过视图控制器之间的数据?

回答

0

我认为问题在于textFieldContent在那时不存在。您需要将一个属性添加到BookVC,以便将要放入的文本放入textFieldContent ...我们将其称为@property NSString *textFieldContentText。那么,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    BookVC *nextPage = [segue destinationViewController]; 
    [nextPage setTextFieldContentText:@"Some content"]; 
    ... 
} 

,然后在-viewDidLoad方法BookVC

- (void)viewDidLoad 
{ 
    [textFieldContent setText:[self textFieldContentText]]; 
} 
+0

我觉得我几乎没有。我认为发送文件应该有: BookVC * nextPage = [segue destinationViewController]; UITextField * tf; tf.text = @“一些内容”; [nextPage setTextFieldContent:tf]; 但是,我不明白viewDidLoad语句。 \t [nextPage setTextFieldContent:tf]; – SimonRH 2012-04-26 02:48:39

+0

第一个控制器不知道第二个控制器的文本字段。它只是设置将最终放置到'BookVC'的'textFieldContent'中的字符串。 'BookVC'的'-viewDidLoad'方法在加载时会自动调用。此时,您知道textFieldContent标签存在,并且其文本可以设置,因此您可以在此处执行此操作。查看'UIViewController'和'-viewDidLoad'方法的文档。 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html – macserv 2012-04-26 03:05:56

+0

忽略最后的评论。你的解释是完美的。谢谢! – SimonRH 2012-04-26 03:16:22

0

你不需要这行:

BookVC* nextPage = [[BookVC alloc] init]; 

,创建一个全新的BookVC你然后去和下一行覆盖。

由于您正在使用[segue destinationViewController],您应该很好。当你延续到下一页时会发生什么?