【问题标题】:Custom UIButton(CheckBox) add responder to action iOS SDK自定义 UIButton(CheckBox) 添加响应者到动作 iOS SDK
【发布时间】:2011-07-29 20:48:28
【问题描述】:

我有自定义 UIButton 类:

CheckBox.h
@interface CheckBox : UIButton {

BOOL isChecked;
IBOutlet UIWebView *webview;
IBOutlet UIImageView *img;
NSMutableString *labelText;
NSInteger fontSize;
NSInteger heightWebView;
}


@property (nonatomic,retain) NSMutableString *labelText;
@property (nonatomic,retain) UIImageView *img;
@property (nonatomic,retain) UIWebView *webview;
@property (nonatomic,assign) BOOL isChecked;
-(IBAction) checkBoxClicked;
-(void)addText:(NSString *) text redLetter:(NSInteger)redLetter isBold:(NSInteger)
     isBold;
-(BOOL)getStatus;
-(NSString*)getText;

-(void)setFontSize:(NSInteger)setFontSizeValue;


@end

CheckBox.m 看看 IBAction 我需要在那里实现功能

#import "CheckBox.h"


@implementation CheckBox

@synthesize isChecked, webview, img, labelText, delegate;


- (id)initWithFrame:(CGRect)frame {
                  if (self == [super initWithFrame:frame]) {
                                 // Initialization code
                      fontSize = 2;
                      self.isChecked = NO;
                      self.labelText = [[NSMutableString alloc] init];
                                 self.contentHorizontalAlignment =
                                            UIControlContentHorizontalAlignmentLeft;
                      img = [[UIImageView alloc] initWithFrame:CGRectZero];
                      img.image = [UIImage imageNamed:@"checkbox.png"];
                      [self addSubview:img];
                      webview = [[UIWebView alloc] initWithFrame:frame];
                      webview.backgroundColor = [UIColor clearColor]; 
                      [webview setOpaque:NO];
                      webview.userInteractionEnabled = NO;
                      [self addSubview:webview];                          

                         /*        [self setImage:[UIImage imageNamed:
                                                                                      @"checkbox.png"]
                                                                         forState:UIControlStateNormal];*/

                                  [self addTarget:self action:
                                                                         @selector(checkBoxClicked)
                                                                  forControlEvents:UIControlEventTouchUpInside];
                        }
                    return self;
    }


-(IBAction) checkBoxClicked{
                   if(self.isChecked ==NO){
                                self.isChecked =YES;
                               img.image = [UIImage imageNamed:@"checkbox-checked.png"];
                    }else{
                                self.isChecked =NO;
                                img.image = [UIImage imageNamed:@"checkbox.png"];
                    }

}


-(BOOL)getStatus{
return self.isChecked;
}

-(NSString*)getText{
return [NSString stringWithFormat:@"%@",self.labelText];
}

-(void)setFontSize:(NSInteger)setFontSizeValue {
fontSize = setFontSizeValue;
if (fontSize >2) {
    heightWebView = fontSize+2;
}
}

-(void)addText:(NSString *) text redLetter:(NSInteger)redLetter isBold:(NSInteger)isBold 
{
[self.labelText setString:text];
if (redLetter != 0) {
    NSString *first;
    NSString *red;
    NSString *second;
    first = [text substringWithRange:NSMakeRange(0, redLetter-1)];
    red = [text substringWithRange:NSMakeRange(redLetter-1, 1)];
    second = [text substringWithRange:NSMakeRange(redLetter, [text length] - redLetter )];

    if(isBold == 0) {
        NSString *html = [NSString stringWithFormat:@"<font size=\"%d\"><p>%@<span style=\"color:red;\">%@</span>%@</p></font>",fontSize, first,red,second];
        [webview loadHTMLString:html baseURL:nil];
    }else{
        NSString *html = [NSString stringWithFormat:@"<font size=\"%d\"><p>%@<span style=\"color:red;\">%@</span>%@</p></font>",fontSize, first,red,second];
        [webview loadHTMLString:html baseURL:nil];
    }

}else {

    if(isBold == 0) {
         NSString *html = [NSString stringWithFormat:@"<font size=\"%d\"><p>%@</p></font>",fontSize, text];
         [webview loadHTMLString:html baseURL:nil];
    }else{
         NSString *html = [NSString stringWithFormat:@"<font size=\"%d\"><p>%@</p></font>",fontSize, text];
         [webview loadHTMLString:html baseURL:nil];           
    }

}

}


- (void)layoutSubviews {
img.frame = CGRectMake(0, 5, 18 , 18);
webview.frame = CGRectMake(12, 0-heightWebView, self.bounds.size.width- 11 , 25+heightWebView);
}



- (void)dealloc {
    [webview release];
    [img release];
                 [super dealloc];
    }



@end

我需要向此类添加功能,当用户单击我实现 CheckBox 类的类中的按钮时,将调用一些 void。 让我更好地解释一下,我想在这里实现功能,例如在 UIAlertView 中单击按钮调用

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
I need something like
- (void)checkBox:(CheckBox *)checkBox didStatusChanged:(BOOL)checkBoxStatus

【问题讨论】:

    标签: ios delegates uibutton programmatically-created


    【解决方案1】:

    听起来你想实现一个委托协议。这将位于您的@interface 上方的 checkbox.h 顶部

    @protocol CheckBoxDelegate
    @optional
    - (void)checkBox:(CheckBox *)checkBox didStatusChanged:(BOOL)checkBoxStatus;
    @end
    

    然后你想把它添加到你的 checkbox.h @interface

    @property (monatomic, assign) NSObject <CheckBoxDelegate> delegate;
    

    然后你可以实现

    checkBox:(CheckBox *)checkBox didStatusChanged:(BOOL)checkBoxStatus

    ViewController 中的函数或创建复选框的任何东西,并为每个复选框执行

    [checkbox setDelegate:self];

    然后在-(IBAction) checkBoxClicked里面可以调用

    [delegate checkBox:self didStatusChanged:self.isChecked];
    

    这将在生成复选框/委托的类上调用该方法。

    希望这足够广泛。

    提姆

    【讨论】:

    • 当然,您需要像使用任何其他委托协议一样将 添加到您的生成类中。
    • 是的,谢谢回复,我已经用协议解决了,我有问题,因为我忘了设置“[checkbox setDelegate:self];”委托给自己。但是您的回复完全正确,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2017-07-08
    • 1970-01-01
    相关资源
    最近更新 更多