Changing viewController on Button Click

僤鯓⒐⒋嵵緔 提交于 2019-12-18 07:11:56

问题


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


回答1:


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.




回答2:


You can do something like this

        YourViewController *objYourViewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
        [self.navigationController pushViewController:objYourViewController animated:YES];
        [YourViewController release];



回答3:


UINavigationController is what you need. It manages a stack of UIViewControllers 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.




回答4:


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




回答5:


- (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;
    }
}


来源:https://stackoverflow.com/questions/5814752/changing-viewcontroller-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!