【问题标题】:Calling a modal window from within a custom UITableViewCell从自定义 UITableViewCell 中调用模式窗口
【发布时间】:2010-11-19 01:26:29
【问题描述】:

我有一个 UITableView,我在其中创建了一个自定义 UITableViewCell,如下所示:

ItemCellController *cell = (ItemCellController *)[tableView dequeueReusableCellWithIdentifier:ContentListsCellIdentifier];
...
cell = [[[ItemCellController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ContentListsCellIdentifier] autorelease];

我这样做是为了获得 touchesBegan 和 touchesEnded 事件(这样我就可以实现长触摸)。使用 NSLog 我可以看到使用以下代码从 touchesBegan 方法中正确调用了 longTouch:

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(longTouch:) userInfo:nil repeats:YES];

问题是我无法从 longTouch 方法中调用模式窗口。

我尝试了以下方法,但收到 NSInvalidArgumentException -[ItemCellController navigationController]: unrecognized selector sent to instance 错误。

AddItemController *addView = [[AddItemController alloc] initWithNibName:@"AddItemView" bundle:nil];  

UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:addView];
controller.navigationBar.barStyle = UIBarStyleBlack;
[[self navigationController] presentModalViewController:controller animated:YES];
[controller release];

所以问题是,如何从自定义 UITableViewCell 中调用模式窗口。

谢谢

【问题讨论】:

    标签: iphone uitableview uinavigationcontroller model-view-controller


    【解决方案1】:

    navigationController 属性存在于UIViewControllers,但UITableViewCell(我猜ItemCellController 是它的子类)不是UIViewController,因此默认情况下它没有该属性。

    有几种方法:

    (1) 将UIViewController* 属性(可能称为controller)添加到您的自定义单元格类型,并使用您的init 方法(例如,initWithController:)传递一个指向您的控制器的指针。然后在你的单元格中,你可以执行:

    UINavigationController* navController = [ /* alloc and init it */ ]
    [self.controller presentModalViewController:navController animated:YES];
    

    (2) 您的应用程序委托对象可能有一个控制器属性,您可以从代码中的任何位置访问该属性。然后你可以这样做:

    MyAppDelegate* myAppDelegate = [[UIApplication sharedApplication] delegate];
    [myAppDelegate.controller presentModalViewController:navController
                                                animated:YES];
    

    (3) 这个不太直接,但更灵活。您可以设置您的根控制器(您想要呈现模态视图的那个)以侦听某个通知,并从表格单元格中发布该通知。

    从根控制器中调用的示例监听代码:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(showModal:)
                                                 name:@"show modal"
                                               object:nil];
    

    从表格单元格中调用的示例邮政编码:

    NSDictionary* userInfo = [ /* store a handle to your modal controller */ ];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"show modal"
                                                        object:self
                                                      userInfo:userInfo];
    

    根控制器的showModal: 方法将使用NSNotification 中包含的userInfo 来确定要以模态方式呈现的视图控制器。这需要更多的工作,但它会自动允许您在任何地方的任何代码呈现模态视图,而无需让它们完全访问根控制器指针。

    【讨论】:

    • 哇。通知方法很棒!它确实清理了很多东西。谢谢。
    • +1 不错的答案,虽然我想指出 iPhone 应用程序的通知中心有点矫枉过正,因为该应用程序只是真正地听自己。 Nice write-up here.
    【解决方案2】:

    它看起来不像你在任何地方设置单元格导航控制器,它似乎不喜欢 [[self navigationController] presentModalViewController:controller animated:YES];该行,检查该“自我”对象上的 nagivationController 属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多