Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-25-2008, 11:33 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2008
Posts: 17
Default 2 button UIAlertView - which button was pressed?

Can someone please post a simple example of how to do an alert (UIAlertView) with an OK and Cancel button and how I could simply change detect which button was pressed?

When I read the docs it would appear that I may have to create a UIAlertView delegate and being a Cocoa newbie I am not sure how to go about doing that.

I can display the alert with two buttons using:

Code:
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"AThe message."  delegate:self cancelButtonTitle:@"button 1" otherButtonTitles: @"button", nil];
		[alert show];
		[alert release];
But how do I determine which button was pressed?

Thanks

Greg
trifusion is offline   Reply With Quote
Old 08-26-2008, 12:09 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2008
Location: Minneapolis, MN
Posts: 208
Default

Look up UIAlertViewDelegate in your documentation, its all in there. Basically you just declare the proper method and it will get called for you passing in the button index.
Scuba is offline   Reply With Quote
Old 08-26-2008, 01:59 AM   #3 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 401
Default

This is taken from the UICatalog sample code.

Insure your header file contains the following:

Code:
@interface YourViewController : UIViewController <UIAlertViewDelegate>
Then just add the following in the class you need the alert:

Code:
- (void)alertOKCancelAction {
  // open a alert with an OK and cancel button
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
  [alert show];
  [alert release];
}
and
Code:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  // the user clicked one of the OK/Cancel buttons
  if (buttonIndex == 0)
  {
    NSLog(@"ok");
  }
  else
  {
    NSLog(@"cancel");
  }
}
Stitch is offline   Reply With Quote
Old 08-26-2008, 06:20 AM   #4 (permalink)
Registered Member
 
Join Date: Apr 2008
Posts: 17
Default

Stitch,

Thank you for the sample code and how to info, that is just what I was hoping for.

Greg
trifusion is offline   Reply With Quote
Old 02-17-2009, 02:20 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 14
Default

thank you so much for this info !
reetu.raj is offline   Reply With Quote
Old 08-23-2009, 06:38 AM   #6 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 3
Thumbs up

Thank for the post, header file information was useful for me.
I was missing that.

Quote:
Originally Posted by Stitch View Post
This is taken from the UICatalog sample code.

Insure your header file contains the following:

Code:
@interface YourViewController : UIViewController <UIAlertViewDelegate>
Then just add the following in the class you need the alert:

Code:
- (void)alertOKCancelAction {
  // open a alert with an OK and cancel button
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
  [alert show];
  [alert release];
}
and
Code:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  // the user clicked one of the OK/Cancel buttons
  if (buttonIndex == 0)
  {
    NSLog(@"ok");
  }
  else
  {
    NSLog(@"cancel");
  }
}
sohel62 is offline   Reply With Quote
Old 12-11-2009, 04:02 AM   #7 (permalink)
Banned
 
Join Date: Oct 2009
Location: East Bay, CA
Posts: 171
Default

i am doing this:
Code:
- (IBAction)showCheat
{
	{
		
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning!"
														message: @"Are You Sure you Want to See the Answer? Beware it Takes 3 Points Away From your Grade"
													   delegate: self
											  cancelButtonTitle: @"Cancel"
											  otherButtonTitles: @"OK"];
		[alert show];
		[alert release];
	}

	- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
		// the user clicked one of the OK/Cancel buttons
		if (buttonIndex == 0)
		{
			NSLog(@"Ok");
			
			UIAlertView *alerttwo = [[UIAlertView alloc] initWithTitle: @"Warning!"
															   message: solutionText
															  delegate: self
													 cancelButtonTitle: @"Cancel"
													 otherButtonTitles: @"OK"];
			[alerttwo show];
			[alerttwo release];
			
		}
		else
		{
			NSLog(@"Cancel");
		}
	}
		
}
but get the following errors on the red line:
Wrong type argument to unary minus
expected ';' before ':' token

thanks for any suggestions
youngcoder is offline   Reply With Quote
Old 12-23-2009, 01:55 AM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: India
Posts: 143
Default

Quote:
Originally Posted by youngcoder View Post
i am doing this:
Code:
- (IBAction)showCheat
{
	{
		
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning!"
														message: @"Are You Sure you Want to See the Answer? Beware it Takes 3 Points Away From your Grade"
													   delegate: self
											  cancelButtonTitle: @"Cancel"
											  otherButtonTitles: @"OK"];
		[alert show];
		[alert release];
	}

	- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
		// the user clicked one of the OK/Cancel buttons
.........
		
}
but get the following errors on the red line:
Wrong type argument to unary minus
expected ';' before ':' token

thanks for any suggestions
As far as the above code is concerned, the method - (void)alertViewUIAlertView *)actionSheet clickedButtonAtIndexNSInteger)buttonIndex { is declared inside another method (IBAction) showCheat.

You should add a curly bracket } before the definition of the method -(void)alertView .....

Hope this helps.
anurag is offline   Reply With Quote
Old 08-14-2010, 07:55 PM   #9 (permalink)
Registered Member
 
Join Date: Aug 2010
Location: 60130
Posts: 2
Default call for this method?

thx

Last edited by rooster61; 08-16-2010 at 11:21 AM. Reason: update
rooster61 is offline   Reply With Quote
Old 08-14-2010, 07:56 PM   #10 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,049
Default

Quote:
Originally Posted by rooster61 View Post
please supply how to call this?
Please supply a question that makes sense.
wuf810 is offline   Reply With Quote
Old 09-09-2010, 01:59 PM   #11 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 37
Default

Hi!

I also have a problem with two UIAlertViews. Basically, I need to do the same thing as the topicstarter did, but I have two UIAlertViews.

Thanks in advance!
Knodel is offline   Reply With Quote
Old 09-09-2010, 02:12 PM   #12 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Brooklyn, NY
Posts: 74
Default

