written by http://recursivesteps.net/blog/2014/12/17/uialertcontroller/
UIAlertController
As a programmer, I really like the idea of UIAlertView and UIActionSheet. This is simply because they don’t mess with the view hierachy. So whenever I have an app that’s not conforming to the view life-cycle, I tend to use these instead of views on their own. Sadly they are limited. By default you can’t have more than two textfields added in an UIAlertView. Of course you could hack them in there, as is suggested on Stack Overflow multiple times, but you never know if Apple won’t reject your app because you messed with their view hierachy.
I really liked this version, but I don’t want to test Apples submission process if I don’t have to:
-(IBAction)showAlertView{UIAlertView*alert;UITextField*callForwardNumber;UItextField*callForwardCondition;alert=[[UIAlertViewalloc]initWithTitle:@"Enter Phone Number & Rule"message:@""delegate:selfcancelButtonTitle:@"Cancel"otherButtonTitles:@"Save",nil];//alert.transform = CGAffineTransformMakeTranslation(0, 110);callForwardNumber=[[UITextFieldalloc]init];callForwardNumber.keyboardType=UIKeyboardTypeNumberPad;callForwardNumber.text=[R.prefsobjectForKey:@"gtalkpbx_fwd_number"];callForwardNumber.borderStyle=UITextBorderStyleRoundedRect;callForwardNumber.delegate=self;callForwardNumber.tag=1;callForwardCondition=[[UITextFieldalloc]init];callForwardCondition.text=callCondition;callForwardCondition.borderStyle=UITextBorderStyleRoundedRect;callForwardCondition.delegate=self;callForwardCondition.tag=2;[callForwardConditionsetKeyboardType:UIKeyboardTypeNumberPad];if(floor(NSFoundationVersionNumber)>NSFoundationVersionNumber_iOS_6_1){UIView*customAccessory=[[UIViewalloc]initWithFrame:CGRectMake(0,0,250,55)];callForwardNumber.frame=CGRectMake(0,0,245.0,25.0);callForwardCondition.frame=CGRectMake(0,30.0,245.0,25.0);[customAccessoryaddSubview:callForwardNumber];[customAccessoryaddSubview:callForwardCondition];[alertsetValue:customAccessoryforKey:@"accessoryView"];[alertshow];}else{alert.message=@"\n\n\n";[alertshow];callForwardNumber.frame=CGRectMake(20.0,45.0,245.0,25.0);callForwardCondition.frame=CGRectMake(20.0,75.0,245.0,25.0);[alertaddSubview:callForwardNumber];[alertaddSubview:callForwardCondition];}}
But with iOS 8 Apple was merciful and replaced both with the UIAlertController. Thus UIAlertView and UIActionSheet are deprecated, even though XCode doesn’t alert you about it. And you still need it, if you want to support iOS 7.
So without further ado, here is what you do from now on…
UIAlertView vs UIAlertController
UIAlertView
1234567891011
-(IBAction)showAlertView:(id)sender{UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"AlertView"message:@"shown by UIAlertView"delegate:selfcancelButtonTitle:@"Cancel"otherButtonTitles:@"OK",nil];[alertshow];}// Delegate-(void)alertView:(UIAlertView*)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex{}
UIAlertController
1234567891011121314
-(IBAction)showAlertController:(id)sender{UIAlertController*alert=[UIAlertControlleralertControllerWithTitle:@"AlertView"message:@"shown by UIAlertController"preferredStyle:UIAlertControllerStyleAlert];UIAlertAction*doCancelAction=[UIAlertActionactionWithTitle:@"Cancel"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*action){}];UIAlertAction*doOKAction=[UIAlertActionactionWithTitle:@"OK"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action){}];[alertaddAction:doCancelAction];[alertaddAction:doOKAction];[selfpresentViewController:alertanimated:YEScompletion:nil];}
So what is the difference? First of all, we don’t need delegates anymore. Depending on your preference, this is a good or a bad thing. Instead, we write every action as a block. The second interesting thing is that we can just add as many Actions and Textfields as we want. UIAlertController is not defined by its limited options, as UIAlertView was.
As you can see, there is not such a huge difference to creating an Alert. Once again, we use blocks instead of delegates and define the type of action when we create an action.
Summary
UIAlertController replaces the deprecated classes UIAlertView and UIActionSheet. It also replaces delegates with blocks (Objective-C) / closures (Swift), thus getting rid of this if (buttonTitle == @"ButtonTitle") tests. The steps to create an UIAlertController are these: 1. Create an instance of UIAlertController and set its preferredStyle: 2. Add actions in blocks/closures 3. If necessary add textfields 4. Present
P.S. Be careful. Adding a textfield to an ActionSheet results in an exception.