开发者

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. 开发者_Python百科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;
    }

}


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.


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

Isn't it always something simple?


I think you simply want to toggle the "selected" property of the UIControl. When "selected" is YES is when the image for UIControlStateSelected will be used...

The typical iOS equivalent of a checkbox is the UISwitch, with it's "on" property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