I had this problem recently. I ended up using tags:

Code:
  alert1.tag=1;
  alert2.tag=2;
and then in the delegate method:

Code:
  if (actionSheet.tag==1) {

   //check buttonIndex value and do something
  }
  else if (actionSheet.tag==2) {
   //check buttonIndex value and do something different
  }
Desdichado is offline   Reply With Quote
Old 09-09-2010, 02:17 PM   #13 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 37
Default

Thank you!
Knodel is offline   Reply With Quote
Old 09-09-2010, 08:41 PM   #14 (permalink)
FailDev xD
 
Join Date: Mar 2009
Posts: 64
Send a message via AIM to PolishDemon
Default

I don't think you guys need to change the tags. If you're used to it, fine, but I'm pretty sure you can do this w/o any problems:

Code:
//Two alert views named alertOne and alertTwo
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
     if(alertView == alertOne) {
          //do custom code for first alert view
     }
     else if(alertView == alertTwo) {
          //do custom code for second alert view
     }
}
I'm pretty sure this would work. If I'm wrong, feel free to correct me. I'm learning just like many others on this forum are!
__________________
~Proud to suck at developing...right now xD
PolishDemon is offline   Reply With Quote
Old 09-10-2010, 09:18 AM   #15 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Brooklyn, NY
Posts: 74
Default

Quote:
Originally Posted by PolishDemon View Post
I don't think you guys need to change the tags. If you're used to it, fine, but I'm pretty sure you can do this w/o any problems:

Code:
//Two alert views named alertOne and alertTwo
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
     if(alertView == alertOne) {
          //do custom code for first alert view
     }
     else if(alertView == alertTwo) {
          //do custom code for second alert view
     }
}
I'm pretty sure this would work. If I'm wrong, feel free to correct me. I'm learning just like many others on this forum are!
Yes, that would work, but only if alertOne and alertTwo were ivars in your class.

Code:
@interface MyClass: NSObject {

    UIAlertView *alertOne;
    UIAlertView *alertTwo;
}
If you create alertOne inside your method, the delegate method has no reference to that pointer.

Code:
- (void)myMethodToAlertUser {

    UIAlertView *alertOne = [[UIAlertView alloc] initWithTitle:@"Title" message:@"AThe message."  delegate:self cancelButtonTitle:@"button 1" otherButtonTitles: @"button", nil];
    [alertOne show];
    [alertOne autorelease];

}
Desdichado is offline   Reply With Quote
Old 09-10-2010, 02:27 PM   #16 (permalink)
FailDev xD
 
Join Date: Mar 2009
Posts: 64
Send a message via AIM to PolishDemon
Default

@Desdichado - You are absolutely correct. I didn't consider the option of creating objects mid-method. Thanks for the correction!
__________________
~Proud to suck at developing...right now xD
PolishDemon is offline   Reply With Quote
Old 02-24-2011, 10:10 AM   #17 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 4
Default

Quote:
Originally Posted by Desdichado View Post
I had this problem recently. I ended up using tags:

Code:
  alert1.tag=1;
  alert2.tag=2;
and then in the delegate method:

Code:
  if (actionSheet.tag==1) {

   //check buttonIndex value and do something
  }
  else if (actionSheet.tag==2) {
   //check buttonIndex value and do something different
  }
Tags seem like a bit of a bodge, is there a more correct way?
simontheu is offline   Reply With Quote
Old 02-24-2011, 12:24 PM   #18 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 4,999
Default

No.
baja_yu is offline   Reply With Quote
Old 02-25-2011, 06:24 AM   #19 (permalink)
- U haz disappoint -
 
Join Date: Jan 2010
Location: Belgium
Posts: 487
Send a message via MSN to jNoxx Send a message via Skype™ to jNoxx
Default

Quote:
Originally Posted by baja_yu View Post
No.
U and Brian allways make my day, with posting Answers like these.
Keep going ^_-

or I mean.
Quote:
Yes.
jNoxx is offline   Reply With Quote
Old 08-01-2011, 09:15 AM   #20 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 1
Default UIAlertView with login failed example

Can someone please post a simple example of how to do an alert (UIAlertView)
with login.. like username and password whenever username and password wrong at that time alert will appear..

I am new in iphone development...

Thank you..


Sagar Patel
sagarpatel is offline   Reply With Quote
Old 08-01-2011, 03:03 PM   #21 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 184
Default

Below is an example on how to create a UIAlterView with a single Text Box, if you want two text boxes then you will need to adjust and add what you need to make both fit. Also, I found it easier to make the TextBox an ivar, this way I can use the self delegate to grab the text that was entered after the Update button was pressed, this should get you on the right path:

Code:
UIAlertView *optionAlert = [[UIAlertView alloc] initWithTitle:@"Update Monthly Goal" message:@"this message gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Update", nil];
    optionText = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
    [optionText setBackgroundColor:[UIColor whiteColor]];
    NSString *placeholder = [NSString stringWithFormat:@"$%@",MTDGoal];
    optionText.placeholder = placeholder;
    optionText.keyboardType = UIKeyboardTypeNumberPad;
    optionText.keyboardAppearance = UIKeyboardAppearanceAlert;
    optionText.tag = 10;
    optionText.delegate = self;
    optionAlert.tag = 25;
    [optionAlert addSubview:optionText];
    [optionAlert show];
    [optionAlert release];
I'm also using the alert tag to determine which UIAlertView is present since this screen that I pulled this from has 2 other Alerts that show up depending on the WebService that is sending data (such as a Timeout Message from the DB).
mavrik5150 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,311
Threads: 89,034
Posts: 379,818
Top Poster: BrianSlick (7,086)
Welcome to our newest member, ramonpadillas
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:12 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0