how to add checkbox into uialertview?

后端 未结 2 929
星月不相逢
星月不相逢 2021-01-21 11:28

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.

相关标签:
2条回答
  • 2021-01-21 11:47

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-21 11:57

    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.

    0 讨论(0)
提交回复
热议问题