0
  • 1.与委托人ViewControllerA发送密钥ViewControllerB

  • 2.ViewControllerB加载时key值正确,但需要使用keyviewDidLoad

所以问题是:如何先运行委托方法,以便我可以使用密钥?或者:如果不能让委托之前运行,viewDidLoad有没有办法使用这个密钥?viewDidLoadviewDidLoad

4

3 回答 3

0

ViewControllerB编写一个初始化方法,initWithNibName:bundle:key:delegate:如下所示

- (id) initWithNibName:(NSString *)nibNameOrNil
            bundle:(NSBundle *)nibBundleOrNil
               key:(NSString*)aKey
          delegate:(id) aDelegate
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self) {
        // Save aDelegate and aKey as properties
    }
    return self;
}

初始化后 viewDidLoad 将被调用,您可以在其中使用这些属性值。

干杯!
阿马尔。

于 2013-05-16T06:14:50.500 回答
0

为什么你不能做这样的事情:

假设您的密钥是一个字符串,

1)在你的ViewControllerB.h添加

@property (nonatomic, copy) NSString *keyFromParent;

- (id)initWithNibName:(NSString *)nibNameOrNil WithKey:(NSString *)key bundle:(NSBundle *)nibBundleOrNil;

2)在您的ViewControllerB.m中将您的initWithNibName方法替换为

- (id)initWithNibName:(NSString *)nibNameOrNil WithKey:(NSString *)key bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.keyFromParent = key;
    }
    return self;
}

3)当您从ViewControllerA呈现ViewControllerB时,请这样做,

ViewControllerB *controller = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" WithKey:KEY_FROM_DELEGATE bundle:nil];
[self presentViewController:controller animated:YES completion:nil];

4) 现在您可以使用keyFromParent ,它是ViewControllerBviewDidLoad方法中从ViewControllerA传递的值

希望这个答案对你有帮助!!

于 2013-05-16T06:20:54.103 回答
0

如果您只想从 ViewControllerA 向 ViewControllerB 发送变量值,则在 ViewControllerB 中创建具有读写权限的该变量的属性,并在导航到 ViewControllerB 之前将其分配到 ViewControllerA 中。

如果是字符串变量,则在 viewControllerB.h 中传递写入

@property (nonatomic, retain) NSString *variableName;

当您导航到 viewControllerB 时,在 ViewControllerA 中

viewControllerB *obj = [[viewControllerB alloc] initWithNibName:@"viewControllerB" bundle:nil];

obj.variableName = @"Your Value";
[self.navigationController pushViewController:obj animated:YES];
[obj release];
于 2013-05-16T06:32:43.120 回答