[英]How can delegate method run before ViewDidLoad
1. ViewControllerA
sent ViewControllerB
a key with delegate 1.
ViewControllerA
发送ViewControllerB
与代表一个键
2.When ViewControllerB
been loaded the key value is correct, but I need to use the key for viewDidLoad
2.当
ViewControllerB
被加载时,键值是正确的,但是我需要将键用于viewDidLoad
So the problem is:How can delegate method run first so i can use the key in viewDidLoad
?OR:is there a way to use this key while viewDidLoad
if can't make delegate run before viewDidLoad
? 所以问题是:如何首先运行委托方法,以便我可以在
viewDidLoad
使用该键?或者:如果无法使委托在viewDidLoad
之前运行,是否可以在viewDidLoad
使用此键?
Write an initialiser method in ViewControllerB
something like initWithNibName:bundle:key:delegate:
Something like shown below 在
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;
}
After init the viewDidLoad will get called where you can use these property values. 初始化后,将调用viewDidLoad,您可以在其中使用这些属性值。
Cheers! 干杯!
Amar. 阿玛
Why can't you do something like this: 你为什么不能做这样的事情:
lets say that your key is a String, 假设您的密钥是一个字符串,
1) in your ViewControllerB.h add 1)在您的ViewControllerB.h中添加
@property (nonatomic, copy) NSString *keyFromParent;
- (id)initWithNibName:(NSString *)nibNameOrNil WithKey:(NSString *)key bundle:(NSBundle *)nibBundleOrNil;
2) in your ViewControllerB.m replace your initWithNibName method with 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) When you present the ViewControllerB from the ViewControllerA do it like this, 3)当您从ViewControllerA呈现ViewControllerB时 ,请按照以下步骤操作:
ViewControllerB *controller = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" WithKey:KEY_FROM_DELEGATE bundle:nil];
[self presentViewController:controller animated:YES completion:nil];
4) Now you can use the keyFromParent which is the value passed from the ViewControllerA in the viewDidLoad method of the ViewControllerB 4)现在可以使用keyFromParent作为从ViewControllerA在ViewControllerB的viewDidLoad方法传递的值
Hope this answer would help you!! 希望这个答案对您有帮助!
If you just want to send a variable value to ViewControllerB from ViewControllerA, then create property of that Variable in ViewControllerB with readWrite authority and assign it in ViewControllerA just before navigating to ViewControllerB. 如果您只想从ViewControllerA向ViewControllerB发送变量值,则在具有readWrite权限的ViewControllerB中创建该变量的属性,并在导航至ViewControllerB之前在ViewControllerA中进行分配。
if it is string variable to pass write in viewControllerB.h 如果它是要传递给viewControllerB.h中写操作的字符串变量
@property (nonatomic, retain) NSString *variableName;
and in ViewControllerA when you are navigating to viewControllerB 并导航到viewControllerB时在ViewControllerA中
viewControllerB *obj = [[viewControllerB alloc] initWithNibName:@"viewControllerB" bundle:nil];
obj.variableName = @"Your Value";
[self.navigationController pushViewController:obj animated:YES];
[obj release];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.