Changing viewController on Button Click

后端 未结 5 1971
不思量自难忘°
不思量自难忘° 2020-12-20 02:05

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 k

相关标签:
5条回答
  • 2020-12-20 02:46

    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.

    0 讨论(0)
  • 2020-12-20 02:46
    - (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;
        }
    }
    
    0 讨论(0)
  • 2020-12-20 02:48

    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.

    0 讨论(0)
  • 2020-12-20 02:49

    You can do something like this

            YourViewController *objYourViewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
            [self.navigationController pushViewController:objYourViewController animated:YES];
            [YourViewController release];
    
    0 讨论(0)
  • 2020-12-20 02:53

    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

    0 讨论(0)
提交回复
热议问题