<分区>
标签 objective-c ios xcode
<分区>
Possible Duplicate:
viewDidLoad being called before didSelectRowAtIndexPath
我正在尝试将变量传递到新 View 。我有以下代码:
appDelegate *dataCenter = (AppDelegate*)[[UIApplication sharedApplication] delegate];
dataCenter.myVariable = [array objectAtIndex:indexPath.row];
在
didSelectRowAtIndexPath
调用 View 。但是,我遇到的问题是该变量在下一个 View 的 vewDidLoad 函数中为空,只是因为它在调用 View 的 didSelectRowAtIndexPath 之前触发。我正在使用 Storyboard将 View 链接在一起。两者都是 UITableView。
如果我回击然后重新选择表格元素,那么它就会被设置,当然,当我回击然后再次选择时,变量就会被设置。有什么办法可以确定执行顺序吗?我真的不想在后端进行 UI View 切换。
非常感谢任何帮助!
最佳答案
如何将数据保存到 didSelectRowAtIndexPath 的 NSUserDefaults 中 然后在 viewDidLoad 处从 NSUserDefaults 读取保存的数据。
-(void) didSelectRowAtIndexPath {
NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
[infoSaved setObject:myVariable forKey:@"myvariable"];
[infoSaved:synchronize];
// then call the new view to load
}
在加载的新 View 中:
-(void) viewDidLoad {
NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
myVariable = [infoSaved objectForKey:@"myvariable"];
// and use your variable
}
希望对你有帮助
关于objective-c - didSelectRowAtIndexPath 在被调用 View 的 viewDidLoad 之后被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13336379/