简体   繁体   English

情节提要中ViewController的访问属性

[英]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.

相关问题 从另一个Storyboard设置ViewController的属性 - Set Property of ViewController from Another Storyboard 访问viewController属性 - Access viewController property 如何从Storyboard initialVC(ContainerView)访问ViewController - How to access ViewController from Storyboard initialVC(ContainerView) 为什么无法将情节提要中的imageVew组件链接到ViewController中的imageView属性 - Why I cannot link the imageVew component in storyboard to the imageView property in ViewController iOS / Objective-c:使用按钮访问ViewController,“反对” storyBoard结构 - IOS/Objective-c: Access a ViewController with a button, “opposing” the storyBoard structure xcode storyboard容器视图 - 如何访问viewcontroller - xcode storyboard Container View - How do I access the viewcontroller 在没有 StoryBoard 的情况下点击通知时访问特定的 ViewController(以编程方式) - Access specific ViewController when notification is Tapped WITHOUT StoryBoard (programatically) 无法从私有 cocoapods 框架访问 storyboard 和 viewcontroller 文件 - Not able to access storyboard and viewcontroller files from private cocoapods framework iOS:从另一个类访问Storyboard TabBar ViewController - iOS: Access Storyboard TabBar ViewController from another Class 使用故事板创建的 ViewController?.instantiateViewController(identifier: "") 无法访问发送 ViewController 的属性/功能 - ViewController created with storyboard?.instantiateViewController(identifier: "") not able to access sending ViewController's properties/functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM