[英]iOS - viewDidLoad is being called BEFORE the didFinishLaunchingWithOptions delegate?
[英]iOS, Unable to delegate outside of viewDidLoad
我有這個appdelegate.h
#import <UIKit/UIKit.h>
@interface appDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
NSString *name;
}
@property (nonatomic, retain) NSString *name;
@end
和.m文件
@synthesize name;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
name=@"john";
return YES;
}
現在...我想從另一個控制器獲取這個名稱,如果我嘗試在我的viewDidLoad方法中調用它,它就可以工作。
- (void)viewDidLoad
{
appDelegate *test= (appDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", test.name);
}
但是如果我嘗試在initWithNibName中做同樣的事情,那將是行不通的...
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
appDelegate *test= (appDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", test.name);
}
有人可以幫我嗎? 這個問題使我發瘋...
如果要覆蓋-initWithNibName:
需要返回該類的實例(或self
); 在-initWithNibName:
嘗試以下代碼-initWithNibName:
它為我工作。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
appDelegate *test= (appDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", test.name);
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
}
return self;
}
我認為這可能對您有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.