Wednesday, 1 October 2014

How to use UIAlertController instead of UIAlertView & UIActionsheet in iOS8


In iOS8 UIAlertController replaces UIActionsheet and UIAlertView. An AlertController can be created with a "title" and a "message".

We can set two styles to an AlertController. Which is given below.
(a)UIAlertControllerStyleActionSheet -  for configuring UIAlertController as an ActionSheet
(b)UIAlertControllerStyleAlert             -  for configuring UIAlertController as an Alert.

How to create an Alert/ActionSheet
  • Creating an AlertController object


  • Adding an Action

      
The handler block can be used to get the call back when “ok” button is pressed.
  • Presenting the alertController

How to create a simple Alert with OK button and a message

The following method will create a simple Alert in iOS8.

The Alert will be looking like as given below.




How to create a an Alert with two buttons

The Alert will be looking like as given below.





How to create an Alert with a TextField


The Alert will be looking like as given below.




How to create an Alert with two textfields

The Alert will be looking like as given below.




How to create an Actionsheet with two buttons

For creating an ActionSheet use "PreferredStyle" as "UIAlertControllerStyleActionSheet".


The ActionSheet will be looking like as given below.