簡體   English   中英

情節提要中ViewController的訪問屬性

[英]Access property of ViewController in Storyboard

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

我想訪問vc.property,但是我要訪問的屬性無法訪問,即使該屬性位於.h文件中並且具有非原子的assign屬性。 無論如何,我可以訪問嗎? 請幫忙! 提前致謝。

vc必須強制轉換為特定類,該特定類是UIViewController的子類。

例如:

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

實現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";
    }
}

如果在.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替換,但這將是以后使用它的方式。 希望這有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM