How to create a custom UIButton inside a UIAlertView

So, I am trying to embed in the UIAlertView a shuffled (is this a word?) Button "Do not show it again" inside, but I ran into some problems that, in my opinion, cannot get around.

Here is my broken code so far ...

EDITED: I added a button method and made some changes to the source code. Now I press the button to respond to the press, but the result is a failure. 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 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; } } 
+7
source share
3 answers

If you want to make this button as a switch, then assign a tag to this button, then in your event function check the tag if it is 0, then set it to 1, and also set BOOL there. When you add it to alert, then check the BOOL value, if it is true, change the button image and tag.

+1
source

SPIRIT! My code is working. It just needed to be deleted: in @selector (alertCheckboxButtonClicked :)

Isn't that always easy?

+1
source

I think you just want to toggle the β€œselected” property of UIControl. When "selected" is YES when the image is used for UIControlStateSelected ...

A typical iOS equivalent of a flag is UISwitch, with the property "on" with it.

0
source

All Articles