gpt4 book ai didi

iphone - 如何在 UIAlertView 中创建自定义 UIButton

转载 作者:太空狗 更新时间:2023-10-30 03:38:25 26 4
gpt4 key购买 nike

所以我试图在 UIAlertView 中嵌入一个可切换的(这是一个词吗?)“不要再显示这个”按钮,但我遇到了一些我似乎无法解决的问题。

到目前为止,这是我的非工作代码......

已编辑:我添加了按钮的方法,并对原始代码进行了一些更改。现在我得到了对媒体使用react的按钮,但结果是崩溃。有帮助吗?

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 并在那里设置一个 BOOL。当您将其添加到警报时,然后检查 BOOL 值是否为真,同时更改您的按钮图像和标签。

关于iphone - 如何在 UIAlertView 中创建自定义 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5670432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com