从UITableViewCell到UITableView到UINavigationController的引用 [英] Reference from UITableViewCell to UITableView to UINavigationController

查看:91
本文介绍了从UITableViewCell到UITableView到UINavigationController的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户点击并按住Foo表视图单元2秒钟,则应显示模式视图.当将新的Foo添加到单元格时,也会显示模式视图.模态视图的委托协议由父UITableView子类实现.

If the user taps and holds on a Foo table view cell for 2 seconds, a modal view should be shown. The modal view is also displayed when a new Foo is added to the cell. The modal view's delegate protocol is implemented by the parent UITableView subclass.

我的按住检测代码在Foo UITableViewCell类中.

My tap and hold detection code is in the Foo UITableViewCell class.

我在引用父表视图的导航控制器以显示模式视图时遇到困难.

I'm having difficulty referencing the parent tableview's navigation controller to display the modal view.

FooModalViewController *modalController = [[FooModalViewController alloc] initWithNibName:@"FooModalViewController" bundle:nil];
FooTableViewController *tableView = (FooTableViewController *) self.superview;
foo.delegate = tableView;

看起来还可以,但是我在引用包含表视图的导航控制器时遇到了问题.下面的代码构建得很好,但引发了异常-NSOBject DidNotRecognizeSelector.

Seems OK but I'm having problems referencing the navigation controller that contains the tableview.. The code below builds OK but throws an exception - NSOBject DoesNotRecognizeSelector.

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:modalController];
[[tableView.navigationController] presentModalViewController:navigationController animated:YES];

我认为也许我的设计有缺陷..?

I think that perhaps my design is flawed..?

推荐答案

执行此操作的正确方法是为单元格提供委托协议(例如- (void)buttonPressedInCell:(UITableViewCell*)cell),并在按下按钮时使用该协议来通知控制器.然后,将对nav控制器进行引用的控制器可以执行适当的操作.

The proper way to do this is to give the cell a delegate protocol (such as, - (void)buttonPressedInCell:(UITableViewCell*)cell) and when the button is pressed use that to notify the controller. Then the controller, which will have a reference to the nav controller, can do the appropriate thing.

这篇关于从UITableViewCell到UITableView到UINavigationController的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