在Storyboard中访问ViewController的属性

时间:2015-11-10 05:18:03

标签: ios objective-c

     NSString * storyboardName = @"Main";
     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
     UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"drawingboard"];

我想访问vc.property,但我想要访问的属性不可访问,即使该属性位于.h文件中并且具有非原子的assign属性。无论如何我可以访问吗?请帮忙!提前致谢。

3 个答案:

答案 0 :(得分:1)

必须将vc转换为特定类,该类是UIViewController的子类。

例如:

Drawingboard *vc = [storyboard instantiateViewControllerWithIdentifier:@"drawingboard"];

答案 1 :(得分:0)

实施prepareForSegue:方法如下: -

让我们说 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";
    }
}

答案 2 :(得分:-1)

如果您在.h文件中定义属性,则不需要在.m中重新定义它。否则,您将访问.m对象而不是.h属性。

    @property (nonatomic) UIViewController *vc;

上面是你的标题,下面是实现

 NSString * storyboardName = @"Main";
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
 _vc = [storyboard instantiateViewControllerWithIdentifier:@"drawingboard"];

_vc也可以替换为self.vc,具体取决于首选项,但这将是您以后访问它的方式。希望这有助于