将值返回给父视图

发布于 2024-11-04 04:54:38 字数 279 浏览 1 评论 0原文

我遇到以下情况:我的表格单元格上连接了一个 UISwipeGestureRecognizer。滑动时,两个按钮的 .hidden-status 切换为 NO

这些细胞是定制的。意思是,这些按钮的 IBAction 位于该控制器中。现在我希望这些自定义单元格中的 UIButton 将变量发送回父视图。该父视图是 UIViewController(包含 UITableView)。

我该怎么做?我读过有关协议的内容,但我找不到可靠的例子......或者还有其他想法吗?我们都欢迎他们:)

I've got the following situation: I've got a UISwipeGestureRecognizer hooked up on my table cell. On swipe, two buttons get their .hidden-status switched to NO.

Those cells are custom made. Meaning, the IBAction of those buttons is in that controller. Now I'd like the UIButton in those custom cells to send back a variable to the parent view. That parent view is the UIViewController (containing a UITableView).

How can I do this? I've read about protocols, but I can't get a solid example for that... Or are there any other ideas? They are all welcome :)

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

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

发布评论

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

评论(1

不顾 2024-11-11 04:54:38

您遵循的方法可能会导致崩溃或内存泄漏,因为您必须保留 uitableview 单元格内容的控制器。

然而,协议是一种简单的方法,在创建表视图单元格后,您可以将视图控制器(创建表视图)分配为单元格内容视图的所有操作的委托。

在您的自定义单元格的控制器中,只需调用委托方法,它将由您的父视图处理。
这听起来可能令人困惑,但其实很简单。将尝试发布示例代码。

@protocol yourDelegate;

@interface yourViewController{
id<yourDelegate> delegate;
}
@property (nonatomic,assign) id<yourDelegate> delegate;
@end
@protocol yourDelegate
- (void)passThisActionToMainController;
- (void)passThisEventToMainController;
@end

在表视图单元格视图控制器中完成上述操作后,在主控制器中创建视图控制器的实例,然后

controller.delegate = self;

在单元格的视图控制器中,当用户点击或执行任何操作时调用

[delegate passThisActionToMainController];

[delegate passThisEventToMainController];

这样您的主控制器就会得到该事件并且可以被处理。

另一种方法是在创建单元格并处理事件时创建按钮。我会推荐这种方法。

[myButton addTarget:self action:@selector(whatMyButtonShouldDo:)   
  forControlEvents:UIControlEventTouchUpInside];

上面的行是从此处的答案复制的。

The approach you are following may result in a crash or a memory leak as you have to retain the controller of your uitableview cell content.

However protocols are simple approach where after you create your table view cell you can allocate the viewcontroller (which creates the table view) as a delegate to all actions for the cell's content view.

In your custom cell's controller just call the delegate method and it will be handled by your parent view.
It may sound confusing, but its really simple. Will try to post sample code.

@protocol yourDelegate;

@interface yourViewController{
id<yourDelegate> delegate;
}
@property (nonatomic,assign) id<yourDelegate> delegate;
@end
@protocol yourDelegate
- (void)passThisActionToMainController;
- (void)passThisEventToMainController;
@end

Once you are done with the above in your table view cell view controller, in your main controller create an instance of your view controller and then

controller.delegate = self;

In your cell's view controller when a user taps or performs any action then call

[delegate passThisActionToMainController];

or

[delegate passThisEventToMainController];

This way your main controller gets the event and it can be processed.

Alternate way to do this would be to create the buttons when you create the cell and handle the events. I would recommend this approach.

[myButton addTarget:self action:@selector(whatMyButtonShouldDo:)   
  forControlEvents:UIControlEventTouchUpInside];

The above line is copied from an answer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文