[英]Access property of ViewController in Storyboard
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"drawingboard"];
I want to access vc.property but the property I want to access in not accessible even though the property is in .h file and has a nonatomic, assign property. 我想访问vc.property,但是我要访问的属性无法访问,即使该属性位于.h文件中并且具有非原子的assign属性。 Is there anyway I can access that?
无论如何,我可以访问吗? Please help!
请帮忙! Thanks in advance.
提前致谢。
The vc
must be casted to a specific class which is a subclass of UIViewController
. vc
必须强制转换为特定类,该特定类是UIViewController
的子类。
For instance: 例如:
Drawingboard *vc = [storyboard instantiateViewControllerWithIdentifier:@"drawingboard"];
Implement prepareForSegue:
method as below:- 实现
prepareForSegue:
方法如下:-
Lets say CommentsViewController is the class name of your viewController whom you want to push. 可以说CommentsViewController是您要推送的viewController的类名。
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.destinationViewController isKindOfClass:[CommentsViewController class]]) { // Your viewControllerclass
CommentsViewController *controller = [segue destinationViewController];
controller.propertyName = @"Write value here";
}
}
If you define the property in the .h file than you don't need to redefine it in the .m. 如果在.h文件中定义属性,则无需在.m中重新定义属性。 Otherwise you'll be accessing the .m object and not the .h property.
否则,您将访问.m对象而不是.h属性。
@property (nonatomic) UIViewController *vc;
Above would be your header, below is the implementation 上面是您的标头,下面是实现
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
_vc = [storyboard instantiateViewControllerWithIdentifier:@"drawingboard"];
_vc can also be replaced with self.vc depending on preference but that would be how you access it later on. _vc也可以根据自己的喜好用self.vc替换,但这将是以后使用它的方式。 Hope this Helped
希望这有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.