سؤال

I'm using storyboard in Xcode 5 and I'm trying to push to a UIViewController from a UITableViewController that's housed in a UINavigationController, but I sometimes get a black screen (probably 75% of the time). From reading other posts, I've learned to use the instantiateViewControllerWithIdentifier: method to instantiate the view controller I'm pushing to prior to pushing, so my code looks like this:

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

And in MPDetailPageViewController I do an NSLog in viewDidAppear and even when the screen is black, it is still printing.

Any advice would be greatly appreciated.

Thanks, Tim

هل كانت مفيدة؟

المحلول

see if this works for you:

first make sure the header file is imported:

#import "MPDetailPageViewController.h"

then do this:

//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


Unless you really have to, it is more difficult (more code) to push views programmatically.

Just create a segue in storyboard please refer to one of my answers if your not sure.

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

Now you can move all that other code to the following 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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top