繁体   English   中英

将Superview方法作为目标添加到Superview的子视图的UIButton中

[英]Adding superview method as target to UIButton of child view from superview

我正在开发一个应用程序,其中所有UIViewControllers都具有UITableView通用结构。 因此,我创建了一个包含通用结构的UITableView子类,然后将该视图添加到了UIViewController

UITableView可以正确显示,但是我需要将TargetTarget添加到从我的ViewController添加到Custom UITableViewCell UIButton ,并且方法是在UIViewController编写的。

谁能告诉我如何实现这一目标?

您需要进行程序设计才能解决此问题。 但是,您已经走了足够远的路,可以返回并更改您的设计。 因此,对您而言,更好的选择是NSNotificationCenter

对于您的情况,请执行以下操作:

  1. 在编写方法的viewController中,假设方法名称为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]; } 
  2. 现在,在您的UITableViewCell类中,像通常一样,将本地方法设置为按钮目标,比如说myLocalMethod:

  3. 现在,在此本地方法中,像这样发布通知消息。 例如,我只是将当前单元格的按钮文本作为消息传递给通知。

     -(void) myLocalMethod:(UIButton *)button{ [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFY_CUSTOM_BUTTON" object: button.title]; } 

这样,您不必依赖于代理人左右。 使用通知对象在myX-Method:收集您的值:

-(void)myX-Method:(NSNotification *)dict {
    NSString *buttonTitle = [dict valueForKey:@"object"];
    NSLog(@"Button Clicked : %@", buttonTitle);
}

希望这个简单的解决方案能够解决您的目的。

请参考此处获取示例代码。 但是在示例代码项目中,请搜索NSNotificationCenter

您可以在tableView:cellForRowAtIndexPath处将单元委托设置为视图控制器:

那么当在单元格中按下按钮时,您可以在单元格中的buttonPressed:方法内触发委托方法。

所以单元 格调用cell.delegate ,它是viewController

在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
}

例如,使用委托方法

制作UITableViewCell的子类

声明委托方法

  @protocol  CustomCellDelegate <NSObject>

-(void)btnClikced;

@end

//接口

@interface CustomCell : UITableViewCell

@property(nonatomic,retain) id<CustomCellDelegate> delegate;

//执行

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
        [btn addTarget:self action:@selector(btnTapped) forControlEvents:UIControlEventTouchUpInside];
        self.accessoryView=btn;
    }
    return self;
}
-(void)btnTapped{
    [self.delegate btnClikced];
}

在您的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;

}

- (void)btnClikced{
//button action code here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM