After much more debugging, I was able to determine it wasn't the view controller, but something to do with the the didSelectRowAtIndexPath and presentViewController. It started happening to other views I was presenting also, and not reliably. I also tried using if/else instead of switch with no help.
I am using a UITableView to present a variety of viewControllers. Depending on which index is clicked, it will present a different view controller, which can then be dismissed. For some reason one of my ViewControllers takes two clicks to display. Thus, it seems every other click is working. Other view presentations work okay, so the table is probably okay.
What I see in the ViewController about to be displayed is that on the first click, it makes it through didSelectRowAtIndexPath and then fires in the target view: ViewWillLoad, ViewLoaded, ViewWillAppear.... but not ViewDidAppear. On the second click, only ViewDidAppear will fire as it displays. On the second click it doesn't even go through didSelectRowAtIndexPath. Also, I can click anywhere on the screen on the second time and it will display.
It will continue to do this back and forth, seemingly only displaying the target view every other click. Any thoughts why this is occuring?
/* RightPanelViewController */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch(indexPath.row){
case 0:
// this view takes two clicks to display
[self presentViewController:self.delegateRef.savedRidesViewController animated:YES completion:nil];
break;
case 1:
// this works fine, as do others
[self presentViewController:self.delegateRef.sensorsViewController animated:YES completion:nil];
break;
case 2:
...
}
/* savedRidesViewController */
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
正确的解决方法如下:
Ultimately I found this solution was able to fix it, but I'm not entirely sure why. By using theperformSelector with no delay, all the views load instantly (perhaps faster than without it?) and reliably every time.It isn't as clean of code, but it does reliably work now. Am still curious why this is needed.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch(indexPath.row){
case 0:
[self performSelector:@selector(load0:) withObject:nil afterDelay:0];
break;
case 1:
[self performSelector:@selector(load1:) withObject:nil afterDelay:0];
break;
case 2:
[self performSelector:@selector(load2:) withObject:nil afterDelay:0];
break;
...
}
}
-(void)load0:(id)sender {
[self presentViewController:self.delegateRef.zonesViewController animated:YES completion:nil];
}
-(void)load1:(id)sender {
[self presentViewController:self.delegateRef.sensorsViewController animated:YES completion:nil];
}
-(void)load2:(id)sender {
[self presentViewController:self.delegateRef.savedRidesViewController animated:YES completion:nil];
}