 |
 |
|
 |
08-25-2008, 10:33 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: Apr 2008
Posts: 17
|
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
|
|
|
08-25-2008, 11:09 PM
|
#2 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Location: Minneapolis, MN
Posts: 208
|
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.
|
|
|
08-26-2008, 12:59 AM
|
#3 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 401
|
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");
}
}
|
|
|
08-26-2008, 05:20 AM
|
#4 (permalink)
|
|
Registered Member
Join Date: Apr 2008
Posts: 17
|
Stitch,
Thank you for the sample code and how to info, that is just what I was hoping for.
Greg
|
|
|
02-17-2009, 01:20 AM
|
#5 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Posts: 14
|
thank you so much for this info !
|
|
|
08-23-2009, 05:38 AM
|
#6 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 2
|
Thank for the post, header file information was useful for me.
I was missing that.
Quote:
Originally Posted by Stitch
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");
}
}
|
|
|
|
12-11-2009, 03:02 AM
|
#7 (permalink)
|
|
Banned
Join Date: Oct 2009
Location: East Bay, CA
Posts: 171
|
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
|
|
|
12-23-2009, 12:55 AM
|
#8 (permalink)
|
|
Registered Member
Join Date: Dec 2008
Location: India
Posts: 139
|
Quote:
Originally Posted by youngcoder
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)alertView UIAlertView *)actionSheet clickedButtonAtIndex NSInteger)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.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 420 |
| 36 members and 384 guests |
| AdamSubach, aderrington, benoitr007, bensj, BrianSlick, Danneman, dev123, ErichGS, GreatWizard, gustavo7sexton, gw1921, HemiMG, HowEver, iSDK, Jeremy1026, lifeCoder45, mattiahalter, melodizzzy, mriphoneman, newchucky, Ovidius, Piequanna, pofak, qilin, Racker, raheel, rendezvouscp, riq, Sega dude, socals, timle8n1, Whitehk, ZunePod |
| Most users ever online was 965, 06-30-2010 at 04:26 AM. |
» Stats |
Members: 41,861
Threads: 49,770
Posts: 213,057
Top Poster: BrianSlick (3,139)
|
| Welcome to our newest member, melodizzzy |
|