我当前正在使用以下代码打开我的新视图:
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
[self presentViewController:vc animated:YES completion:nil];
我希望能够使用类似于下面发布的代码将变量的值传递给SecondViewController:
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.isSomethingEnabled = YES;
[self pushViewController:viewControllerB animated:YES];
但我对Objective-C&iOS编码,不知道如何。那么,我如何使用我已经拥有的代码(第一块代码)将SecondViewController中的变量值更改为FirstViewController中变量的值呢。感谢所有的帮助。
假设您的第二个视图控制器是MyCustomViewController类型,请更改
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
至
MyCustomViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
无关注释,我将更改
MyCustomViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
至
MyCustomViewController * vc = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([MyCustomViewController class])];
您需要更改情节提要中的标识符才能匹配。如果你这样做,你将保留一些类型信息,而不是使用脆弱的硬编码字符串。(仅供参考,这些代码都没有经过测试,只是在这里写出来。)