如何根据在Table View Controller Popup上进行的选择更新父视图

时间:2015-02-10 13:02:53

标签: ios objective-c swift

我有一个带有按钮的ViewController。附图中的参考#1。 现在单击该按钮可打开Table View控制器作为Popover。附图中的参考#2。 enter image description here

到目前为止还不错。 现在我要做的是点击表格视图中的一行,我想关闭Popover并根据点击的内容更新#1 View控制器。

使用Segue Show从屏幕底部重新加载整个视图控制器,这是我不想要的。我只想让弹出窗口关闭并查看要更新。

尝试重新创建类似于itune上的弹出窗口的流程。 enter image description here

我该怎么办?

4 个答案:

答案 0 :(得分:0)

在要添加到弹出窗口的表视图控制器类中,添加一个属性,该属性包含在进行选择时要退出的块。类似的东西:

@property (nonatomic,copy) void (^onSelect)(NSString *item);

在表格视图控制器中,当选择一个项目时,请致电:

self.onSelect(<insert item selected>);

我在这里使用过NSString,但你可以使用任何东西。

如果您愿意,可以对表格中的单元格执行相同的操作,如果您希望单元格告诉表格然后表格告诉创建它的代码,则在cellForRowAtIndexPath中设置块。

在主控制器(1)中创建表视图控制器时,只需执行以下操作:

tableControllerYouCreated.onSelect=(NSString *itemSelected) {
  <insert main view update code>      
}];

这将允许单元格单击以通知表格的选择,然后可以通知主视图。

您可以决定如何将表格弹出窗口作为单独的问题弹出。

答案 1 :(得分:0)

试试NSNotificationCenterdidSelectRowAtIndexPath发布通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"desiredEventHappend" object:nil];

并在viewcontrollers viewDidLoad中添加观察者

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someThingInterestingHappened:) name:@"desiredEventHappend" object:nil];

然后实现选择器

- (void)someThingInterestingHappened:(NSNotification *)info
{
    NSLog(@"info : %@",info.userInfo);
    //update view controller
}

答案 2 :(得分:0)

或者在Swift 3中......

在didSelectRowAtIndexPath:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "desiredEventHappend"), object: nil)

在viewController / viewDidLoad:

NotificationCenter.default.addObserver(self, selector: #selector(self.myFunction), name: NSNotification.Name(rawValue: "desiredEventHappend"), object: nil)

而不是func。:

func myFunction(notification: NSNotification) {
//do anything
}

答案 3 :(得分:0)

如何使用PopUp Swift ViewController中的XIB更新Objective-c tableView。

弹出ViewController中的

: (在您想要更新表的方法内)。你的快速课程。

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdf", object: nil)

在父视图控制器的viewdidload方法中:(Obj-c class)

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(reloadTable:)
                                             name:@"NotificationIdf"
                                           object:nil];

最终在父视图控制器中定义函数:(Obj-c class)

-(void)reloadTable:(NSNotification*)noti { [self.tableView beginUpdates];[self.tableView endUpdates]; [self.tableView reloadData]; }