2

我目前有一个弹出框,它是一个TableViewController带有 4 个单元格的 UITableViewController(称为)。选择其中一个单元格后,我想关闭弹出框,同时获取一个基于单元格选择设置的变量,并将其用作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;
}
4

2 回答 2

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];
}
于 2012-08-30T01:44:50.077 回答
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,则不会被调用。这可以在故事板或OtherViewController-prepareForSegue:sender:

于 2012-07-15T23:00:30.267 回答