How to prevent segue transfer of control to another UIView when UIPopover is displayed from UITableView?

StackOverflow https://stackoverflow.com/questions/23614521

質問

I have an iPad app (XCode 5.1.1, iOS 7.1, ARC and Storyboards). I have a UITableView that calls other UIViews and UITableViews. (oServicesCell is outlined in image below)

UITableView

Normally, when the user taps the Services row, I display another UITableView of data. However, there are times when I need to display a UIPopover rather than the `UITableView.

This is the code I'm using, which works but the anchor disappears when the the normal UITableView is shown (the UIPopover is also shown... I can see it before the app crashes with -[UIPopoverController dealloc] reached while popover is still visible.).

#pragma mark prepareForSegue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"segueStartTime"]) {
    DateTime *dt = (DateTime *)segue.destinationViewController;
dt.whichTextField = @"oStartTime";
}
else if ([segue.identifier isEqualToString:@"segueEndTime"]) {
    DateTime *dt = (DateTime *)segue.destinationViewController;
    dt.whichTextField = @"oFinishTime";
}
else if ([segue.identifier isEqualToString:@"segueFromServices"]) {

    SingletonServicesType *sharedInstance = [SingletonServicesType sharedServicesType];

    //  initialize
    UIViewController* popoverContent = [[UIViewController alloc] init];
    UIView *popoverView;
    SZTextView *activityField;
    UIPopoverController *popoverController;

    if([sharedInstance.globalServicesType  isEqual: @3])  {  //  "manual entry"?
        popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 150)];  //  was 216
        popoverContent.preferredContentSize = CGSizeMake(250.0, 150.0);

        activityField = [[SZTextView alloc] init];
        activityField.frame =  CGRectMake(0, 0, 250, 150);

        activityField.placeholder = NSLocalizedString(@"Enter your activity for this appointment only",nil);
        [activityField becomeFirstResponder];

        //  add it to the popover
        popoverContent.title = NSLocalizedString(@"manual",nil);
        [popoverView addSubview:activityField];
        popoverContent.view = popoverView;


        //  if previous popoverController is still visible... dismiss it
        if ([popoverController isPopoverVisible]) {
            [popoverController dismissPopoverAnimated:YES];
        }

        popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
        popoverController.delegate = (id)self;

        [popoverController setPopoverContentSize:CGSizeMake(250, 150) animated:NO];

        //  show it
        [popoverController presentPopoverFromRect: oServicesCell.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];

    }
}
}

How do I prevent the transfer of control the the UITableView? All I want to do is display the UIPopover.

役に立ちましたか?

解決

-[UIPopoverController dealloc] reached while popover is still visible.

This error means that you need to have a strong pointer to the popover controller somewhere the entire time it is being displayed.

In the code above you have a local variable to the popover controller, which will be deallocated when the method ends. Instead have a property on whatever class that is above, which will hold the popover for as long as it is required - you can set the property to nil in the popover delegate method when it dismisses.

Your other issue is a limitation of storyboards - you can't link two different segues from the same thing (a cell selection). The simplest thing would probably be to implement - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender on the view controller - if you are showing the table, return YES, otherwise, show the popover. By the time it gets to prepareForSegue it's too late - you'll show the popover and perform the segue.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top