Ios MVC中的数据连接

Ios MVC中的数据连接,ios,model-view-controller,Ios,Model View Controller,我有一个导航控制器和两个视图控制器。第一个视图控制器与名为ViewController的UIViewController相关联。第二个连接到名为BookVC的UIViewController。BookVC有一个UITextField,通过一个插座连接: @property (strong, nonatomic) IBOutlet UITextField *textFieldContent; 通过使用segue的按钮进行连接。我想在两者之间传递一些数据,我使用以下失败的代码: -(void) p

我有一个导航控制器和两个视图控制器。第一个视图控制器与名为ViewController的UIViewController相关联。第二个连接到名为BookVC的UIViewController。BookVC有一个UITextField,通过一个插座连接:

@property (strong, nonatomic) IBOutlet UITextField *textFieldContent;
通过使用segue的按钮进行连接。我想在两者之间传递一些数据,我使用以下失败的代码:

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

如何在视图控制器之间传递数据?

您不需要此行:

BookVC* nextPage = [[BookVC alloc] init];
这将创建一个全新的BookVC,然后在下一行中进行覆盖


因为您使用的是[segue destinationViewController],所以应该可以很好地使用它。当你转到下一页时会发生什么?

我认为问题是
textFieldContent
在那一点上不存在。您需要在
BookVC
中添加一个属性,该属性可以保存要放入
textFieldContent
中的文本。。。我们将其命名为
@property NSString*textfieldcontext
。那么

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    BookVC *nextPage = [segue destinationViewController];
    [nextPage setTextFieldContentText:@"Some content"];
    ...
}
然后,在
BookVC
-viewDidLoad
方法中:

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

我想我快到了。我认为发送文件应该有:BookVC*nextPage=[segue destinationViewController];UITextField*tf;tf.text=@“某些内容”;[nextPage setTextFieldContent:tf];但是,我不理解viewDidLoad语句。[nextPage setTextFieldContent:tf];第一个控制器不知道第二个控制器的文本字段。它只是设置最终将放入
BookVC
textFieldContent
中的字符串
BookVC
-viewDidLoad
方法在加载时自动调用。此时,您知道textFieldContent标签存在,并且可以设置它的文本,因此您可以在那里进行设置。查看
UIViewController
上的文档和
-viewDidLoad
方法。忽略最后的评论。你的解释很好。非常感谢。