Conditional Segue navigation from UITableViewCell based on response to UIAlertView

后端 未结 3 1903
说谎
说谎 2021-02-03 14:06

My problem seems like a generic problem, yet can\'t seem to find an answer for it.

I have a situation where when the user taps on a custom UITableViewCell, I would like

相关标签:
3条回答
  • 2021-02-03 14:42

    You can create segues directly from the startingViewController to multiple destinationViewControllers that can then be "performed" programmatically. You do not need to create any hidden buttons for them, which does seem like a hack.

    0 讨论(0)
  • 2021-02-03 14:47

    OK I came up with a solution in keeping with the storyboard that I like.

    Example:

    My tableview has 2 sections, grouped, and cells are dynamic prototype. Section 0 contains one row/UITableViewCell & I don't want it to segue. Section 1 contains multiple cells that I want to trigger the segue & drill down into the detail.

    In Storyboard:

    • I removed the segue linking the tableviewcell to the destination view controller.
    • I made a 'generic' segue linking the source view controller directly to the destination view controller.
    • In the attributes on the segue, I set the identifier ('EditTimePeriod') and set the type to Push (I presume Modal would work just the same).

    In the source view controller:

    • In the prepareForSegue method I handled both the common 'AddTimePeriod' segue I control-dragged from my UIBarButtonItem (Add), along with 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 implemented the tableView:didSelectRowAtIndexPath method, and put my condition in here. If the selected row was outside of section zero I called the EditTimePeriod segue manually, 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;    
      }
      

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

    ** and then 5 minutes later I took another look and realized I could just move the data from section 0 into the section header, which is more intuitive and wasn't being used anyway. leaving the design open for a standard segue from each tableviewcell without needing any condition/check. Was a good exercise anyway though :)

    0 讨论(0)
  • 2021-02-03 15:03

    @user, Just create the alertView the old fashion way; I do know of any storyboard feature to do this differently. Where storyboard can help is with the segues. You can call the segues programmatically. With you alert view cancel button you can just return (i.e. do nothing). For the other option, to display another view, you can programmatically call a segue to transition to the desired view. If you don't have the proper segue already defined for some other reason on your storyboard, just create a button out and use that to create the segue and name it. Name the segue by clicking on it in storyboard and use the attributes inspector to give it name (identifier). Then hide the button or put it out of the view. I typically put these type of button on the toolbar and use spacers to keep them out of the view. Here's some sample code:

    Call the segue from the alert view delegate like this:

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

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

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if ([[segue identifier] isEqualToString:@"done"]) 
        {
            //    [[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];
            //    [[segue destinationViewController] setSelectedClient:selectedClient];
        }
    }
    
    0 讨论(0)
提交回复
热议问题