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!
Can anyone help me?
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;
}
}
来源:https://stackoverflow.com/questions/22727842/how-to-add-checkbox-into-uialertview