0

假设我们有 2 个名为ViewControllerAViewControllerB的视图控制器。

当我不使用情节提要时,我有以下初始化机制。

//ViewControllerA.h

@interface ViewControllerA : UIViewController

@end

//ViewControllerA.m

@implementation ViewControllerA

- (IBAction)showViewControllerB:(id)sender {
    ViewControllerB *vcB = [[ViewControllerB alloc] initWithTitle:@"Test"];
    [self.navigationController pushViewController:vcB animated:YES];
 }

@end

//ViewControllerB.h

@interface ViewControllerB : UIViewController

- (id)initWithTitle:(NSString *)title;

 @end

//ViewControllerB.m

@interface ViewControllerB()

@property (nonatomic, retain) NSString *title;//Private scope.

@end

@implementation ViewControllerB

- (id)initWithTitle:(NSString *)title {
    self = [self init];
    if(self)
    {
        _title = title;
    }
    return self; 
}

@end

如何使用情节提要实现此目的而不在公共范围(ViewControllerB.h)中声明标题属性?

提前致谢。

4

3 回答 3

2

您可以使用KVC访问该属性。

- (IBAction)showViewControllerB:(id)sender 
{
    ViewControllerB *viewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"]; // corresponding to the storyboard identifier
    [viewcontroller setValue:@"Hello World" forKey:@"title"];
    [self.navigationController pushViewController:viewcontroller animated:YES];
}
于 2013-11-11T13:32:07.197 回答
0
UIStoryboard *storyboard=self.storyboard;

ViewControllerB *vc=(ViewControllerB*)[storyboard instantiateViewControllerWithIdentifier:@"Scene_Id"];

[vc setTitle:@"your Text"];

[self.navigationController pushViewController:vc animated:YES];
于 2013-11-11T12:42:08.940 回答
0

如果您确实需要将属性保密,您可以vcB使用 . 将数据附加到对象objc_setAssociatedObject。或者您可以将属性保存在 AppDelegate 中,并在 ViewControllerB 初始化时从那里获取它。

于 2013-11-11T13:35:19.157 回答