Как создать пользовательский UIButton внутри UIAlertView

Итак, я пытаюсь встроить переключаемую (это слово?) Кнопку «Больше не показывать» в UIAlertView, но у меня возникли некоторые проблемы, которые я Можно'

Вот мой пока нерабочий код ...

РЕДАКТИРОВАТЬ: Я добавил метод кнопки и внес некоторые изменения в исходный код. Теперь я получаю кнопку, реагирующую на нажатие, но в результате вылетает. Любая помощь?

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;
    }

}
7
задан Sam 17 April 2011 в 01:59
поделиться