how to add checkbox into uialertview?

不想你离开。 提交于 2019-12-02 00:57:25

try this code for add checkbox in alertview.

 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.

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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!