Conditional Segue Navigation from UITableViewCell Based on UIAlertView Response

My problem seems to be a common problem, but cannot find an answer to it.

I have a situation where a user clicks on a custom UITableViewCell, I would like to display a warning, and then based on the response to the warning, either stay on the same view (the user chooses to cancel), or display a different view (if the user selects an action) . And I would like to do this using the storyboard and segues function.

How could this be done? Do you have to do it the old fashioned way?

+6
uitableview ios5 storyboard uialertview
Dec 17 '11 at 4:15
source share
3 answers

@user, just create an alertView for the old way; I know any storyboard function to do it differently. Where storyboards can help, this happens with segues. You can program segues. With the cancel button, you can simply return (i.e. do nothing). For another option, to display a different view, you can programmatically call segue to go to the desired view. If you don't have the correct segue that is already defined for some other reason on your storyboard, just create a button and use it to create a segue and name it. Name segue by clicking on it in the storyboard and use the attribute inspector to indicate its name (identifier). Then hide the button or pull it out of the window. I usually put these buttons on the toolbar and use the spacers so that they do not appear. Here is a sample code:

Call segue from the alert view delegate as follows:

[self performSegueWithIdentifier: @"done" sender: self]; 

Also implement this method to perform any necessary task to prepare for segue:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"done"]) { // [[segue destinationViewController] setManagedObjectContext:self.managedObjectContext]; // [[segue destinationViewController] setSelectedClient:selectedClient]; } } 
+11
Dec 17 '11 at 16:38
source share
β€” -

You can create segues directly from the startViewController for multiple target controllers, which can then be "executed" programmatically. You do not need to create hidden buttons for them, which seems to be a hack.

+8
Dec 30 '11 at 20:07
source share

OK I came up with a solution according to the storyboard that I like.

Example:

In my view of the table, 2 sections, grouped and cells are a dynamic prototype. Section 0 contains a single line / UITableViewCell, and I do not want it to jump. Section 1 contains several cells that I want to call, and I will go on to the details.

In the storyboard:

  • I removed segue by linking tableviewcell to the destination view controller.
  • I made a β€œgeneric” segue related to the source view controller directly with the destination view controller.
  • In the attributes on segue I set the identifier ("EditTimePeriod") and set the type to Push (I assume that Modal will work the same way).

In the source view controller:

  • In the prepareForSegue method, I processed both the regular "AddTimePeriod" segue I element that was transferred from my UIBarButtonItem (Add) and the "generic" (vc β†’ vc) "EditTimePeriod" segue.

     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // handle the click of the 'Add' bar button item if([segue.identifier isEqualToString:@"AddTimePeriod"]) { TimePeriodViewController* tpvc = (TimePeriodViewController*)segue.destinationViewController; tpvc.delegate = self; // database & entity stuff for adding the new one to the mOC, etc } // handle the click of one of the 'editable' cells - if([segue.identifier isEqualToString:@"EditTimePeriod"]) { TimePeriodViewController* tpvc = (TimePeriodViewController*)segue.destinationViewController; tpvc.delegate = self; TimePeriod * newTP = [self.timePeriodArray objectAtIndex:self.tableView.indexPathForSelectedRow.row]; tpvc.timePeriod = newTP; } } 
  • Then I applied the tableView: didSelectRowAtIndexPath method and placed my condition here. If the selected row was outside the zero section, I manually called EditTimePeriod segue, defining the sender as the selected tableviewcell:

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(self.tableView.indexPathForSelectedRow.section!=0){ [self performSegueWithIdentifier:@"EditTimePeriod" sender:[tableView cellForRowAtIndexPath:indexPath]]; } [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; return; } 

it would be nice to encode the cell in section 0 so that it is not selected in the first place! Hope this helps.

**, and then after 5 minutes I took another look and realized that I could just move the data from section 0 to the section header, which is more intuitive and was not used in any case. leaving the construct open for standard exit from each tableview table without any condition / check. It was a good exercise anyway though :)

+4
May 02 '12 at 7:04
source share



All Articles