【问题标题】:How do I make a UIViewController the delegate for a separate UITableViewController?如何使 UIViewController 成为单独 UITableViewController 的委托?
【发布时间】:2011-02-16 09:53:58
【问题描述】:

我有一个 UITabBarController,它的一个选项卡上有一个 UINavigationController。然后 UINavigationController 将自定义 UIViewController 作为其主视图。

基于由自定义 UIViewController 中的按钮触发的 IBAction,我将 UITableView 控制器推送到 UINavigationController 堆栈,就像这样。 (注意:这些只是实际代码的 sn-ps)。

主 UIViewController 的头文件:

@interface ATMs : UIViewController <CLLocationManagerDelegate, NSFetchedResultsControllerDelegate, MKMapViewDelegate> {
}
- (IBAction) showLocationList;
@end  

主 UIViewController 的实现文件:

@implementation ATMs
- (void)showLocationList {

    LocationList *locationTableController = [[LocationList alloc] initWithNibName:@"LocationList" bundle:nil];

    [self.navigationController pushViewController:locationTableController animated:YES];
    [locationTableController release];

}

locationTableController 视图控制器对 Core Data 进行获取对象列表并将它们显示在表格上。当用户选择一行时,我希望发生以下事情:

  • locationTableController 弹出导航控制器,以便我的ATMs 视图控制器可见
  • 调用ATMs中的方法,传入locationTableController中点击的行所代表的对象

我试图通过在showLocationList 中使用[locationTableController setDelegate:self]; 来做到这一点,就在我alloc UITableViewController 的下方,并将UITableViewDelegate 添加到ATMs 的标题中。
但是当我执行上述操作时,我收到一个构建错误,指出locationTableController 可能无法响应setDelegate

【问题讨论】:

    标签: iphone cocoa-touch ios4


    【解决方案1】:

    简单来说,无需检查其余代码,您需要实例化 UITableViewController 并将其 tableView(不是 TableViewController)委托设置为 self。

    然后你可以在你的 ATM UIViewController 中实现- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath,一旦在你的 tableView 中选择了一行,它就会被调用。

    我还将按照下面的代码以模态方式呈现您的 tableViewController,并将其声明为实例变量,以便在调用 tableview 委托方法时将其关闭:

    @class LocationList
    @interface ATMs : UIViewController
    {
       LocationList *locationTableController
       ...
    }
    

    在你的实现中:

    @implementation ATMs
    ...
    - (void)showLocationList 
    {
        locationTableController = [[LocationList alloc] initWithNibName:@"LocationList" bundle:nil];
        locationTableController.tableView.delegate = self;
        [self presentModalViewController:locationTableContrller animated:YES];
        [locationTableController release];
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [locationTableController dismissModalViewControllerAnimated:YES];
        // Rest of your implementation code here
    }
    

    【讨论】:

      【解决方案2】:

      这可以通过各种方法来实现,例如:-

      1. 委托模式
      2. 通知
      3. 通过使用类方法(getter/setter),在 ATM 控制器中,在其他一些管理器类中
      4. 键值观察。

      但我认为最好的方法是创建您自己的委托方法,并在 LocationList 中选择行时发送回调,以及您想要在 ATM 控制器上的数据

      【讨论】:

      • 当您使用 locationTableController setDelegate:self]; 时出现错误,但您尚未在 locationList tableController 中定义任何 delegate' accessor (getter/setter) in LocationList. This is the reason, it is not able to find any setter for Delegate. If you want to use this approach, then try to create a property with name delegate`
      • 这并不完全正确。 OP 需要将 self 设置为 tableView 而不是 tableViewController 的委托。
      • @Rog 是的,我刚刚注意到了。它应该是 tableView.delegate,而不是 tableViewController.delegate。谢谢指点
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多