UIAlertController introduced in iOS 8 .UIAlertController replaces the UIActionSheet and UIAlertView classes. UIAlertController can be used for displaying alerts ,confirmationalert ,act as action sheet and used for login purpose etc. If you want to add button to UIAlertController there is a class named UIAlertAction .
UIAlertAction provides an action handler with block which gets called when the button get tapped. Here we are using UIAlertController for login purpose below is the sample of UIAlertController -
UIAlertController *alertView = [UIAlertController alertControllerWithTitle: @"User Login"
message: @"Input username and password"
preferredStyle:UIAlertControllerStyleAlert];
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"email"; // email as username in my case
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"password";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.secureTextEntry = YES;
}];
[alertView addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSArray * textfields = alertView.textFields;
UITextField * userNameField = textfields[0];
UITextField * passwordFiled = textfields[1];
NSLog(@"email=%@ pasword=%@",userNameField.text,passwordFiled.text);
/* NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Employee"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"email==%@ AND password==%@",userNameField.text,passwordFiled.text]];
NSArray *arrayList = [[appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
if(arrayList.count>0){
[self performSegueWithIdentifier:@"showUsers" sender:self];
}
else{
NSLog(@"login failed");
} */
}]];
[alertView addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
[alertView dismissViewControllerAnimated:YES completion:nil];
}]];
[self presentViewController:alertView animated:YES completion:nil];
Following is the screen shot of UIAlertController -
Leave Comment