I can't seem to figure this out for the life of me. I have a custom table view cell, in that cell I have a few buttons configured. Each button connects to other view controllers via a storyboard segue. I've recently removed these segues and put a pushViewController method in place. Transition back and forth across the various views works as expected however the destination view controller is not displaying anything! I have some code below as an example.

Buttons have this method set:

[cell.spotButton1 addTarget:self action:@selector(showSpotDetails:) forControlEvents:UIControlEventTouchUpInside];
// etc...
[cell.spotButton4 addTarget:self action:@selector(showSpotDetails:) forControlEvents:UIControlEventTouchUpInside];
// etc...

showSpotDetails Method contains this code:

- (void)showSpotDetails:(id)sender
{
    // determine which button (spot) was selected, then use its tag parameter to determine the spot.
    UIButton *selectedButton = (UIButton *)sender;
    Spot *spot = (Spot *)[spotsArray_ objectAtIndex:selectedButton.tag];

    SpotDetails *spotDetails = [[SpotDetails alloc] init];
    [spotDetails setSpotDetailsObject:spot];
    [self.navigationController pushViewController:spotDetails animated:YES];
}

The details VC does receive the object data.

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"spotDetailsObject %@", spotDetailsObject_.name);
}

The NSLog method below does output the passed object. Also, everything in the details view controller is as it was. Nothing has changed on the details VC. It just does not render anything ever since I removed the segue and added the pushViewController method. Perhaps I am missing something on the pushViewController method? I never really do things this way, I try to always use segues...

Any suggestions?

有帮助吗?

解决方案

Welcome to the real world. Previously, the storyboard was a crutch; you were hiding from yourself the true facts about how view controllers work. Now you are trying to throw away that crutch. Good! But now you must learn to walk. :) The key here is this line:

SpotDetails *spotDetails = [[SpotDetails alloc] init];

SpotDetails is a UIViewController subclass. You are not doing anything here that would cause this UIViewController to have a view. Thus you are ending up a with blank generic view! If you want a UIViewController to have a view, you need to give it a view somehow. For example, you could draw the view in a nib called SpotDetails.xib where the File's Owner is an SpotDetails instance. Or you could construct the view's contents in code in your override of viewDidLoad. The details are in the UIViewController documentation, or, even better, read my book which tells you all about how a view controller gets its view:

http://www.apeth.com/iOSBook/ch19.html

The reason this problem didn't arise before is that you drew the view in the same nib as the view controller (i.e. the storyboard file). But when you alloc-init a SpotDetails, that is not the same instance as the one in the storyboard file, so you don't get that view. Thus, one solution could be to load the storyboard and fetch that SpotDetails instance, the one in the storyboard (by calling instantiateViewControllerWithIdentifier:). I explain how to do that here:

http://www.apeth.com/iOSBook/ch19.html#SECsivc

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top