2011-05-03 29 views
0

我需要動態地添加一個包含具有多個按鈕的導航欄的UIViewController。當按下其中一個按鈕時,我需要將顯示的視圖替換爲另一個,同時保持導航欄。IOS從動態創建的導航欄中顯示不同的視圖

CGRect appFrame = [[UIScreen mainScreen] applicationFrame]; 

ViewControllerOne* viewOne = [[ViewControllerOne alloc] init]; 
[viewOne.view setFrame:appFrame]; 

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44.01)]; 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3]; 

UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(back:)]; 
[buttons addObject:bi]; 
[bi release]; 

bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
[buttons addObject:bi]; 
[bi release]; 

bi = [[UIBarButtonItem alloc] initWithTitle:@"Two" style:UIBarButtonItemStyleBordered target:self action:@selector(showTwo:)]; 
[buttons addObject:bi]; 
[bi release]; 

[tools setItems:buttons animated:NO]; 
[buttons release]; 
viewOne.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
[tools release]; 

m_navViewController = [[UINavigationController alloc] initWithRootViewController:viewOne]; 
[m_navViewController.view setFrame: appFrame ]; 
[self addSubview: m_navViewController.view]; 

現在,當有人按下兩個按鈕,我想刪除viewOne並添加ViewControllerTwo對象

- (void) showTwo:(id)sender{ 
    ViewControllerTwo* viewTwo = [[ViewControllerOne alloc] init]; 
    [viewOne.view setFrame:[[UIScreen mainScreen] applicationFrame]]; 

    // remove viewOne from m_navViewController and add viewTwo 
} 

換句話說,我想按項目之一,在導航欄上顯示不同的看法但爲所有視圖保留相同的導航欄。

請注意導航欄實際上將持有五個按鈕。爲了解釋的目的,我簡化了它。

在此先感謝。

回答

1

您可以使用導航控制器模板;將這些按鈕添加到IB的導航欄中(或者按照上面的方法在代碼中),然後當其中一個按鈕被輕敲時,按下相應的視圖控制器。

+0

示例的機會 – user346443 2011-05-03 10:31:29

+0

只需使用此代碼並將其作爲IBAction連接到每個按鈕: '[self.navigationController pushViewController:someViewController animated:YES];' – FeifanZ 2011-05-03 20:59:13