以编程方式打开一个新的视图控制器给我一个黑屏

时间:2013-07-02 09:34:19

标签: ios uiviewcontroller

我已经检查了类似的问题,但没有一个适合我的情况(他们都提到了我没有的函数loadView)。我认为我想做的很容易,但我无法弄清楚它为什么会发生。

我想按下单元格中的按钮以编程方式打开新的视图控制器。这是我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    dealViewController=[[DealViewController alloc]init];
    if (indexPath.row==0){
        [self presentModalViewController:(UIViewController *)dealViewController animated:TRUE];
    }

}

在我的另一个控制器中:

@implementation DealViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"Opened");
}

打印Opened,但模拟器显示黑屏。我也试过这个:

dealViewController=[[DealViewController alloc]initWithNibName:@"DealViewController" bundle: nil];

但是我遇到了段错误。我做错了什么?

2 个答案:

答案 0 :(得分:4)

试试这个:

Apple Doc for StoryBoard

另外,您可以通过performSegue:withIdentifier。

来完成

A Good Tutorial on same

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"<Your StoryBoard_Name" bundle:nil];

    dealViewController=[storyboard instantiateViewControllerWithIdentifier:@"ViewController_Identifiter"];
        if (indexPath.row==0){
            [self presentModalViewController:(UIViewController *)dealViewController animated:TRUE];
        }

    }

答案 1 :(得分:0)

如果您使用的是故事板,请使用以下代码

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"dealViewController" bundle:nil];

    dealViewController=[[DealViewController alloc]initWithNibName:@"DealViewController" bundle: nil];
        if (indexPath.row==0){
            [self presentModalViewController:(UIViewController *)dealViewController animated:TRUE];
        }

    }