Iphone - 当在 UITableViewCell 中单击按钮时将视图控制器推送到导航堆栈上

发布于 2024-08-20 20:21:14 字数 216 浏览 3 评论 0原文

当在我的应用程序的表视图单元格之一中按下按钮时,我需要将某个视图控制器推送到导航堆栈上。

这可以通过使用 NSNotification 实例来通知表视图的控制器按下按钮来完成。但这将是非常重量级的,特别是因为应用程序中选项卡栏中的选择可能会导致表视图出现或消失,从而产生额外的开销,因为每当将它们切换到屏幕上或从屏幕上切换时,各种表视图都会注册和取消注册。

有人能想到更好的解决方案吗?

When a button is pushed in one of my app's table view cells, I need to push a certain view controller onto the navigation stack.

This could be done by using an instance of NSNotification to inform the table view's controller of the button press. But that would be awfully heavyweight, especially since selections in a tab bar in the app could cause the table view to appear or disappear, creating additional overhead as the various table views register and unregister themselves whenever they are tabbed onto or off of the screen.

Can anyone think of a better solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莳間冲淡了誓言ζ 2024-08-27 20:21:14

为什么不放入

[[self navigationController] pushViewController:targetViewController animated:YES];

按钮调用的方法呢?

Why not put

[[self navigationController] pushViewController:targetViewController animated:YES];

in the method called by the button?

安穩 2024-08-27 20:21:14

让你的 UITableViewController 使用 UITableViewDelegate 协议 并实现这个方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

从indexPath你可以得到哪一行被按下了,然后你就知道哪个单元格被选中了。 UITableViewController 的目的是了解单元格,单元格本身不需要按钮来触发事件来推送新视图。

Make your UITableViewController use the UITableViewDelegate Protocol and implement this method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

From the indexPath you can get which row has been pressed and then you know which cell is being selected. The purpose of the UITableViewController is to know about the cells and the cell itself does not need a button to trigger an event to push a new view.

叫思念不要吵 2024-08-27 20:21:14

我所做的是将表视图的委托设置为与其控制器相同。然后:

UITableView *myTableView = (UITableView *)self.superview;
NSIndexPath *indexPath = [myTableView indexPathForCell: self];
MyTableViewController *myTableViewController = (MyTableViewController *)(myTableView.delegate);
[myTableViewController buttonWasPressedOnCellWithIndexPath: indexPath];

What I did was set the table view's delegate to be the same as its controller. Then:

UITableView *myTableView = (UITableView *)self.superview;
NSIndexPath *indexPath = [myTableView indexPathForCell: self];
MyTableViewController *myTableViewController = (MyTableViewController *)(myTableView.delegate);
[myTableViewController buttonWasPressedOnCellWithIndexPath: indexPath];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文