Conditional Segue navigation from UITableViewCell based on response to UIAlertView

血红的双手。 提交于 2019-12-03 08:44:25

问题


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 to display an alert and then based on the response to the alert, either stay on the same view (user selecting cancel) or display another view (if the user selects proceed). And I would like to do this using the storyboard feature & segues.

How would one go about this? Do you have to do this the old fashioned way?


回答1:


@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];
    }
}



回答2:


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.




回答3:


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 :)



来源:https://stackoverflow.com/questions/8542538/conditional-segue-navigation-from-uitableviewcell-based-on-response-to-uialertvi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!