1

I have custom UIButton class:

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 look on IBAction i need implement functionality there

#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

I need to add functionality to this class, that when user click on button in class where i implement CheckBox class will call some void. Let me explain better i want to implement here functionality like in UIAlertView where you click on button calls

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
I need something like
- (void)checkBox:(CheckBox *)checkBox didStatusChanged:(BOOL)checkBoxStatus
tshepang
  • 12,111
  • 21
  • 91
  • 136
itworksinua
  • 127
  • 2
  • 14

1 Answers1

1

Sounds like you want to implement a delegate protocol. This would go at the top of checkbox.h above your @interface

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

You'd then want to add this to your checkbox.h @interface

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

You could then implement the

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

function in your ViewController or whatever is creating the checkboxes, and for each checkbox do

[checkbox setDelegate:self];

Then inside -(IBAction) checkBoxClicked you can call

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

and this would call that method on the class spawning the checkboxes/delegate.

Hope this is extensive enough.

Tim

Tim Davies
  • 824
  • 5
  • 17
  • of course you'll want to add to you spawning class as you would with any other delegate protocol. – Tim Davies Jul 29 '11 at 22:06
  • Yes, thx for reply, i already solve it with protocols, i have problem with it, because i forgot to set "[checkbox setDelegate:self];" delegate to self. But your reply is totally correct, thx again ! – itworksinua Jul 29 '11 at 22:48