Monday, 13 July 2015

Other Button Title in Alert View IOS

OtherButtonTitle in UIAlertView IOS


Learn Objective C from here:Click Here

Learn Swift from Here:Click Here

Learn Initializers in Objective C:Click Here


Learn Inheritance in Swift:Click here

Learn IOS Application Development From here:Click Here

Learn Attractive Table View in IOS:Click Here.

Learn Accessory Type in IOS:Click Here

As you have studied my last tutorial of Attractive Table View in IOS if you have not read that tutorial: Click Here

Due to High Demand of One Visitor on my blog Today i am Published a post on Delegate in AlertView.







Let we start with UIAlertView.You have seen in last tutorials very heavily i used UIAlertView.
It is the Popup View appear above the main container or view of user interface.In UIAlert View Method you have seen code like below:

 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"StudentName" message:@"ABC is Student" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];

you see in above code we have take delegate nil due to nil use of otherButtonTitles.In CancelButtonTitle i have write OK when user click on it,Popup View is automatically dismiss due to method written for cancelButtonTitle already.OtherButtonTitle is make for user defined button linked with code.Also if user want to run their code with other button he can bind it.

What is delegate?

Delegate is Object responsible for behaviour of UIAlertView.

There are two ways to addOtherButtonTitles.
One way to add Other Button Title is:

 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"StudentName" message:@"ABC is Student" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Store", nil];
    [alert show];




Why i take delegate as Self?

Self means call with same object from which alert view is called.
I already describe the tutorials on Self and Super can see from here:Click Here.

Second way to write OtherButtonTitle is:

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"StudentName" message:@"ABC is Student" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert addButtonWithTitles:@"Store"];
    [alert show];

Now you have seen i have added Store button on AlertView.But what happen when you click on it there is no code to executed it dismiss the popup view. you can also bind your code with Store Button.

There is method in Protocol UIAlertViewDelegate.If you have not studied about protocol you can: Click Here.

Add <UIAlertViewDelegate> at top of Interface in ViewController.h file.

 didDismissWithButtonIndex method is used which describe what code to execute when dismisspopupview with button index.
There is array of Indexes of button on AlertView. You can add multiple button according to your choice.
ButtonIndex=0 is used for CancelButtonTitle that is already described.
Furthor User defined Indexes start from 1 to your choice of end button.

Lets us look up about code with dismiss method:

 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
 {
     if (buttonIndex==1) {
         NSLog(@"Store Button run successfully");
     }
 }


Above method Run Message on Console when user click on the Store button.You can add code according to your choice like store value in string or something else.

There is one Othermethod

 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
     if (buttonIndex==1) {
         NSLog(@"Store Button run successfully");
     }
 }




The difference between above two methods are that clickedButtonAtIndex run when user click on Custom Button and then automatically dismiss pop up view.But didDismissWithButtonIndex runs code when popup view Dismiss.

The work of above code run same in both written methods.

Who Studied my Above tutorial carefully think that what happens if there are two Alert View in same class or more then two.The work of didDismissWithButtonIndex method is just to run code while dismissing popup not to check which alert view is executed.

Its solution is also possible.you need to add tag number with alert view.

 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"StudentName" message:@"ABC is Student" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Store", nil];
[alert setTag:100];  
[alert show];



Now Another AlertView is:


UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:@"StudentDetail" message:@"DEF is Student Father" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Store", nil];
[alert setTag:200];  
[alert show];



Now the code will be in didDismissWithButtonIndex is:



 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
 {
if([alertview tag]==100)
{
     if (buttonIndex==1) {
         NSLog(@"Store Button run successfully");
     }
 }
if([alertview tag]==200)
{
if(buttonIndex==1)
{
NSLog(@"Second Store button run successfully");
}
}


There are some readymade design for alert view.These are UIAlertViewPlainTextInput,UIAlertViewSecureTextInput,UIAlertViewLoginAndPasswordInput.

UIAlertViewPlainTextInput looks like:



UIAlertViewSecureTextInput:




UIAlertViewLoginAndPasswordTextInput:


Now problem arise how to fetch data from textfield in alertView.Its solution is also possible.

write below code:

 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"StudentName" message:@"ABC is Student" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Store", nil];
alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;  
[alert show];



 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
 {
     if (buttonIndex==1) {
          UITextField *logintext=[alertView textFieldAtIndex:0];
        UITextField *passwordtext=[alertView textFieldAtIndex:1];
        NSString *storestringlogin=logintext.text;
        NSString *storestringpassword=passwordtext.text;
        NSLog(@"Store Button run successfully:\n Login:%@ \n
     Password:%@",storestringlogin,storestringpassword);
     }
 }


Final Output will be :




There is also array of TextFields on Alert View.You can invoke with Index number which textfield to store data from it.

Above Code save Data in String and Display in Console with login and Password.

You can make Custom Alert View i will go on lator on it requires more coding.

I think it is very simple code you do not need any source code.If you need source code can ask from me by mailing,comment here.

I hope you like my tutorials.Any query can mail me or comment here.

My next tutorial will be Delete row from Table View.

4 comments:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in IOS development, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on IOS development . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete
  2. I do trust all of the concepts you’ve presented on your post. They’re really convincing and will definitely work. Still, the posts are too brief for newbies. May you please extend them a little from subsequent time?Also, I’ve shared your website in my social networks.
    Mobile App Development Company in Dubai
    Android App Development Company in Dubai

    ReplyDelete

  3. Thanks for sharing the informative stuff. i really enjoy to read this post. can you share some tutorial on POS System. I mean to say how to create POS App with the help of IOS.

    ReplyDelete
  4. Hey, such a useful piece of information you are sharing here. Since a long time, I was badly in search of this information. Thanks for sharing it, dude. Also, I would like to share the information in my blog as well, so that, my visitors will also get such wonderful information.
    Xamarin Developers in Frisco | Hire Xamarin Developer

    ReplyDelete