ios - 以编程方式打开一个新的 View Controller 给我一个黑屏

标签 ios uiviewcontroller

我检查过类似的问题,但没有一个适用于我的情况(他们都提到了我没有的函数 loadView)。我认为我想做的事情很容易,但我不明白为什么会这样。

我想在按下单元格中的按钮时以编程方式打开新的 View Controller 。这是我的代码:

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

}

在我的另一个 Controller 中:

@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];

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

最佳答案

试试这个:

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

    }

关于ios - 以编程方式打开一个新的 View Controller 给我一个黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17422131/

相关文章:

ios - 如何删除AVExportSession的AVMetadata

ios - 发布 header 数据和 x-www-form-urlencoded 数据时出错

ios - shouldShowMenuForRowAtIndexPath 和复制菜单位置

iOS 确保 View Controller 转换结束

ios - 在 xib、界面生成器中创建 UIView,而不使用 UIViewController 将其附加到窗口

iphone - WebView 与 Safari

ios - 无法使用类型为 "save"的参数列表调用 "(nil)'

ios - 父 View Controller 调用自身而不是 subview

iphone - 渐变背景 UIScrollView

ios - 为什么设备旋转时 self.view.frame 和 self.view.bounds 不同?