¿Cómo cambiar entre controladores cuando se toca el botón, iOS?
Frecuentes
Visto 1,545 veces
0
So i have my main viewController and all i have is a single button inside. The code of the button is :
- (IBAction)eventsButton:(id)sender {
self.eventsViewController =[[EventsViewController alloc]initWithNibName:@"EventsViewController" bundle:nil];
[self.navigationController pushViewController:eventsViewController animated:YES];}
where EventsViewController is another controller where i want to navigate when this button is clicked. But when i click it nothing happens.. I dont navigate to the other controller.
ViewController.h
-------------------
#import <UIKit/UIKit.h>
@class EventsViewController;
@interface ViewController : UIViewController <UIActionSheetDelegate>
- (IBAction)eventsButton:(id)sender;
@property (strong, nonatomic) EventsViewController *eventsViewController;
@end
ViewController.m
-----------------
#import "ViewController.h"
#import "EventsViewController.h"
@implementation ViewController
@synthesize eventsViewController;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)eventsButton:(id)sender {
self.eventsViewController =[[EventsViewController alloc]initWithNibName:@"EventsViewController" bundle:nil];
[self.navigationController pushViewController:eventsViewController animated:YES];
}
@end
I put a breakpoint in the IBACTION and i see that the code is executed , but it never navigates me to the other controller. The other controller is just a simple controller i created with Xcode , it has all the code Xcode gives and nothing mine. Any ideas?
2 Respuestas
0
My guess is you didn't put ViewController inside UINavigationController. First you have to alloc and init UINavigationController. So inside application: didFinishLounchingWithOptions:
ViewController *viewController = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible]
Deberia de funcionar.
Respondido 28 ago 12, 14:08
0
[self.navigationController pushViewController:eventsViewController animated:YES];
its works only current view controller is navigation controller.Actually you implement this code in normal view controller not in navigation controller. Check with this following code replace above line
[self presentModalViewController:eventsViewController animated:YES];
So You need to implement navigation controller for your current view controller. In Appdelegate you change root view controller to navigation controller with your view controller.
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:yourController] autorelease];
self.window.rootViewController = navController;
Respondido 28 ago 12, 14:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas objective-c ios uiviewcontroller or haz tu propia pregunta.
And if i dont want the navigation bar on my main controller i ll just put this line on the ViewWillAppear method : [self.navigationController setNavigationBarHidden:NO animated:animated]; and in the other controllers i ll put YES. Is that right? - user1511244
Great!! Thank you very much for your help! - user1511244