2013-08-02 60 views
0



UINavigationItem是不是在我的ViewController

我正在寫在使用Xcode中的iOS簡單的應用中展示的,我試圖加載另一個ViewController爲模式。起源HomeScreenViewController(從UIViewController繼承),我正在加載模態是源於項目的Storyboard。
然後,如一個按鈕按下事件的響應,我加載這個模式是這樣的:

-(IBAction)onAddButtonPressed:(UIButton *)sender { 
    MyAnotherViewController *vc = [[MyAnotherViewController alloc] init]; 
    [self presentViewController:vc animated:YES completion:nil]; 
} 

MyAnotherViewController未在Storyborad因爲它的一個簡單類表示以顯示導航欄和文本域。該代碼是(部分代碼,其餘爲Xcode的自動生成方法):

@implementation MyAnotherViewController 

- (void)viewDidLoad { 
    [self.navigationItem setTitle:@"Example"]; 
    [self.view addSubview:[[UITextView alloc]initWithFrame:self.view.bounds]]; 
} 
@end 

該問題(也可以附圖所示),該navigationItem未示出由於某種原因。
我也驗證了self.navigationItem不是nil而不是。更甚者,我可以在調試模式下看到標題實際設置爲「示例」。

As can be seen in the screenshot, the UITextView captures the entire screen

你的幫助是十分讚賞,
乾杯......

+0

嗨,你需要通過對其進行管理添加工具欄和欄按鈕項目。 – Hindu

+0

感謝您的注意:)下面的Vinzzz給了我確切的答案! –

回答

2

當視圖控制器是一個UINavigationController裏面,這樣一個UIViewController的UINavigationItem屬性僅用於:

-(IBAction)onAddButtonPressed:(UIButton *)sender { 
    MyAnotherViewController *vc = [[MyAnotherViewController alloc] init]; 
    UINavigationController *navCtl = [[UINavigationController alloc] initWithRootController:vc]; 
    [self presentViewController:navCtl animated:YES completion:nil]; 
} 
0

如果您的MyAnotherViewController不是UINavigationController的子類,或者您沒有手動添加UINavigationItemUIViewController無法顯示導航項目。也許你可以試着將MyAnotherViewController換成UINavigationController

// Assume you have adopted ARC 
-(IBAction)onAddButtonPressed:(UIButton *)sender { 
    MyAnotherViewController *vc = [[MyAnotherViewController alloc] init]; 
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; 
    [self presentViewController:nav animated:YES completion:nil]; 
} 

而在你的MyAnotherViewController-viewDidLoad,你只需要做到這一點:當我們加載視圖控制器模型,然後導航欄是隱藏每次

-(void)viewDidLoad { 
    self.title = @"Example"; 
    /* 
    * Your other code 
    */ 
} 
相關問題