目标c-iPhone:UINavigationController不显示新视图



我有以下操作,当单击按钮时会调用该操作。日志消息尚未打印,视图没有更改。此函数位于UIViewController中。

你知道我做错了什么吗?为什么新视图没有显示为红色背景?

- (void)viewDidLoad
{
[levelButton2 addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
//MORE CODE
}
- (void) clickButton 
    {
        NSLog(@"Clicked Button");
        UIViewController *myViewController = [[UIViewController alloc] init];
        myViewController.title = @"My First View";
        myViewController.view.backgroundColor = [UIColor redColor];
        //to push the UIView.
        [self.navigationController pushViewController:myViewController animated:YES];
        //[self.navigationController pushViewController:nextController animated:YES];
    }
-(void) clickButton 
{
    NSLog(@"Clicked Button");
    MyFirstController *myViewController = [[MyFirstController alloc] init];
    [self.navigationController pushViewController:myViewController animated:YES];
    [myViewController release];
}

然后进入MyFirstController.m文件和

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
    if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        self.title = @"My First View";
    }
    return self;
}

我希望它能帮助你。

您尚未在该控制器中使用或提及NIB文件。。添加NIB文件,或者用视图控制器设置导航的根视图控制器,并推送视图控制器

您必须将IBAction方法更改为

 - (void) clickButton 
    {
        NSLog(@"Clicked Button");
        MyFirstController *myViewController = [[MyFirstController alloc] initWithNibName:@"MyFirstController" bundle:nil];
        myViewController.title = @"My First View";
        myViewController.view.backgroundColor = [UIColor redColor];
        //to push the UIView.
        [self.navigationController pushViewController:myViewController animated:YES];
        //[self.navigationController pushViewController:nextController animated:YES];
    }

最新更新