Petko
Petko

Reputation: 1

Navigate from one view to another using UIButton

I have an application with 2 views . In the first one I have a button which when I clicked the user should go to the second view. I tried what is explained before here from Karoley , but it does not work . When I click the button nothing happened?

Here is the code of my action :

-(IBAction)gotoSecondPage:(id) sender{
    NSLog(@"In gotoSecondPage");

        LeoActionViewController *aSecondPageController =
        [[LeoActionViewController  alloc] 
         initWithNibName:@"LeoActionViewController" 
         bundle:[NSBundle mainBundle]];

[self.navigationController pushViewController:aSecondPageController animated:YES];
}

LeoActionViewCOntroller is a controler for a second view. It just do not switch to a second view. I do not know why

Upvotes: 0

Views: 5000

Answers (2)

Nikunj Jadav
Nikunj Jadav

Reputation: 3402

I put code your problem this will help you. First of all, you declare method and open .xib file and then connect to that button with selected touchupinside connection.

In the .h file:

- (IBAction)gotoSecondPage:(id) sender;

In the .m file:

 - (IBAction)gotoSecondPage:(id) sender
    {
        NSLog(@"In gotoSecondPage");

        LeoActionViewController *aSecondPageController = 
            [[LeoActionViewController  alloc] 
            initWithNibName:@"LeoActionViewController" 
            bundle:nil];

     [self.navigationController pushViewController:aSecondPageController animated:YES];

     [aSecondPageController release];
    }

Upvotes: 1

Thomas Clayson
Thomas Clayson

Reputation: 29925

I'm not sure in what capacity you want to switch views.

What immediately comes to mind is that you want a Navigation Controller. This is an object that lets you put view controllers on a stack and push and pop them to show and hide them. It creates a navigation pathway through your app and is easy to use. It also facilitates the 'standard' navigation bar which is found in many iphone apps.

If you just want to change one view for another view you can do many things including hiding and showing different views using setHidden:(bool)hidden. Otherwise you can use addSubview:(UIView *)view and removeFromSuperview to add and remove views completely from the superview.

Upvotes: 0

Related Questions