UIAlertView button tagging - iphone

Is there a way I can add a .tag to an UIAlertView button? Reason being, I'm adding a few dynamic buttons to the alert that will sometimes be in the alert and sometimes not. I figured the best way was to add a tag. Is there a better method for this?
The options that will ALWAYS be in the alert are Email, Save. And the 2 optional options are Tweet This and Facebook.
Thanks for any help in advance!

There is one method buttonTitleAtIndex for UIAlertView. Use that to find the button tapped by user.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if( [buttonString isEqualToString:#"Facebook"] ){
// your code here
} else if( [buttonString isEqualToString:#"twitter"] ){
// your code here
}
}

You can also use tag proprerty:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Info"
message:#"Info text"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert setTag:0];
Then in delegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (alertView.tag) {
case 1: {
...
break;
}
...
default:
break;
}

Related

Multiple alertviews in a single view?

I have an Iphone application in which when i am pressing a button it shows an alertview to chose the background.whichever background user is chosing will be played as the background of the audio clips.But now i need to add another alert before i am showing this alert for giving some warning.after that only i need to pop the second one.but i was done that chosing alert in the didappear of that viewcontroller and set it as a Uialertview delegate.and on the button actions i was doing different actions.Can anybody help me on achieving this?
proAlertView *loginav1=[[proAlertView alloc] initWithTitle:#"title" message:#"Choose a Background to play with this program?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Field",#"Beach", #"Stars",nil];
[loginav1 setBackgroundColor:[UIColor colorWithRed:0.129 green:0.129 blue:0.129 alpha:1.0] withStrokeColor:[UIColor colorWithHue:0.625 saturation:0.0 brightness:0.8 alpha:0.8]];
[loginav1 show];
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
//[self play];
//moviePlayer.scalingMode=MPMovieScalingModeAspectFill;
if(actionSheet.tag==123)
{
[self backButtonPressed];
}
}
else if (buttonIndex == 1)
{
videoFile = [[NSBundle mainBundle] pathForResource:#"video-track" ofType:#"mp4"];
[self play];
moviePlayer.scalingMode=MPMovieScalingModeAspectFill;
}
how can i include another alert before this is my question?
Initialize first Alertview
UIAlertView *al1 = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Warning Msg!!!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
al1.tag=1;
al1.delegate=self;
[al1 show];
Implement Delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==1){
// implement button events for first Alertview
if(buttonIndex==1){
//First button clicked of first Alertview
UIAlertView *al2 = [[UIAlertView alloc] initWithTitle:#"Choose BG" message:#"Choose BG?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"1",#"2",#"3", nil];
al2.tag=2;
al2.delegate=self;
[al2 show];
}
}
if(alertView.tag==2){
// implement button events for second Alertview
if(buttonIndex==1){
// First button clicked second Alertview.
}
}
}
Controller Class header
#interface ViewController : UIViewController<UIAlertViewDelegate>{
}
Hope this will fulfill your need !
You can do like this, first display warning message in alertview and when user click OK in alertview then in alertview delegate method write code to display second alertview where user can choose background.

How to exit the program after detecting user clicked the OK button on the UIAlertView

In my program, I have a code as below. How to exit the program after DETECTING user click OK on the UIAlertView?
Thanks
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"YOur Message" message:#"Your description"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
For getting the cancel (your "OK") button Implement this method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
exit(0);
}
Check the QA here.
see this tutorial
if You want then use exit(0);
set AlertView delegate to self. and do your task in following delegate-
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
For capturing the ok pressed, use this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
exit(0);
}

UIAlertViewStylePlainTextInput - Program continues without waiting for text input from user

I'm sort of stuck on a UIAlertViewStylePlainTextInput. (My code is too long to post here, but this is the part where the problem exists.) It all sort of works, but the program doesn't wait until the user enters the information before continuing. The "editName" button doesn't change the display in the code below, but the updateDisplay button works fine (if you've pressed the "editName" button before you pressed "updateDisplay"). How can I get everything to sort of halt until the user enters the info (or cancels). Or is there some other lead that will take me where I need to go. (I've been toying with putting the rest of the code in the -(void) alertView, but that just doesn't seem to be the correct way of doing it).
Thanks in advance.
#import "HSTestViewController.h"
#implementation HSTestViewController
#synthesize display;
NSString *newName;
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
newName = [[alertView textFieldAtIndex:0] text];
}
}
- (void) getNewName;
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You made the High Score List"
message:#"Please enter your name"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
- (IBAction)editName:(id)sender
{
[self getNewName];
newName = display.text;
}
- (IBAction)updateDisplay:(id)sender
{
display.text = newName;
}
#end
Add UIAlertViewDelegate to the class. And implement the below delegate method.
OK button index value will be 1 in your case.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex)
{
// call the method or the stuff which you need to perform on click of "OK" button.
}
}
Hope this helps.

