可能重复:
viewDidLoad being called before didSelectRowAtIndexPath
我正在尝试将变量传递给新视图。我有以下代码:
appDelegate *dataCenter = (AppDelegate*)[[UIApplication sharedApplication] delegate];
dataCenter.myVariable = [array objectAtIndex:indexPath.row];
中的
didSelectRowAtIndexPath
调用视图的。但是,我遇到的问题是这个变量在下一个视图的vewDidLoad函数中是空的,只是因为它在调用视图的didSelectRowAtIndexPath之前触发了。我正在使用故事板将视图链接在一起。两者都是UITableView。
如果我回击然后重新选择表格元素,那么它会被设置,当我回击然后再次选择时,该变量被设置。执行顺序有什么办法吗?我真的不想在后端进行UI视图切换。
非常感谢任何帮助!
答案 0 :(得分:1)
如何在didSelectRowAtIndexPath将数据保存到NSUserDefaults 然后在viewDidLoad中读取从NSUserDefaults保存的数据。
-(void) didSelectRowAtIndexPath {
NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
[infoSaved setObject:myVariable forKey:@"myvariable"];
[infoSaved:synchronize];
// then call the new view to load
}
在加载的新视图中:
-(void) viewDidLoad {
NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
myVariable = [infoSaved objectForKey:@"myvariable"];
// and use your variable
}
希望有所帮助