Changing viewController on Button Click
I am new to iPhone programming. what I am trying is I have one screen with a button. And I want to change the view controller not only the view when I click that button (I know how to add subview) because from that 2nd view controler, I 开发者_如何学运维have to go to the third view which is not possible if I add subview in first place. Can anybody please help me with that? Is this possible? and if yes, how? All the views and view controller are created programmaticaly. I am not using IB.
EDIT: here is the relevant code that fires when clicking button
-(id)showCurrentLoc:(id)sender {
locationController = [currentLocController alloc];
[entry removeFromSuperview];
[newLoc removeFromSuperview];
[currentLoc removeFromSuperview];
[self.view setBackgroundColor:[UIColor clearColor]]; //[self.view addSubview: [locationController view]];
[self.navigationController pushViewController:locationController animated:YES]; [locationController release];
return 0;
} //Location Controller is the tableViewController
Thanks Vik
You can do something like this
YourViewController *objYourViewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
[self.navigationController pushViewController:objYourViewController animated:YES];
[YourViewController release];
Usually, you use a navigation controller for this sort of thing so that the user can easily go back to the previous view. Your view controller would then do something like this:
[self.navigationController pushViewController:someNewViewController animated:YES];
If you want to manage the view controllers yourself, you can always just change the window's rootViewController
property. Please read View Controller Programming Guide for the complete picture.
UINavigationController is what you need. It manages a stack of UIViewController
s and if you want to add new UIViewController just push it into this navigation stack. It automates back button behavior for you, and you can pop your current UIViewController from stack whenever you are done with it.
You could work with a UINavigationController. Adding your first UIViewController like this in the init method:
[self setViewControllers:[NSArray arrayWithObject:viewController]];
And then when a button is click or a choice is made, your push the second few controller with (in the first viewController):
[self.navigationController pushViewController:controller animated:YES];
This way you will also get an automatic (back button). Basicly you create a stack of UIViewControllers that you can push and pop like with a normal stack.
I hope this helps. Look into the following: UINavigationController Class Reference
- (void) loadViewAtIndex:(NSInteger)index {
[self unloadViewAtIndex:activeViewIndex];
switch (index) {
case 1:
{
if (viewController1 == nil)
{
viewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
}
viewController1.view.frame = CGRectMake(0.0, 0.0, viewController1.view.frame.size.width, viewController1.view.frame.size.height);
[window addSubview:viewController1.view];
}
break;
case 2:
{
if (viewController2 == nil)
{
viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
}
viewController2.view.frame = CGRectMake(0.0, 0.0, viewController2.view.frame.size.width, viewController2.view.frame.size.height);
[window addSubview:viewController2.view];
}
break;
default:
break;
}
activeViewIndex = index;
}
- (void) unloadViewAtIndex:(NSInteger)index {
switch (index) {
case 1:
{
if (viewController1 != nil)
{
[viewController1.view removeFromSuperview];
[viewController1 release];
viewController1 = nil;
}
}
break;
case 2:
{
if (viewController2 != nil)
{
[viewController2.view removeFromSuperview];
[viewController2 release];
viewController2 = nil;
}
}
break;
default:
break;
}
}
精彩评论