我目前有一个带有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;
}
答案 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];
}