Iphone 如何在UIAlertView中创建自定义UIButton

Iphone 如何在UIAlertView中创建自定义UIButton,iphone,objective-c,cocoa-touch,uibutton,uialertview,Iphone,Objective C,Cocoa Touch,Uibutton,Uialertview,因此,我试图在UIAlertView中嵌入一个toggleable(这是一个词吗?)按钮“不再显示此内容”,但我遇到了一些我似乎无法回避的问题 这是迄今为止我的非工作代码 编辑:我添加了按钮的方法,并对原始代码进行了一些更改。现在我按下按钮对按下按钮做出反应,但结果是崩溃。有什么帮助吗 if (![[NSUserDefaults standardUserDefaults] objectForKey:@"disclaimer"]){ UIAlertView *alert = [[UIAlert

因此,我试图在UIAlertView中嵌入一个toggleable(这是一个词吗?)按钮“不再显示此内容”,但我遇到了一些我似乎无法回避的问题

这是迄今为止我的非工作代码

编辑:我添加了按钮的方法,并对原始代码进行了一些更改。现在我按下按钮对按下按钮做出反应,但结果是崩溃。有什么帮助吗

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

}

如果要将此按钮设置为切换,则将标记分配给此按钮,然后在其事件函数中检查标记是否为0,然后将其设置为1,并在那里设置布尔值。当您将其添加到警报时,请检查布尔值(如果为真),同时更改按钮图像和标签。

我认为您只需切换UIControl的“选定”属性。当“selected”为是时,将使用uicontrol状态选定的图像


典型的iOS复选框等价物是UISwitch,它的属性为“on”。

DUH!我的代码有效。只需删除@selector中的:即可(alertCheckboxButtonClicked:)


这不是很简单吗?

如何更改按钮的状态?在我的代码中,如果我点击它,什么也不会发生,也就是说,我看不到不同的PNG,我只看到相同的controlstatenormal图像。我知道如何使用IB实现这一点,但我不知道如何以编程方式更改按钮状态。让我问一下,有没有更好的方法在启动时显示免责声明,最好不要再次显示?(在更新、重新安装或类似的情况下,我猜这会抹去用户默认设置)操作表或其他功能是更被接受的方式吗?我是不是浪费了很多时间试图弄清楚这个按钮的东西,结果却发现它看起来很古怪,而我的用户都说“艾达就是这样做的…”?