导航控制器有时会使用故事板推送到黑屏

时间:2014-03-20 16:53:07

标签: objective-c uinavigationcontroller xcode5 xcode-storyboard

我在Xcode 5中使用了storyboard,我试图从UITavigationController中的UITableViewController推送到UIViewController,但我有时会得到一个黑屏(可能是75%的时间)。通过阅读其他帖子,我已经学会了使用instantiateViewControllerWithIdentifier:方法来实例化我推送到推送之前的视图控制器,所以我的代码看起来像这样:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row < self.objects.count) {
        PFObject *retrievedProduct = [self.objects objectAtIndex:indexPath.row];
        Product *product = [[Product alloc] init];

        // set properties for retrieved product 
        // (code omitted)

        //load product detail page
        self.detailPage = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MPDetailPageViewController"];
        self.detailPage.product = product;

        //Bring to detail page
        [self.navigationController pushViewController:self.detailPage animated:YES];

    } else if (self.paginationEnabled) {
        // load more
        [self loadNextPage];
    }
}

在MPDetailPageViewController中,我在viewDidAppear中执行NSLog,即使屏幕为黑色,它仍在打印。

非常感谢任何建议。

谢谢, 添

1 个答案:

答案 0 :(得分:2)

看看这是否适合你:

首先确保导入头文件:

#import "MPDetailPageViewController.h"

然后这样做:

//the storyboard
UIStoryboard *storyboard = self.navigationController.storyboard;

//the detail controller
MPDetailPageViewController *detailPage = [storyboard 
           instantiateViewControllerWithIdentifier:@"MPDetailPageViewController"];
//set the product
detailPage.product = product;
//Push to detail View
[self.navigationController pushViewController:detailPage animated:YES];

PROJECT NO SEGUE


除非你真的需要,否则以编程方式推送视图会更加困难(更多代码)。

只需在故事板中创建一个segue,如果您不确定,请参阅one of my answers

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row < self.objects.count) {
        //perform the segue
        [self performSegueWithIdentifier:@"detailSegue" sender:sender];
    } else if (self.paginationEnabled) {
        // load more
        [self loadNextPage];
    }
}

现在您可以将所有其他代码移动到以下segue deleguate:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
 if([segue.identifier isEqualToString:@"detailSegue"]){
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    PFObject *retrievedProduct = [self.objects objectAtIndex:indexPath.row];
    Product *product = [[Product alloc] init];

    MPDetailPageViewController *detailVC = (MPDetailPageViewController *)segue.destinationViewController;
    detailVC.product = product;
 }
}

PROJECT USING SEGUE