I am trying to perform the smile task of having two alerts in a single View Controller. The code below works fine, but how would I make another instance of it elsewhere in the View Controller. I am concerned that if I duplicated the code, my buttonIndex would not know which alert it is responding to. Any ideas? Thanks!
-(void)alertChoice
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Confirm", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
//do something
}
}
You can use the tag property on UIAlertView to decipher which alert is which:
-(void)alertChoice
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Confirm", nil];
alert.tag = 0;
[alert show];
}
-(void)alertChoice1
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Confirm", nil];
alert1.tag = 1;
[alert1 show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 0)
{
}
}
Simply set a tag to each Alert view and check which one sent the messeg.
alertView.tag=0;
And then
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==0){
if(buttonIndex == 0)//OK button pressed
{
//do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
//do something
}
}else{
if(buttonIndex == 0)//OK button pressed
{
//do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
//do something
}
}
set tag to alert view.
alert.tag = 1;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1 && alertView.tag == 1)
{
//do something
}
}
Step 1: Add UIAlertViewDelegate in your view controller.h file
step 2: Add the following methods in your view controller.m file
-(void)AlertMethodOne
{
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:#"AlertMethodOne" message:#"AlertMethodOne successfully Called" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
alert.tag=101;
[alertview show];
}
-(void)AletrMethodTwo
{
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:#"AletrMethodTwo" message:#"AlertMethodTwo successfully Called" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
alert.tag=102;
[alertview show];
}
call the above two methods in your viewController as like below:
[self AlertMethodOne];
[self AlertMethodTwo];
Now AlertView Button Clicked Method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag==101)
{
if (buttonIndex == 0)
{
}
}
if(alertView.tag==102)
{
if (buttonIndex == 1)
{
}
}
}
Related
I searched Stack for ways to implement multiple alertViews. Most of the answers were to use Tags. This way works great, except for one huge thing- the cancel button! When the alertView pops up, whether you tap "cancel" or "yourButtonTitle", your action goes through. Is there a way to cancel an alertView with Tags?
Here is my code:
#define TAG_ONE 1
#define TAG_TWO 2
- (IBAction)someButton1 {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Call" message:#"Call number?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
alertView.tag = TAG_ONE;
[alertView show];
}
- (IBAction)someButton2 {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Log Out?" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Yes", nil];
alertView.tag = TAG_TWO;
[alertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_ONE) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:101-101-1010"]];
} else if (alertView.tag == TAG_TWO){
[PFUser logOut];
[self performSegueWithIdentifier:#"showLogin" sender:self];
}
}
you can set TAG for only the UIAlertview , but you can Identify the button using button Index
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == TAG_ONE) {
if(alertView.cancelButtonIndex == buttonIndex){
// Do cancel
}
else{
// Do the success thing
}
}
else if (alertView.tag == TAG_TWO) {
// same thing followed
}
}
buttonIndex == 0 // for OK , buttonIndex == 1 // for Cancel
additional Reference
You must add if (alertView.cancelButtonIndex == buttonIndex){
// Do cancel
} before asking if (alertView.tag == TAG_ONE) {
I am trying to see what option the client has pushed on the alert view button, though the logging doesn't appear to be working
-(IBAction)flattenBtn:(id)sender
{
UIAlertView* flatView = [[UIAlertView alloc] initWithTitle:#"Flatten Image"
message:#"Are you sure you want to flatten your image?" delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Yes",#"No",nil];
[flatView show];
[flatView release];
}
- (void)flatView:(UIAlertView *)flatView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"YES");
}
else if (buttonIndex == 1)
{
NSLog(#"NO");
}
}
Try it using like this :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Yes"])
{
//your code if YES pressed;
}
else
{
//your code if NO pressed;
}
}
I have 2 alertView in a method and I want to check the button index of the second alertView. How can I do it? I want to get the buttonIndex for the else condition. Below is the code I am trying to do.
-(IBAction)buttonClick{
NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.xyz.com"] encoding:NO error:nil];
if(connect == NULL) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"No Internet Connection" message:#"Please check your internet connection" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
} else {
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:#"Upload to the server?" message:#"" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"CANCEL",nil];
[alert1 show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
My functionality
}
One way to do this is having the alert view's instances global and checking those in the alertview's delegate method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// assuming the myAlert2 is your alert's instance
if (alertView == myAlert2)
{
if (buttonIndex == 0)
{
...
}
}
}
Or you can just give tags to your alert and then checking the tags in the alertView's delegate method, like:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 2)
{
if (buttonIndex == 0)
{
...
}
}
}
You can use tag for alert view
eg :
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:#"hai" delegate:self cancelButtonTitle:#"ok" otherButtonTitles: nil];
alert.tag = 1;
[alert show];
then you can check like below
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1)
{
if (buttonIndex == 0)
{
...
}
}
}
I have this UIAlertview with tags that I am trying to use to call a function. The fogotpassword alert view shows up then the reset alertview shows up, but then when I am trying to call the NSLog(#"Password"); function by pressing the first button in reset alertview it doesn't get called. Instead, the reset alertview button pops up again. I will appreciate any help I get.
-(void)viewDidLoad{
forgotPassword = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Your login credentials do not match"
delegate:self
cancelButtonTitle:#"Try Again"
otherButtonTitles: #"Forgot Password",nil];
[forgotPassword show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
forgotPassword.tag = 1;
resetPassword.tag = 2;
if (buttonIndex == 1 && forgotPassword.tag ==1)
{
resetPassword = [[UIAlertView alloc] initWithTitle: #"Forgot Password"
message:#"Email" delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
[resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput];
[resetPassword show];
NSLog(#"RESET");
}
if (buttonIndex == 1 && resetPassword.tag == 2) {
NSLog(#"Password");
}
}
Your logic is all messed up. You set both tags so both will always be true. Since you appear to have ivars for the two different alert views, get rid of the tags and do this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == forgotPassword) {
if (buttonIndex == alertView.firstOtherButtonIndex) {
// show other alert
}
else if (alertView == resetPassword) {
if (buttonIndex == alertView.firstOtherButtonIndex) {
// reset
}
}
}
No need to use tag
Try in this way
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView==forgotPassword)
NSLog(#"forgotPassword alert");
}
Your code is confused. You create an alert view "forgotPassword" and show it, without setting it's tag. Then, in your alertView:clickedButtonAtIndex: method, you explicitly set the tag on both alert views, and THEN interrogate those alert views to see what their tags are. Those tags will never change.
Instead, you should set the tag on each alert view before showing it, and then check the tag of the UIAlertView that is passed in to the alertView:clickedButtonAtIndex: method. Something like this:
-(void)viewDidLoad{
forgotPassword = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Your login credentials do not match"
delegate:self
cancelButtonTitle:#"Try Again"
otherButtonTitles: #"Forgot Password",nil];
forgotPassword.tag = 1;
[forgotPassword show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1 && alertView.tag ==1)
{
resetPassword = [[UIAlertView alloc] initWithTitle: #"Forgot Password"
message:#"Email" delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
[resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput];
resetPassword.tag = 2;
[resetPassword show];
NSLog(#"RESET");
}
if (buttonIndex == 1 && alertView.tag == 2) {
NSLog(#"Password");
}
}
You should be using alertView.tag == 1 and alertView.tag == 2 in your methods. As you have it right now, both if statements will always be true as long as buttonIndex == 1 since you explicitly set forgotPassword.tag = 1 and resetPassword.tag = 2, then check each item.
Also, you should set the tags within your viewDidLoad unless you have some reason to keep resetting their values every time an alertView button is pressed.
Or, an easier way would be what Anand K said.
-Stephen
Your code is redundant and in any case hasn't a sense..
You don't need to use UIAlertView as property in this case..
Use this:
-(void)viewDidLoad{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Your login credentials do not match"
delegate:self
cancelButtonTitle:#"Try Again"
otherButtonTitles: #"Forgot Password",nil];
alertView.tag = 1;
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if(alertView.tag == 1)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: #"Forgot Password"
message:#"Email" delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
alertView.tag = 2;
[alertView show];
NSLog(#"RESET");
}
} else if (alertView.tag == 2) {
NSLog(#"Password");
}
}
This code is clean use it ;)
I want to delete data in UITableView using plist and i want to put alertview when delete button is pressed. I want to mention that i have used edit button to delete the data from tableview. code of edit button is as follows: for your information.
self.navigationItem.leftBarButtonItem=self.editButtonItem;
Plz help me solve this problem.
thanx in advance.
In your function write :
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: #"" message: #"" delegate: self cancelButtonTitle: #"OK" otherButtonTitles: nil];
alertView.tag = 1;
[alertView show];
[alertView release];
On alert "OK" button pressed
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
{
if (alertView.tag == 1)
{
//Do your code
}
}
}
Hope it helps...
on you button action write:-
-(IBAction)btnPressed
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alert didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}