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

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

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, 10: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-25-2008, 11:09 PM   #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, 12: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, 05: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, 01: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, 05:38 AM   #6 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 2
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, 03: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, 12:55 AM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: India
Posts: 139
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
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: 41,861
Threads: 49,770
Posts: 213,057
Top Poster: BrianSlick (3,139)
Welcome to our newest member, melodizzzy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:02 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0