IOS prepareForSegue 传递数据成功但是viewdidload 清除了?
IOS prepareForSegue pass a data successfully but viewdidload clear it?
在UIViewController A中,我在A的prepaerForSegue函数中执行Segue到UIViewControllerB.And,我使用set函数设置了B的强变量值。但是,当B的viewDidLoad()被调用时,值为nil .
谁能帮帮我,告诉我为什么?
我的代码是:
UIViewController A:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
CXPWDViewController *cxpwdViewController=[[CXPWDViewController alloc] init];
//I can't use the segue.desinationViewController, cause it is a present modally , the desinationViewController is navigationViewController.
[cxpwdViewController setAction:@"EnterPasscode"];//here the cxpwdViewController .action is @"EnterPasscode"
}
UIViewController B:
-(void) viewDidLoad()
{
[super viewDidLoad];// here the cxpwdViewController .action is nil; Why?
}
CXPWDViewController *cxpwdViewController=[[CXPWDViewController alloc] init];
//I can't use the segue.desinationViewController
我不知道为什么你认为你不能使用 segue 目标视图控制器。但无论如何,你肯定不能用这种方式做你想做的事。您正在创建一个完全 不同的 CXPWDViewController - 然后稍后将其销毁。那样你走不了多远!
如果您要展示 UINavigationViewController
,那么 rootViewController
控制器应该是您的 CXPWDViewController
。这样您就可以进行相同的类型转换。由于您正在创建另一个 viewcontroller 实例,因此您没有获得 value.Try 此代码。
UIViewController A:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue destinationViewController] isKindOfClass:[UINavigationViewController class]])
{
UINavigationViewController* destinationNavController =(NavigationViewController*)[segue destinationViewController];
CXPWDViewController *cxpwdViewController=(CXPWDViewController*)[destinationNavController.viewControllers firstObject];
if([cxpwdViewController isKindOfClass:[CXPWDViewController class]])
{
[cxpwdViewController setAction:@"EnterPasscode"];
}
}
}
在UIViewController A中,我在A的prepaerForSegue函数中执行Segue到UIViewControllerB.And,我使用set函数设置了B的强变量值。但是,当B的viewDidLoad()被调用时,值为nil . 谁能帮帮我,告诉我为什么?
我的代码是:
UIViewController A:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
CXPWDViewController *cxpwdViewController=[[CXPWDViewController alloc] init];
//I can't use the segue.desinationViewController, cause it is a present modally , the desinationViewController is navigationViewController.
[cxpwdViewController setAction:@"EnterPasscode"];//here the cxpwdViewController .action is @"EnterPasscode"
}
UIViewController B:
-(void) viewDidLoad()
{
[super viewDidLoad];// here the cxpwdViewController .action is nil; Why?
}
CXPWDViewController *cxpwdViewController=[[CXPWDViewController alloc] init];
//I can't use the segue.desinationViewController
我不知道为什么你认为你不能使用 segue 目标视图控制器。但无论如何,你肯定不能用这种方式做你想做的事。您正在创建一个完全 不同的 CXPWDViewController - 然后稍后将其销毁。那样你走不了多远!
如果您要展示 UINavigationViewController
,那么 rootViewController
控制器应该是您的 CXPWDViewController
。这样您就可以进行相同的类型转换。由于您正在创建另一个 viewcontroller 实例,因此您没有获得 value.Try 此代码。
UIViewController A:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue destinationViewController] isKindOfClass:[UINavigationViewController class]])
{
UINavigationViewController* destinationNavController =(NavigationViewController*)[segue destinationViewController];
CXPWDViewController *cxpwdViewController=(CXPWDViewController*)[destinationNavController.viewControllers firstObject];
if([cxpwdViewController isKindOfClass:[CXPWDViewController class]])
{
[cxpwdViewController setAction:@"EnterPasscode"];
}
}
}