2

我检查了类似的问题,但没有一个适用于我的案例(他们都提到了我没有的函数 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];

但我得到一个段错误。我究竟做错了什么?

4

2 回答 2

4

试试这个:

用于 StoryBoard 的 Apple Doc

此外,您可以通过 performSegue:withIdentifier 来完成。

一个很好的教程

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

    }
于 2013-07-02T10:21:56.763 回答
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];
        }

    }
于 2013-07-02T09:48:31.347 回答