简体   繁体   中英

How to change view on button click in iPhone without navigation controller?

I want to know how to change view on button click in iPhone without navigation controller?

If you're doing this with a UIViewController, you would probably do this like following:

- (IBAction)change {
    UIViewController* viewController = [[UIViewController alloc] init];
    [self.view addSubView];
    // Save the UIViewController somewhere if necessary, otherwise release it
}

Not sure why you don't want to use a UINavigationController though. If it's the navigation bar at the top you don't want to see, there's a property for that so you can hide that bar. Not sure about it's name, probably navigationBarHidden or something like that. You can look that up in the API.

There are many different ways you can do that, and you should possibly provide more information about your app.

A generic way to do it is the following, with animation:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

[vc1 viewWillDisappear:YES];
[vc2 viewWillAppear:YES];
vc1.view.hidden = YES;
vc2.view.hidden = NO;
[vc1 viewDidDisappear:YES];
[vc2 viewDidAppear:YES];

[UIView commitAnimations];

In this case, both views are there, you only hide/show them as you need. Alternatively, you could add/remove the view from their superview:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

[vc1 viewWillDisappear:YES];
[vc2 viewWillAppear:YES];
[vc1 removeFromSuperview];
[masterController.view addSubview:vc2.view;
[vc1 viewDidDisappear:YES];
[vc2 viewDidAppear:YES];

[UIView commitAnimations];

Place this in Button IBAction method

[mCurrentView removeFromSuperview];
[self.view addSubView:mNewView];

Assume you have two view myView1 and myView2 and one button myUIButton in memory

myView1.tag = 1;
myView2.tag = 2;
myUIButton.tag = 1; //Current visible view is myView1

-(void) ButtonCLicked:(id) sender 
{
       UIButton* myButton = (UIButton*)sender;


       if(sender.tag == 1)
       {
           [myView1 removeFromSuperview];
           [self addSubview:myView2]
           myButton.tag = myView2.tag;
       }
       else 
      {
          [myView2 removeFromSuperview];
          [self addSubview:myView1]
           myButton.tag = myView1.tag;
      }
}

You can place two UIView variables in the header file and hide/show one of them or you can make another layer over the existing view like this:

-(IBAction)changeView {

    UIView *view = [[UIView alloc] initWithNibName:@"letMeSeeThis" bundle:nil];
    [self.view addSubview:view];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM