2011-05-19 27 views
2

我在UINavigationController中有一个UITableView。在导航栏上我有一个名为add的按钮。当按下此按钮时,它将显示一个UIPopoverController,用户可以在UITableView中输入要添加为新行/单元格的数据。我的问题是如何从UIPopover添加一个新的单元格到UITableView?我是否将阵列数据传递给UIPopOver根控制器?UIPopover和UITableView数据交换

+0

我想这将取决于你的表视图的数据源。如果你在iPhone上使用模态视图来做同样的事情,它应该与iPad上的弹出窗口类似。 – BoltClock 2011-05-19 20:38:38

+0

是的,这将与模态观点一样..问题是如何?当你说它取决于数据源时,这是什么意思?我希望能够从popover中做insertRowsAtIndexPath ..这是主要目标。 – aherlambang 2011-05-19 20:48:17

回答

4

有两种解决方案,我知道这一点。一种方法是将弹出通知发送到根控制器,并应用必要的代码更新handleNotification方法中的tableView。

另一个我个人使用的是为popover设置一个委托协议。你必须将其设置是这样的:

@protocol PopoverDelegate 
- (void)addNewCell; // you can add any information you need to pass onto this if necessary such as addNewCellWithName:(NSString *)name, etc. 
@end 

@interface MyPopoverViewController..... { 
    id <PopoverDelegate> delegate; 
    // the rest of your interface code; 
} 
@property (nonatomic, retain) id delegate; 
// any other methods or properties; 
@end 

然后在你的根视图控制器头文件,你需要添加委托

@interface RootViewController .... <PopoverDelegate> { 

然后在你的根视图控制器实现文件,在实例化时分配弹出窗口委托。例如:

MyPopoverViewController *vc = [[MyViewController alloc] init]; 
vc.delegate = self; // this is where you set your protocol delegate 
myPopover = [[UIPopoverController alloc] initWithContentViewController:vc]; 
myPopover.delegate = self; 
[vc release]; 

最后,你会在某个地方在代码

- (void)addNewCell { 
    // do what you want with the tableView from here 
} 

对不起,这是一个有点长添加协议方法。我只是想确保我彻底。希望它有帮助