2012-06-27 39 views
0

之外我有這個appdelegate.h的iOS,無法委託viewDidLoad中

​​

與.m文件

@synthesize name; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    [email protected]"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); 
    } 

任何人都可以幫我嗎?這個問題讓我發瘋......

+1

從哪裏調用'initWithNibName'即初始化您的視圖控制器?你有沒有調試過控制器嗎? – rishi

+0

「沒有工作」=什麼? – borrrden

+0

「沒有工作」=名稱沒有打印出來 – user486174

回答

1

如果你重寫-initWithNibName:,你需要返回一個類的實例(或self); 請嘗試下面的代碼-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; 
} 

我認爲這可能對您有用。