从视图导航到另一个视图

时间:2011-04-30 09:00:09

标签: objective-c cocoa-touch uiviewcontroller

主要:xxxVieController 第二个:OptionViewController

在xxxView控制器中我有

.h
@interface iMetroAlertViewController : UIViewController {

    IBOutlet UIButton *option;
}

-(IBAction) goToOption;

.m
@implementation iMetroAlertViewController

-(IBAction) goToOption{
    NSLog(@"Works");
    OptionViewController *dvController = [[OptionViewController alloc] initWithNibName:@"OptionView" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:dvController animated:YES];
    [dvController release];
    dvController = nil;
}

当然会创建一个OptionViewController.xib,.m和.h ......

当我按下按钮时,没有任何反应(链接已完成,因为我有一个NSLog

我不明白为什么它不起作用

2 个答案:

答案 0 :(得分:0)

它可能无效,因为您没有导航控制器,因此调用self.navigationController会返回nil

答案 1 :(得分:0)

首先,您必须创建导航控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
 {   
    RootViewController *mainMenuVC = [[RootViewController alloc] init];   
    navigationController = [[UINavigationController alloc] initWithRootViewController:mainMenuVC];
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
    return YES;
}