2011-08-31 27 views
0

下面的代碼更改標籤名稱,然後選擇索引2問題調用實例方法

[(UITabBarItem*)[self.rootController.tabBar.items objectAtIndex:0] setTitle:@"User"]; 

self.rootController.selectedIndex = 2; 

但是,它的工作原理例如,當應用程序didFinishLaunching方法,但當叫喜歡遵循不工作。

觸摸起來內部按鈕觸發renameTabs:

- (IBAction) renameTabs: (id)sender 
{ 
    CompanyAppDelegate *theInstance = [[CompanyAppDelegate alloc] init]; 
    [theInstance rename]; 

} 

和在控制器:

- (void) rename 
    { 

     [(UITabBarItem*)[self.rootController.tabBar.items objectAtIndex:0] setTitle:@"User"]; 

     self.rootController.selectedIndex = 2; 

    } 

重命名功能被觸發,並且還在.H定義。沒有錯誤,但沒有改變!有什麼不對的嗎??謝謝

回答

3

你不應該創建新的CompanyAppDelegate。嘗試實施方法sharedAppDelegateCompanyAppDelegate.m

+ (CompanyAppDelegate *)sharedAppDelegate 
{ 
    return (CompanyAppDelegate *) [UIApplication sharedApplication].delegate; 
} 

不要忘了聲明它CompanyAppDelegate.h

而更換renameTabs這一個:

- (IBAction) renameTabs: (id)sender 
{ 
    CompanyAppDelegate *theInstance = [CompanyAppDelegate sharedAppDelegate]; 
    [theInstance rename]; 
} 
+0

嗨,仍然會崩潰。現在沒有達到重命名,嘗試調用重命名時收到Sigabrt信號。 – Ruth85

+0

你的'CompanyAppDelegate'看起來像這樣:'@interface CompanyAppDelegate:NSObject '?什麼返回'[CompanyAppDelegate sharedAppDelegate];'? – Nekto

+0

是的,看起來像@interface CompanyAppDelegate:NSObject 和[CompanyAppDelegate sharedAppDelegate];在輸出終端上返回0x5b59350和(gbd)。 – Ruth85

0

[啊,當我第一次看到這個,我想你說重命名沒有達到。沒關係。]

+0

是,重命名達到且沒有錯誤執行,但似乎做任何事情 – Ruth85

1
- (IBAction) renameTabs: (id)sender 
{ 
    CompanyAppDelegate *theInstance = (CompanyAppDelegate *) [UIApplication sharedApplication].delegate; 
    [theInstance rename]; 

} 

- (void) rename 
    { 

     [(UITabBarItem*)[self.rootController.tabBar.items objectAtIndex:0] setTitle:@"User"]; 

     self.rootController.selectedIndex = 2; 

    }