Alert message for the button click in iphone sdk 4.3

I am beginner to xcode programming.Please tell me how to display the alert message when we are going to click the button in xcode-iphone-4.3
My Code is as follows,
- (IBAction)buttonPressed:(id)sender{
UIAlertView* mes=[[UIAlertView alloc] initWithTitle:#"Hello World!!!!!!"
message:#"This is the Iphone app" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[mes show];
[mes release];
Please help me regarding this.
-(IBAction)buttonOnePressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Clicked button 1"
message: #"Alert Message here"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert setTag:1];
[alert show];
}
-(IBAction)buttonTwoPressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Clicked button 2"
message: #"Alert Message here"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert setTag:2];
[alert show];
}
Below is the delegate method to track which button on Alertview is hit.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1) { // UIAlertView with tag 1 detected
if (buttonIndex == 0)
{
NSLog(#"user pressed Button Indexed 0");
// Any action can be performed here
}
else
{
NSLog(#"user pressed Button Indexed 1");
// Any action can be performed here
}
}
else if (alertView.tag == 2) { // UIAlertView with tag 2 detected
if (buttonIndex == 0)
{
NSLog(#"user pressed Button Indexed 0");
// Any action can be performed here
}
else
{
NSLog(#"user pressed Button Indexed 1");
// Any action can be performed here
}
}
}
You can set tag to UIAlertView in case you have more than one UIAlertViews and can determine which UIAlertView button is clicked in its delegate method clickedButtonAtIndex using its respective tag.
In IBAction you have to write the code and give the Connections to the Button
Create the IBAction for your button and add the code for alert view in that method.

UIAlertView and detemining what is clicked

I have code that when a user hits the end of the game, it prompts them if the would like to play again:
-(void)showAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#" B U S T E D ! "
message:#"Sorry, you busted!\n\nWant to try your luck 1 More Time! ?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"New Game", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
//here is where we can close it
}
if (buttonIndex == 1)
{
[self createNewGame];
}
}
Now I want to also do a check when a user first starts the app to see if a prior game file exists and if so ask if they want to continue. I know I can do via:
-(void)priorGameExists
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#" Previous Game Exists ! "
message:#"A previous game currently exists. Would you like to resume that game?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Resumse", nil];
[alert show];
[alert release];
}
But how do I have it go to a new "custom" clickedButtonAtIndex? Am I correct in assuming it has something to do with setting a different delegate? And if so, how would I do that?
You don't necessarily need a different delegate. Read my answer to this question:
iPhone - Several UIAlertViews for a delegate
One solution is to declare some UIAlertView as private class instance like that:
#interface myViewControllerInterface : UIViewController {
#private
UIAlertView *newGameAlert;
UIAlertView *resumeGameAlert;
}
Then in your view controller you can create your alertViews using them:
-(void)showAlert {
newGameAlert= [[UIAlertView alloc] initWithTitle:#" B U S T E D ! "
message:#"Sorry, you busted!\n\nWant to try your luck 1 More Time! ?"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"New Game", nil];
[newGameAlert show];
[newGameAlert autorelease];
}
-(void)priorGameExists {
resumeGameAlert = [[UIAlertView alloc] initWithTitle:#" Previous Game Exists ! "
message:#"A previous game currently exists. Would you like to resume that game?"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Resumse", nil];
[resumeGameAlert show];
[resumeGameAlert autorelease];
}
And to finish you can make the difference between each alert view using their pointer:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (actionSheet == newGameAlert ) {
//do something
} else if (actionSheet == resumeGameAlert ) {
//do something
}
}
You could use a different delegate but an easier way would be to set the tag property to a unique value. If tag was, say, 10 you'd know it was from the original alert and if it was 20 it would be from the priorGameExits question. (You should probably use constants of course.)
in your clickedButtonAtIndex method test the title of the incoming alertview.
if ([actionSheet.title isEqualToString:#" B U S T E D ! "]) {
// do some busted stuff here
else if ([actionSheet.title isEqualToString:#" Previous Game Exists ! "]) {
// do some previous game stuff here
}
You'll probably want to set those titles using static strings, so you only have the string in one place in your code, but this is basically how you'd do it.