【发布时间】:2011-05-03 10:22:31
【问题描述】:
我需要动态添加一个 UIViewController,其中包含一个带有多个按钮的导航栏。当按下其中一个按钮时,我需要将显示的视图换成另一个,同时保持导航栏。
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
}
换句话说,我想通过按导航栏中的一项来显示不同的视图,但为所有视图保持相同的导航栏。
请注意,导航栏实际上包含五个按钮。出于解释的目的,我对其进行了简化。
提前致谢。
【问题讨论】:
标签: ios uiviewcontroller uinavigationcontroller