iPhone:UINavigationController没有显示新视图

时间:2011-06-16 04:21:37

标签: iphone objective-c cocoa-touch

我点击按钮时会调用以下操作。日志消息仍然打印,视图不会更改。这个函数在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];
    }

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你必须稍微改变你的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];
    }

答案 2 :(得分:0)

-(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;
}

我希望它会对你有所帮助。