Search code examples
iphoneuialertview

how to add checkbox into uialertview?


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?


Solution

  • 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.