[![CI Status](http://img.shields.io/travis/Daria Kopaliani/DAAlertController.svg?style=flat)](https://travis-ci.org/Daria Kopaliani/DAAlertController)
If you are a luckier developer and only target iOS 8+ devices, enjoy the new UIAlertController
, otherwise check DAAlertController
out, it’s a real timesaver.
DAAlertController
provides a convenient block-based interface for configuration and presentation of UIAlertView
s and UIActionSheet
s. If UIAlertContoller
is available DAAlertController
will just pass all the work to it, otherwise it will use associated references (explained in detail later) to invoke action handlers when buttons are clicked just like UIAlertController
would.
Naturally, DAAlertController
is limited to what the old UIAlertView
and UIActionSheet
could do:
To run the example project, clone the repo, and run pod install
from the Example directory first.
Let’s take a look at the case of a simple alert view:
DAAlertAction *cancelAction = [DAAlertAction actionWithTitle:@"Cancel"
style:DAAlertActionStyleCancel handler:nil];
DAAlertAction *signOutAction = [DAAlertAction actionWithTitle:@"Sign out"
style:DAAlertActionStyleDestructive handler:^{
// perform sign out
}];
[DAAlertController showAlertViewInViewController:self
withTitle:@"Are you sure you want to sign out?"
message:@"If you sign out of your account all photos will be removed from this iphone."
actions:@[cancelAction, signOutAction]];
Here is what you will get for iOS 8 and 7: (UIAlertView
does not support destructive buttons so "Sign out" button will be rendered as a default button)
iOS 8+ | iOS 7 |
---|---|
Presenting an alert view with 2 textfields ("sign up" button should only be enabled when "nickname" is at least 5 letters long) would look like
DAAlertAction *cancelAction = [DAAlertAction actionWithTitle:@"Cancel" style:DAAlertActionStyleCancel handler:nil];
DAAlertAction *signUpAction = [DAAlertAction actionWithTitle:@"Sign up" style:DAAlertActionStyleDefault handler:^{
// perform sign up
}];
[DAAlertController showAlertViewInViewController:self
withTitle:@"Sign up"
message:@"Please choose a nick name."
actions:@[cancelAction, signUpAction]
numberOfTextFields:2
textFieldsConfigurationHandler:^(NSArray *textFields)
{
UITextField *nickNameTextField = [textFields firstObject];
nickNameTextField.placeholder = @"Nick name";
UITextField *fullNameTextField = [textFields lastObject];
fullNameTextField.placeholder = @"Full name";
} validationBlock:^BOOL(NSArray *textFields) {
UITextField *nickNameTextField = [textFields firstObject];
return nickNameTextField.text.length >= 5;
}];
iOS 8+ | iOS 7 |
---|---|
Methods for presenting action sheets are quite similar, but they also include parameters for sourceView
or barButttonItem
and permittedArrowDirections
.
iOS 8+ | iOS 7 |
---|---|
DAAlertController is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "DAAlertController"
DAAlertController is available under the MIT license. See the LICENSE file for more info.