user746909
user746909

Reputation: 105

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?

Upvotes: 3

Views: 17287

Answers (5)

sergio
sergio

Reputation: 69027

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];

Upvotes: 3

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

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

Upvotes: 1

pkoning
pkoning

Reputation: 384

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.

Upvotes: 4

Naveen Thunga
Naveen Thunga

Reputation: 3675

Place this in Button IBAction method

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

Upvotes: 1

Leon
Leon

Reputation: 410

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

Upvotes: 0

Related Questions