简体   繁体   中英

Adding superview method as target to UIButton of child view from superview

I am developing an application in which I am having common structure of UITableView for all my UIViewControllers . So I have created a UITableView subclass containing common structure, then I have added this view to my UIViewController .

UITableView is displaying properly but I need to addTarget to UIButton added to Custom UITableViewCell from my ViewController and method is written in UIViewController .

Can anyone please tell me how to achieve this?

You need to work on your Program design to get this problem sorted. However you have come far enough to go back & change your design. So for you the better option is the NSNotificationCenter .

For your case, do this:

  1. In your viewController where your method is written, lets say method name is myX-Method: (which performs the button's target functionality), then in the viewDidLoad of this controller just add this line.

     -(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. Now, in your UITableViewCell class, set a local method as buttons target as normally we do, lets say , myLocalMethod:

  3. Now in this local method, post the notification message like this. For example I am just passing the current cell's button text as message to notification.

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

This way you need not have to be dependent on delegate or so. Use notification object to collect your values in myX-Method: like this :

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

Hope this simple solution will solve your purpose.

Refer here for a sample code. However in example code project, kindly search NSNotificationCenter

you can set the cell delegate as the view controller at tableView:cellForRowAtIndexPath:

then when a button is pressed inside the cell you can trigger the delegate method inside the buttonPressed: method in the cell.

so the cell calls cell.delegate which is the viewController

in CellView.h

@property (nonatomic, weak) id delegate;

in CellView.m

- (void)buttonPressed:(id)sender {
    [self.delegate cellButttonPressed:sender];
}

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

Use Delegate Methods For Example

Make SubClass of UITableViewCell

Declare Delegate Methods

  @protocol  CustomCellDelegate <NSObject>

-(void)btnClikced;

@end

// InterFace

@interface CustomCell : UITableViewCell

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

//implementation

- (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];
}

in your 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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