开发者

IOS Display different views from dynamically created nav bar

开发者 https://www.devze.com 2023-03-02 02:41 出处:网络
I need to dynamically add a UIViewController that contains a nav bar with multiple buttons. When one of the buttons is pressed i need to swap the displayed view for another, while keeping the nav bar.

I need to dynamically add a UIViewController that contains a nav bar with multiple buttons. When one of the buttons is pressed i need to swap the displayed view for another, while keeping the nav bar.

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] in开发者_Python百科itWithCapacity: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];

Now when someone presses the Two button i would like to remove viewOne and add a ViewControllerTwo object

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

    // remove viewOne from m_navViewController and add viewTwo
}

In other words i want to show different views by pressing one of items in the nav bar but keep the same nav bar for all views.

Please note the nav bar will actually hold five buttons. I have simplified it for explanation purposes.

Thanks in advance.


You could use the nav controller template; add the buttons to the nav bar in IB (or in the code as you do above), then when one of them is tapped you push on the corresponding view controller.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号