ВЫПУСК TableView CustomCell UIButton - PullRequest
1 голос
/ 18 апреля 2011

Я использую customCell для моего tableView. CustomCell содержит UIButton. И я хочу назначить действие с помощью этой кнопки.

BeginingCell.h содержит склонение для кнопки UIB как:

#import <UIKit/UIKit.h>
@interface BeginingCell : UITableViewCell {
    IBOutlet UIButton *ansBtn1; 
}

@property(nonatomic,retain)IBOutlet UIButton *ansBtn1;

@end

И реализация:

//
//  BeginingCell.m
//  customCells
//

#import "BeginingCell.h"    

@implementation BeginingCell

///########### Synthesize ///###########

@synthesize ansBtn1;

///########### Factory ///###########

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state.
}

///########### End Factory ///###########

- (void)dealloc {
    [super dealloc];
}
@end

Я использую эту CustomCell в tableView. Я хочу получить ответ от кнопки Click. Как я могу назначить действие кнопки для моей кнопки CustomCell?

Ответы [ 3 ]

1 голос
/ 18 апреля 2011
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self
             action:@selector(myButtonAction:)
   forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:@"Action" forState:UIControlStateNormal];
myButton.frame = CGRectMake(buttonFrame);
[self addSubview:myButton];
0 голосов
/ 18 апреля 2011

Вы можете программно добавить действие к UIButton, используя метод addTarget:action:forControlEvents: (метод UIControl, UIButton является подклассом).

Итак, ваша реализация будет выглядеть примерно так:

[ansBtn1 addTarget:self
             action:@selector(btnAction)
   forControlEvents:UIControlEventTouchUpInside];


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

          [ansBtn1 addTarget:self
             action:@selector(btnAction:)
          forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
//Buttons Action
-(void) btnAction:(id) sender
{

}
0 голосов
/ 18 апреля 2011

Вместо того, чтобы делать вашу кнопку IBOutlet и пытаться переписать ее на IBAction, просто подключите ее вручную -(void)viewDidLoad примерно так:

[ansBtn1 addTarget:self action: @selector(myActionMethod:) 
        forControlEvents:UIControlEventTouchUpInside];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...