在iOS简单通讯录的教程中,从登录界面到通讯录联系人界面的数据顺传部分的方法如下,根据不同的登录用户名,变换通讯录联系人界面的标题:
1 2 3 4 |
|
我的理解是:这个方法新建了一个UIViewController
类的对象vc
,并把segue
的destinationViewController
赋值给了vc
,然后改变了vc
对象的title
,最后也没有返回vc
,和destinationViewController
的title
应该没有关系啊?为什么这样写是正确的呢?
怪我咯2017-04-18 09:15:14
This method creates a new object vc of the UIViewController class
There is no operation to create a new VC here, there is no memory allocation.
1 2 |
|
This code is exactly the same as the one below
1 |
|
Of course there is no need to return.
PHP中文网2017-04-18 09:15:14
If you don’t use segue to generate a vc from sb, it’s probably like this
1 2 3 4 5 6 |
|
segue calls destinationViewController, it actually does the same thing. It generates vc from sb and returns it to you
This vc is the same as what you generated yourself
{
1 2 |
|
}
1 |
|
PHP中文网2017-04-18 09:15:14
vc is not newly created, it is just a pointer to the target viewcontroller, which is the viewcontroller to be jumped
ringa_lee2017-04-18 09:15:14
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender This method is called when switching interfaces. At this time, the segue already contains the interface you want to go to, and is not created at this time. The storyboard will create the viewController object you want and give it to the segue.
UIViewController *vc = segue.destinationViewController;
vc.title = [NSString stringWithFormat:@"%@'s contact list",_accountField.text];
These two lines of code mean that you take out the target VC of the segue and modify it title.
The key point is to understand that the target VC you are going to is built by storybard, not something you create and then return.