我正在尝试像这样将一些数据传递给我的 View Controller 类:
MyViewController *vc = [[MyViewController alloc] init];
vc.myProperty = dataToBePassed;
[self.navigationController pushViewController:vc animated:YES];
我需要在 viewDidLoad 中进行一些 View 配置,但似乎 viewDidload 调用早于属性分配。
然后在 MyViewController 实现中:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@", self.myProperty); // Here i get myProperty = nil
}
- (void)viewWillAppear
{
[super viewWillAppear];
NSLog(@"%@", self.myProperty); // Here i get myProperty = dataToBePassed but it's to late
}
如何在不实现单例或委托(delegate)模式的情况下在 viewDidLoad 方法中获取传递的数据?
最佳答案
尝试这样做
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
@"MainStoryboard" bundle:[NSBundle mainBundle]];
MyViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"storyboardIdentifier"];
vc.myProperty = dataToBePassed;
[self.navigationController pushViewController:vc animated:YES];
您必须首先在 View Controller 的 Storyboard中设置 Storyboard标识符。
关于ios - 无法在 viewDidLoad 方法中获取传递的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38356397/