如何在UIAlertview中添加复选框?

3
我是iPhone开发的新手。我想在警告视图中添加一个复选框。我已经测试了这个警告视图两天,但没有得到任何可用的演示项目。我需要完全像这个警告框!请问有人可以帮助我吗?

UIAlertView的功能相当有限。你需要使用UIView,并以模态方式呈现它。 - nivritgupta
使用此代码创建自定义AlertView https://github.com/goncz9/GRAlertView - nivritgupta
谢谢您的支持...但是我该如何在这个自定义视图中添加复选框按钮呢...? - Sonu
@Sweeta,请检查我的答案。 - Darshan Kunjadiya
请为此制作一个自定义的AlertView...意味着使用UIView显示类似于警报视图。 - Parvendra Singh
这里使用更符合iOS标准的解决方案有什么问题吗?例如:[[[UIAlertView alloc] initWithTitle:@"Information" message:@"(blah)" delegate:self cancelButtonTitle:@"Remind me again" otherButtonTitles:@"Don't show me again", nil] show];。那个“不再显示”复选框让人想起Windows... - holex
2个回答

3
尝试使用以下代码在alertview中添加复选框。 Swift版本:
let nameField = UIButton(frame: CGRect(x: 0.0, y: 0, width: 50, height: 50.0))
let v = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 40))
nameField.setImage(UIImage(named: "checkbox_off.png"), for: .normal)
v.addSubview(nameField)
var av = UIAlertView(title: "TEST", message: "subview", delegate: nil, cancelButtonTitle: "NO", otherButtonTitles: "YES")
av.setValue(v, forKey: "accessoryView")
av.show()

Objective C

UIButton *nameField = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0, 50, 50.0)];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 40)];
[nameField setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];
[v addSubview:nameField];
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[av setValue:v  forKey:@"accessoryView"];
[av show];

我希望这段代码对你有所帮助。

我不知道这段代码在App Store是否有效。但是在我的代码中,这段代码可以正常工作。如果你知道,请告诉我。 - Darshan Kunjadiya
它能够运行,那不是问题,但当有人想提交包含该解决方案的申请时,可能会被驳回。这就是我问你,你发布了多少应用程序,在审核批准程序中通过了该解决方案的数量?这是一个非常重要的细节,可以鼓励或避免使用这样的解决方案。 - holex
不幸的是,那将会更安全。 - holex
1
你也知道,苹果公司只提供他们想要提供给开发者的东西。在iOS7+中没有公共访问UIAlertView内容的原因是,许多应用程序欺骗性地更改了应用内购买官方弹出窗口的内容,特别是价格,最终用户看到的金额比实际支付的金额少。这就是为什么苹果公司禁止未来访问官方UIAlertView内容的原因。 - holex
在iOS 17中不再起作用了 - undefined
显示剩余4条评论

0

iOS版本无关

-(void) showAlert {
    isReemeber = TRUE;
    rememberButton = [[UIButton alloc] init];
    UIView *v = [[UIView alloc] init];
    [rememberButton setImage:[UIImage imageNamed:@"checkbox-checked.png"] 
                                    forState:UIControlStateNormal];
    [rememberButton setTitle:@" Don't show me again" forState:UIControlStateNormal];
    [rememberButton addTarget:self action:@selector(toggleRememberMethod) 
                     forControlEvents:UIControlEventTouchUpInside];

    [v addSubview:rememberButton];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" 
                                                    message:@"" 
                                                   delegate:self 
                                          cancelButtonTitle:@"Cancel" 
                                          otherButtonTitles:@"Yes", nil];

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {

        v.frame = CGRectMake(0, 0, 250, 40);
        rememberButton.frame = CGRectMake(0.0, 0, 250, 50.0);
        [rememberButton setTitleColor:[UIColor blackColor] 
                             forState:UIControlStateNormal];
        alert.message = @"Message here";
        [alert setValue:v  forKey:@"accessoryView"];

    } else {

        v.frame = CGRectMake(0, 80, 250, 40);
        rememberButton.frame = CGRectMake(0.0, 0, 250, 40.0);
        alert.message = @"Message here\n\n\n\n";
        [alert addSubview:v];

    }
    [alert show];

}


-(void)toggleRememberMethod {
    if (isReemeber) {
        [rememberButton setImage:[UIImage imageNamed:@"checkbox.png"] 
                        forState:UIControlStateNormal];

        isReemeber = FALSE;
    } else {
        [rememberButton setImage:[UIImage imageNamed:@"checkbox-checked.png"] 
                        forState:UIControlStateNormal];

        isReemeber = TRUE;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接