所以我有我的主viewController,我只有一个按钮。 按钮的代码是:
- (IBAction)eventsButton:(id)sender {
self.eventsViewController =[[EventsViewController alloc]initWithNibName:@"EventsViewController" bundle:nil];
[self.navigationController pushViewController:eventsViewController animated:YES];}
其中EventsViewController是另一个控制器,我想在单击此按钮时导航。但是,当我点击它没有任何反应..我没有导航到另一个控制器。
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
我在IBACTION中放了一个断点,我看到代码已执行,但它从未导航到另一个控制器。另一个控制器只是一个我用Xcode创建的简单控制器,它拥有Xcode提供的所有代码,而不是我的代码。 有什么想法吗?
答案 0 :(得分:0)
我的猜测是你没有将ViewController放在UINavigationController中。首先,你必须分配和初始化UINavigationController。所以在application: didFinishLounchingWithOptions:
ViewController *viewController = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible]
它应该有用。
答案 1 :(得分:0)
[self.navigationController pushViewController:eventsViewController animated:YES];
它的工作原理当前视图控制器是导航控制器。实际上你在普通视图控制器中实现此代码而不是在导航控制器中。请检查以下代码替换上面的行
[self presentModalViewController:eventsViewController animated:YES];
所以你需要为当前的视图控制器实现导航控制器。在Appdelegate中,您可以使用视图控制器将根视图控制器更改为导航控制器。
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:yourController] autorelease];
self.window.rootViewController = navController;