您必须将按钮操作委托给控制器。你应该自己研究一下,但我会给你一个简短的例子。
假设你有一个 UITableViewCell ,当然 UITableView 有它自己的委托方法,比如
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
这些是单元格的选择,但您想控制按钮操作。
所以你有一个像这样的单元格内的按钮
UIButton *someButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0, 50, 50)];
[someButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:someButton];
这是在你的单元格中。
和.h 你的单元格你这样写委托。
@protocol SomeProtocol <NSObject>
- (void) buttonPressedInCell:(NSString *) data;
@end
以及您在.h中的委托属性
@property (nonatomic, strong) id<SomeProtocol> delegate;
还有你的cell.m
最后你可以委托你的按钮操作
- (void) buttonClicked {
[delegate buttonPressedInCell];
}
因此,在此之后,您可以随时委托此对象,在您的情况下,您想将它交给您的UITableView,让我们来做吧。
你喜欢CustomTableViewCell
你的控制器必须yourController<SomeProtocol>
你必须像yourCell.delegate = self;一样委派你的单元格
然后实现你的委托方法
- (void) buttonPressedInCell:(NSString *) data {
// do something like pushing new controller
//your data is in your controller now
}
希望对你有所帮助。
更多关于委托here的信息