3

I'm new in iPhone development. I want to add a check box into an alert view. I'm doing tests on this alertview for the last two days, but do not get any working demo project. I want exactly this alertbox!

enter image description here

Can anyone help me?

5
  • UIAlertView is pretty limited. you have to use UIView, and present that modally Mar 29, 2014 at 6:46
  • use this code to create custom Alertview github.com/goncz9/GRAlertView Mar 29, 2014 at 6:48
  • Thank you for your support...but how can i add check button in this customview...?
    – Sonu
    Mar 29, 2014 at 6:52
  • Please make a custom AlertView for it... Means Use UIView Show Like alert view Mar 29, 2014 at 7:01
  • what is the problem with a more iOS-conform solution here? like: [[[UIAlertView alloc] initWithTitle:@"Information" message:@"(blah)" delegate:self cancelButtonTitle:@"Remind me again" otherButtonTitles:@"Don't show me again", nil] show];. that don't show me checkbox reminds Windows...
    – holex
    Mar 29, 2014 at 9:31

2 Answers 2

4

try this code for add checkbox in 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];

i hope this code useful for you.

8
  • is it AppStore-safe? the accessoryView is not part of the public interface of the UIAlertView, and there is good reason why the UIAlertView cannot be customized in iOS7+ freely.
    – holex
    Mar 29, 2014 at 9:11
  • i don't know this code is valid in appstore . but this code work proprely in my code . you know so please tell me Mar 29, 2014 at 9:14
  • it works, that is not a problem, but when someone'd like to submit an application with that solution, there might be rejected. that is why I've asked you about how many applications, which you published, did pass the review approval procedure with that solution? that is a very important detail to encourage or to avoid using such a solution.
    – holex
    Mar 29, 2014 at 9:21
  • ok thanks for this info . any other way for add checkbox in alertview is create custom alert ? right ? Mar 29, 2014 at 9:25
  • 1
    you know as well that Apple provides what they want to provide to developers. the reason why there is no public access of the UIAlertView content in iOS7+ is that many applications cheated to change the content of the In-App Puchase official popup, especially the price, and the end-user saw less amount what they finally paid. that is why Apple forbids to access to the content of the official UIAlertView in future.
    – holex
    Mar 29, 2014 at 9:38
0

iOS version independent

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.