如何在选择单元格后解除其他视图控制器上的UITableViewController popover和更新插座?

时间:2012-07-15 22:02:38

标签: objective-c xcode uitableview xcode4.3 popover

我目前有一个带有4个单元格的UITableViewController(名为TableViewController)的popover。在选择其中一个单元格后,我想解除弹出窗口,同时取一个基于单元格选择设置的变量,并将其用作我的另一个标签出口中的文本(即self.styleText.text = thePopoverCellVariable;)查看控制器(称为OtherViewController)。我怎么能这样做?

现在这只有在我通过点击外部解除弹出窗口时才有效。 TableViewController.m中的代码是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ((OtherViewController *)self.presentingViewController).thePopoverCellVariable=theVariable;
}

OtherViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UIStoryboardPopoverSegue *popoverSegue;
    popoverSegue = (UIStoryboardPopoverSegue *)segue;

    UIPopoverController *popoverController;
    popoverController = popoverSegue.popoverController;
    popoverController.delegate=self;
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    thePopoverCellVariable = ((StyleViewController *)popoverController.contentViewController).theVariable;
    self.styleText.text=thePopoverCellVariable;
}

2 个答案:

答案 0 :(得分:0)

TableViewController

@property (weak, nonatomic) UIPopoverController *popoverController; // In header

@synthesize popoverController = _popoverController;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    self.theVariable = … // get the variable from the cell.

    [self.popoverController dismissPopoverAnimated:YES];
}

OtherViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIStoryboardPopoverSegue *popoverSegue;
    popoverSegue = (UIStoryboardPopoverSegue *)segue;

    UIPopoverController *popoverController;
    popoverController = popoverSegue.popoverController;
    popoverController.delegate=self;

    TableViewController *tableViewController = (TableViewController *)popoverSegue.destinationViewController;
    tableViewController.popoverController = popoverController;
}

对于之前的混淆感到抱歉,我认为这会像模态视图控制器一样工作。


好的,我不认为这是最佳的,但它应该有用。


<强>更新

如果您未在-popoverControllerDidDismissPopover:中设置popoverController.delegate = self,则无法调用{p> OtherViewController(请参阅:delegate)。这可以在故事板或-prepareForSegue:sender:

中完成

答案 1 :(得分:0)

终于想出来了。 @Jeffery Thomas有99%的正确答案。另一个1%是您必须从根视图控制器中解除弹出窗口,在本例中为OtherViewController。所以我在头文件中创建了popoverController变量,并按如下方式实现了我的代码

//OtherViewController.m
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UIStoryboardPopoverSegue *popoverSegue;
    popoverSegue = (UIStoryboardPopoverSegue *)segue;
    popoverController = popoverSegue.popoverController;
    pCVisible = YES;
    [[segue destinationViewController] setDelegate:self];
}

- (void) setDataFromPopover {
    if (pCVisible) {
        [popoverController dismissPopoverAnimated:YES]; // THIS IS KEY! this is where the popover is dismissed, not in the popover itself
    }
}

和...

//TableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //variable = whatever

    OtherViewController *initialView;
    initialView=(OtherViewController *)self.delegate;
    initialView.theLabel.text = variable;
    [initialView setDataFromPopover];
}