Ios 将superview方法作为目标添加到superview子视图的UIButton
我正在开发一个应用程序,在该应用程序中,我的所有Ios 将superview方法作为目标添加到superview子视图的UIButton,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在开发一个应用程序,在该应用程序中,我的所有UIViewControllers都具有通用的UITableView结构。 因此,我创建了一个包含公共结构的UITableView子类,然后将该视图添加到我的UIViewController中 UITableView显示正确,但我需要将目标添加到UIButton从我的ViewController添加到自定义UITableViewCell,方法在UIViewController中写入 有人能告诉我如何实现这一点吗?您可以在tableView:cell
UIViewControllers
都具有通用的UITableView
结构。
因此,我创建了一个包含公共结构的UITableView
子类,然后将该视图添加到我的UIViewController
中
UITableView
显示正确,但我需要将目标添加到UIButton
从我的ViewController
添加到自定义UITableViewCell
,方法在UIViewController
中写入
有人能告诉我如何实现这一点吗?您可以在tableView:cellForRowAtIndexPath: 然后,当在单元格中按下按钮时,可以在单元格中的buttonPressed:方法中触发委托方法 因此,单元格调用作为viewController的cell.delegate
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.delegate = self;
return cell;
在CellView.h中
@property (nonatomic, weak) id delegate;
在CellView.m中
- (void)buttonPressed:(id)sender {
[self.delegate cellButttonPressed:sender];
}
在MyTableViewController.m中
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CellView *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.delegate = self;
return cell;
}
- (void)cellButttonPressed:(id)sender {
//button action code here
}
您需要进行程序设计,以解决此问题。然而,你已经走得够远了,可以回去改变你的设计。因此,对您来说,更好的选择是
NSNotificationCenter
对于您的情况,请执行以下操作:
myX method:
(执行按钮的目标功能),然后在此控制器的viewDidLoad
中添加这一行
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myX-Method:) name:@"NOTIFY_CUSTOM_BUTTON" object:nil];
}
UITableViewCell
类中,像通常一样将一个本地方法设置为按钮目标,比如说,myLocalMethod:
-(void) myLocalMethod:(UIButton *)button{
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFY_CUSTOM_BUTTON" object: button.title];
}
myX方法:
中使用通知对象收集您的值,如下所示:
-(void)myX-Method:(NSNotification *)dict {
NSString *buttonTitle = [dict valueForKey:@"object"];
NSLog(@"Button Clicked : %@", buttonTitle);
}
希望这个简单的解决方案能解决你的问题
请参阅示例代码。然而,在示例代码项目中,请搜索
NSNotificationCenter
例如使用委托方法
生成UITableViewCell的子类
声明委托方法
@protocol CustomCellDelegate <NSObject>
-(void)btnClikced;
@end
在ViewController中
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.delegate = self;
return cell;
}
您使用的是故事板还是旧系统?如果是情节提要,这可能会有很大帮助@JoeBlow:是的,我正在使用Storyboard@JoeBlow我尝试使用container view controller来完成此操作,但这不符合我的要求,因为在
ui按钮上单击“我的桌子高度为”changing@ShitalTiwari:请按照我的回答选择通知中心。这里有一个示例代码链接,解释了NSNotificationCenter
在自定义子类中的用法。您能详细说明一下吗?
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.delegate = self;
return cell;
- (void)btnClikced{
//button action code here
}