Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom UIButton inside UIAlertView

So I'm trying to embed a toggleable (is that a word?) "Do not show this again" button inside a UIAlertView, but I'm running into some trouble that I can't seem to get around.

Here is my non-working code thus far...

EDITED: I added the button's method, and made some changes in the original code. Now I get the button to react to the press but the result is a crash. Any help?

if (![[NSUserDefaults standardUserDefaults] objectForKey:@"disclaimer"]){


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DISCLAIMER" 
                                                message:@"This program is a blah blah"
                                               delegate:self 
                                      cancelButtonTitle:nil
                                      otherButtonTitles:@"I Agree", nil];

UILabel *alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 230, 260, 50)];
alertLabel.backgroundColor = [UIColor clearColor];
alertLabel.textColor = [UIColor whiteColor];
alertLabel.text = @"Do not show again";
[alert addSubview:alertLabel];
[alertLabel release];

//declared alertCheckboxButton in the header due to errors I was getting when referring to the button in the button's method below
alertCheckboxButton = [UIButton buttonWithType:UIButtonTypeCustom];
alertCheckboxButton.frame = CGRectMake(200, 247, 16, 16);
alertCheckboxButton.backgroundColor = [UIColor clearColor];
UIImage *alertButtonImageNormal = [UIImage imageNamed:@"checkbox.png"];
UIImage *alertButtonImagePressed = [UIImage imageNamed:@"checkbox-pressed.png"];
UIImage *alertButtonImageChecked = [UIImage imageNamed:@"checkbox-checked.png"];
[alertCheckboxButton setImage:alertButtonImageNormal forState:UIControlStateNormal];
[alertCheckboxButton setImage:alertButtonImagePressed forState:UIControlStateHighlighted];
[alertCheckboxButton setImage:alertButtonImageChecked forState:UIControlStateSelected];
[alertCheckboxButton addTarget:self action:@selector(alertCheckboxButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

//made the button a subview of alert rather than alertLabel
[alert addSubview:alertCheckboxButton];  
[alert show];

//moved alertCheckboxButton release to (void)dealloc
[alert release];    

}


-(void)alertCheckboxButtonClicked{

if (![[NSUserDefaults standardUserDefaults] objectForKey:@"disclaimer"]){

    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"disclaimer"];
    alertCheckboxButton.selected = YES;
}else {

    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"disclaimer"];
    alertCheckboxButton.selected = NO;
    }

}
like image 782
Sam Avatar asked Apr 14 '11 22:04

Sam


2 Answers

If you want to make this button as toggle then assign a tag to this button, then in its event function check for tag if it is 0 then set it to 1 and also set a BOOL there. When you are adding it to alert then check for the BOOL value if that is true change you button image and tag also.

like image 62
saadnib Avatar answered Nov 05 '22 04:11

saadnib


DUH! My code works. Just had to delete the : in @selector(alertCheckboxButtonClicked:)

Isn't it always something simple?

like image 1
Sam Avatar answered Nov 05 '22 04:11

Sam