Thursday, January 10, 2013

Creating UIAlertView and UIActionSheet Programmatically

For Beginner's, you may get a doubts like  this
1.what is the difference between UIAlertView and UIActionSheet?
2. In which scenarios we will use these controls?
Now i am going to clarify your doubts

UIAlertView

  •  Alerts give users important information that affects their use of the application (or the device). Alerts are usually unexpected, because they generally tell users about a problem or a change in the current situation that might require users to take action.
EXAMPLE:
      Giving alert when your Mobile Battery is low,
      we can  also design Login form using alert View


Creating AlertView programmatically
UIAlertView *alert = [[UIAlertView alloc]
   initWithTitle: @"AlertView"
   message: @"Welcome To IOS Controls!"
   delegate: nil
   cancelButtonTitle:@"OK"
   otherButtonTitles:nil];
[alert show];

The above code  pop an alert with a title, message and OK 


button like this


NOTE:
If you want  to know  the properties and methods that are present in the Class Or Control, put the cursor on the Class and command+click.You will get that class properties and Methods.

But  if you want to have more than one button on the alert 

and collect user input from it? . To do that, you first need to 

create a class that employs the UIAlertViewDelegate delegate 

like this:

@interface PresentAlert : NSObject <UIAlertViewDelegate>

Then, in PresentAlert.m file, viewDidLoad Method create 

alertView  like this
UIAlertView *alertView = [[UIAlertView alloc]
   initWithTitle: @"AlertView"
   message: @"Welcome To IOS Controls!"
   delegate: self
   cancelButtonTitle:@"Cancel"
   otherButtonTitles:@"Submit",nil];
[alert show];

 you need to override the  alertView  method in order to 

receive input like this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
 if (buttonIndex == 0) {
  NSLog(@"Cancel Pressed");
 }
 else  if (buttonIndex==1){
  NSLog(@"Submit Pressed");
 }
}

 Different styles of alertView's are

a.alertView.alertViewStyle = UIAlertViewStyleSecureTextInput.

It pop an alert with textfield,the entered text will be in secure format

b. alertView.alertViewStyle = UIAlertViewStylePlainTextInput.

It pop an alert with textfield,the entered text will be plain format.

c.alertView.alertViewStyle= UIAlertViewStyleLoginAndPasswordInput.

It pop an alert with two textfield's one for user name and another one for pasword.

---------------------------------------------------------------------------------

UIActionSheet

  • Action sheets give users additional choices related to the action they are currently taking. 

EXAMPLE:
   If you want to delete the recent call list from your mobile,
action sheet will give options like
           -->Delete Dialed Calls.
           -->Delete Received Calls.
           -->Delete Missed Calls.
           -->Cancel 
Creating UIActionSheet Programmatically











No comments:

Post a Comment