Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prepareForSegue getting called twice, with Attempt to present <UINavigationController> while presentation is in progress

I am new to ios programming and asking here, but I visit all the time! I am stumped at why I am getting this problem, it compiles with no errors and I have checked and checked all my outlets and identifiers in my MainStoryboard.

I have 2 UITableViewControllers, I am passing a string from the first to the second when the user selects an item in the table, so in FirstTableViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int sel = indexPath.row;
    if (sel == 0) {
        _keyName = [NSString stringWithString:_string1];
        NSLog(@"the table was selected at cell 0, %@", _string1);
    }
    if (sel == 1) {
        _keyName = [NSString stringWithString:_string2];
    }
    // more code below...
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ResultsSegue"])
    {
        UINavigationController *navigationController = segue.destinationViewController;
        ResultsViewController *rv = [[navigationController viewControllers] objectAtIndex:0];
        [rv setResults: _keyName];
        NSLog(@"in the progress view, %@", _keyName);
        //rv.delegate = (id)self;
        rv.delegate = self;            
    }
}

And in my ResultsViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"in the results, %@", _results);
    NSLog(@"in the results view");
}

In the NSlog readout I get: ... in the progress view, (null) in the results, (null) in the progress view, The Right String Warning: Attempt to present on

Then when I hit the cancel button to return to the firstTableview and press the detail view again it no longer shows null..

in the progress view, The Right String in the results, The Right String in the progress view, The Right String

like image 500
BubbleMop Avatar asked Feb 18 '13 16:02

BubbleMop


2 Answers

Rob's answer helped me, as well - thanks! I'm coding in Swift, so for those who run into this while Swifting, here's how to get the index (or index row) clicked on in Swift 3:

var rowClicked = (self.tableView.indexPathForSelectedRow?.row)!
like image 137
Gallaugher Avatar answered Sep 22 '22 13:09

Gallaugher


The problem is prepareForSegue is called before didSelectRowAtIndexPath. You should just eliminate the didSelectRowAtIndexPath method, and do everything in prepareForSegue. You can use the following line to get the indexPath you need:

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
like image 20
rdelmar Avatar answered Sep 22 '22 13:09

rdelmar