UIAlertView IndexButton - ios

I have this code right here for annotations in my map...
//alert view
if ([ann.title isEqual: #"Al-saidiya"]) {
NSString *msg=#"Phone No : 079011111";
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"Contact" message:msg delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"Call Us", nil];
[alert1 show];
}
else if ([ann.title isEqual: #"Al-Kadmiya"]) {
NSString *msg=#"Phone No : 07902222222";
UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:#"Contact" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#"Call Us", nil];
[alert2 show];
}
else if ([ann.title isEqual: #"Palestine St"]) {
NSString *msg=#"Phone No : 0790333333";
UIAlertView *alert3 = [[UIAlertView alloc]initWithTitle:#"Contact" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles: #"Call Us",nil];
[alert3 show];
}
else if ([ann.title isEqual: #"Karada Maryam"]){
NSString *msg=#"Phone No : 07905867";
UIAlertView *alert4 = [[UIAlertView alloc]initWithTitle:#"Contact" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#"Call Us", nil];
[alert4 show];
}
else if ([ann.title isEqual: #"Mansour Office"]) {
NSString *msg=#"Phone No : 07954212";
UIAlertView *alert5 = [[UIAlertView alloc]initWithTitle:#"Contact" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles: #"Call Us",nil];
[alert5 show];
}
else if ([ann.title isEqual: #"Hunting Club"]) {
NSString *msg=#"Phone No : 079337745";
UIAlertView *alert6 = [[UIAlertView alloc]initWithTitle:#"Contact" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles: #"Call Us",nil];
[alert6 show];
}
else if ([ann.title isEqual: #"Al-jadriya"]) {
NSString *msg=#"Phone No : 07976231";
UIAlertView *alert7 = [[UIAlertView alloc]initWithTitle:#"Contact" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles: #"Call Us",nil];
[alert7 show];
}
else if ([ann.title isEqual: #"Al-jamea'a"]) {
NSString *msg=#"Phone No : 07865323";
UIAlertView *alert8 = [[UIAlertView alloc]initWithTitle:#"Contact" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles: #"Call Us",nil];
[alert8 show];
}
}
And when i apply this method ::
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex==1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://576576576"]]];
NSLog(#"It works!");
}
}
it has been applied on every alert objects above there and took the same number.i want every alert object to get its own phone number when i want to call.

Just add a tag to your alert views
if ([ann.title isEqual: #"Al-saidiya"]) {
NSString *msg=#"Phone No : 079011111";
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"Contact" message:msg delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"Call Us", nil];
alert1.tag = 0; // <--
[alert1 show];
}
and check the tag in alertView:clickedButtonAtIndex::
if (alertView.tag == 0) {
// call Al-saidiya
}
...

Well even if the solution proposed by tilo works, I think is not the right approach when you have multiple instances of objects like UIAlertview.
I would like to suggest you to use blocks instead.
These categories (the project use the same pattern for UIActionSheet) allow you to bind an action block to a specific button in your alertView.
Using this approach you can get rid of all the if/switch statements using the delegate pattern.

As the title and the phone number is a 1:1 relationship I'd use a dictionary:
NSDictionary *titlesAndMessages = #{#"Al-saidiya" : #"Phone No : 079011111",
#"Al-Kadmiya" : #"Phone No : 07902222222",
#"Palestine St" : #"Phone No : 0790333333"};
...
NSString *messageString = nil;
for (NSString *keyTitle in [titlesAndMessages allKeys]) {
if ([ann.title isEqualToString:keyTitle]) {
messageString = [titlesAndMessages objectForKey:keyTitle];
break;
}
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Contact" message:messageString delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#"Call Us", nil];
[alert show];
}
This scales a lot better as you won't have to write any additional code to expand, just add entries to the dictionary (automagically or otherwise).

Using UIAlertViewDelegate is really clumsy. I recommend everyone use PSAlertView for any non-trivial use of alerts.
Using this, the code becomes simple and self contained.
- (void)promptToContact:(NSString *)message
withNumber:(NSString *)phoneNumber
{
PSAlertView *alert = [[PSAlertView alloc] initWithTitle:#"Contact"];
[alert setCancelButtonWithTitle:#"Dismiss" block:^{}];
[alert addButtonWithTitle:#"Call" block:^{
NSString *urlString = [NSString stringWithFormat:#"telprompt://%#", phoneNumber];
NSURL *url = [NSURL urlWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
}];
[alert show];
}

First set the tag in your alertview in above code then in your below method. Try like this:-
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
int indexValue=alertView.tag;
switch (indexValue)
{
case 0:
NSLog (#"zero");
//your code
break;
case 1:
NSLog (#"one");
//your code
break;
case 2:
NSLog (#"two");
//your code
break;
case 3:
NSLog (#"three");
// your code
break;
case 4:
NSLog (#"four");
//your code
break;
case 5:
NSLog (#"five");
// your code
break;
...... Up to
case 8:
// your code
break;
default:
NSLog (#"done");
break;
}

Related

iOS ARC UIAlertView leaking memory

I have simple method showing AlertView with textfield. Instruments showing memory leak in this. Please explain.
- (void)method {
NSString *value = [[NSUserDefaults standardUserDefaults] valueForKey:#"key"];
if (value == nil) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alertView.tag = 101;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *txtGroup = [alertView textFieldAtIndex:0];
[txtGroup becomeFirstResponder];
[alertView show];
alertView = nil;
}
}
Please find the screenshot of Instruments:
You need to create alertView as :
static UIAlertView *alertView = nil;
if (!alertView){
alertView = [[UIAlertView alloc] initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
}

I am trying to call an UIAlertView method for particular UIButton like UpdateEmail or ForgotPassword but properly call i am use this code

I am using this code for Update Email Address and Forgot Password but their is a problem when I click on 'ForgotPassword' button it's work properly but when I click on 'UpdateEmail' button it not work properly it call the UIAlert for 'ForgotPassword' button and I am trying to call" else if (self.ForgotPassword.tag == 1) part of -(Void)alertView " for when I press 'UpdateEmail' UIButton.
//Forgot method for ForgotPassword
-(IBAction)ForgotPassword:(id)sender
{
UIAlertView * forgotPassword=[[UIAlertView alloc] initWithTitle:#"Forgot Password" message:#"Please enter your email id" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
forgotPassword.alertViewStyle=UIAlertViewStylePlainTextInput;
[forgotPassword textFieldAtIndex:0].delegate=self;
[forgotPassword show];
}
//Method for Update Email Address
-(IBAction)UpdateEmail:(id)sender
{
if ([PFUser currentUser])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Update Email"
message:#"Enter Your Email Address"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
else
{
UIAlertView *myAlert1 = [[UIAlertView alloc]
initWithTitle:#"Please First Loginig"
message:#"Please First Loging"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok",nil];
[myAlert1 show];
}
}
// Method for Alert View
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
self.ForgotPassword.tag=0;
self.UpdateEmail.tag=1;
if (self.ForgotPassword.tag == 0){
if(buttonIndex ==1){
NSLog(#"ok button clicked in forgot password alert view");
NSString *femailId=[alertView textFieldAtIndex:0].text;
if ([femailId isEqualToString:#""]){
UIAlertView *display;
display=[[UIAlertView alloc] initWithTitle:#"Email" message:#"Please enter password for resetting password" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[display show];
}else{
[PFUser requestPasswordResetForEmailInBackground:femailId block:^(BOOL succeeded, NSError *error){
UIAlertView *display;
if(succeeded){
display=[[UIAlertView alloc] initWithTitle:#"Password email" message:#"Please check your email for resetting the password" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
}else{
display=[[UIAlertView alloc] initWithTitle:#"Email" message:#"Email doesn't exists in our database" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles: nil];
}
[display show];
}];
}
}
}else if (self.ForgotPassword.tag == 1){
PFUser *user = [PFUser currentUser];
user[#"email"] = [alertView textFieldAtIndex:0].text;
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
if (succeeded){
UIAlertView *myAlert1 = [[UIAlertView alloc]
initWithTitle:#"Email Upadated!"
message:#"your Email is Updated"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok",nil];
[myAlert1 show];
//NSLog(#"Success");
}else{
UIAlertView *myAlert1 = [[UIAlertView alloc]
initWithTitle:#"Email is NOT Update"
message:#"Email is alredy registred"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok",nil];
[myAlert1 show];
NSLog(#"Error");
}
}];
}
}
You need to give tag to your two different UIAlertView like below.
-(IBAction)ForgotPassword:(id)sender
{
UIAlertView * forgotPassword=[[UIAlertView alloc] initWithTitle:#"Forgot Password" message:#"Please enter your email id" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
forgotPassword.alertViewStyle=UIAlertViewStylePlainTextInput;
[forgotPassword textFieldAtIndex:0].delegate=self;
[forgotPassword show];
forgotPassword.tag = 0; //// Here for forgot password
}
-(IBAction)UpdateEmail:(id)sender
{
if ([PFUser currentUser])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Update Email"
message:#"Enter Your Email Address"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
alert.tag =1; ///Here for email update
}
}
Then, in -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex, you can detect which alertView's button was clicked.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
{
if(alertView.tag == 0) /// Because we assigned forgotPassword.tag = 0; above for forgotPassword
{
if(buttonIndex == YOUR_DESIRED_BUTTON_INDEX)
{
///Your code for Forgot Password.
}
}
else if(alertView.tag ==1) /// Because we assigned alert.tag = 1; above for update email
{
if(buttonIndex == YOUR_DESIRED_BUTTON_INDEX)
{
///Your code for Update Email.
}
}
}

Opening a new view controller after alert

enter code hereI am having trouble with going back to the previous view controller when alert is presented.
What I am trying to do is have the user enter in data, then an alert appear saying it was successful, then return to the previous view controller.
I currently have no code doing so and am seeking assistance with what I should put in.
- (IBAction)saveLabel:(id)sender
{
NSArray *data = [[NSUserDefaults standardUserDefaults] objectForKey:#"DATA"];
NSMutableArray *currentDataArray;
if (data == nil)
{
currentDataArray = [[NSMutableArray alloc]init];
}
else
{
currentDataArray = [[NSMutableArray alloc]initWithArray:data];
}
[currentDataArray addObject:self.textField.text];
[[NSUserDefaults standardUserDefaults] setObject:currentDataArray forKey:#"DATA"];
}
- (IBAction)enterButtonPressed:(id)sender
{
NSLog(#"enterButtonPressed");
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
}
//If u r using dismissing
[self dismissViewControllerAnimated:YES completion:^{
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
}];
//if u r using navigation ,popViewController
[CATransaction begin];
[CATransaction setCompletionBlock:^{
// handle completion here
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
}];
[self.navigationController popViewControllerAnimated:YES];
[CATransaction commit];
Set Delegate self of your UIAlertView
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
Use Delegate Method of UIAlertviewController.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0){
// Do your Stuff Here....
[self.navigationController popViewControllerAnimated:TRUE];
}
}
Add the following UIAlertViewDelegate method to your implementation file:
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex {
// If you are presenting this view controller
[self dismissViewControllerAnimated:YES completion:nil];
// If you are pushing this view controller
[self.navigationController popViewControllerAnimated:YES];
}
Also remember to set your UIAlertView delegate to your view controller, change to the following code:
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
enterAlert.tag=100;
[enterAlert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == 100) {
if (buttonIndex == 0) {
// Do something when ok pressed
// If you are presenting this view controller
[self dismissViewControllerAnimated:YES completion:nil];
// If you are pushing this view controller
[self.navigationController popViewControllerAnimated:YES];
} else {
// Do something for other alertviewButton}
else{// Do something with responses from other alertViews by giving tags
}

Using buttonIndex with multiple alert views

I have about 4 alert views with different criteria when they appear. In all 4 views, the right button should always do the same thing.
I use the code below to try and say IF the buttonIndex == 1, do something.
Currently, It only works in one of my alert views. The others just end up closing the alert view and never running the code for IF buttonIndex == 1.
Any ideas would be appreciated.
if (a==1) {
NSString *message = [[NSString alloc] initWithFormat:
#"Only $%#!",dollas.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Really?!"
message:message
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:#"Facebook",nil];
[alert show];
[alert release];
[message release];
}
else if (a==2) {
NSString *message = [[NSString alloc] initWithFormat:
#"Somone just paid you $%#", dollas.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Swish!"
message:message
delegate:nil
cancelButtonTitle:#"Close"
otherButtonTitles:#"Facebook",nil];
[alert show];
[alert release];
[message release];
}
And the delegate:
- (void)alertView:(UIAlertView *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1)
{
do.stuff;
}
You should be setting the delegate to self so that method gets called.
IE -
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Really?!"
message:message
delegate:self //SELF
cancelButtonTitle:#"Close"
otherButtonTitles:#"Facebook",nil];
Set the tags on each alertview and inside -didDismissWithButtonIndex check first for the alerts tag
eg:
if (a==1) {
NSString *message = [[NSString alloc] initWithFormat:
#"Only $%#!",dollas.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Really?!"
message:message
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:#"Facebook",nil];
alert.tag = 1;
[alert show];
[alert release];
[message release];
}
else if (a==2) {
NSString *message = [[NSString alloc] initWithFormat:
#"Somone just paid you $%#", dollas.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Swish!"
message:message
delegate:nil
cancelButtonTitle:#"Close"
otherButtonTitles:#"Facebook",nil];
alert.tag = 2;
[alert show];
[alert release];
[message release];
}
then in -didDismissWithButtonIndex
- (void)alertView:(UIAlertView *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1 && actionSheet.tag == 1)
{
do.stuff;
}
else if (buttonIndex == 1 && actionSheet.tag == 2)
{
do.otherStuff;
}
For the case (a == 2) you set the UIAlertView delegate to nil, so - (void)alertView:(UIAlertView *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex shouldn't even be getting called for this case. Change it to set the delegate to self.

UIAlertVIew in UIAlertView

-(IBAction) secondTwoSlotSettings: (id)sender{
UIAlertView *secondTwoSlotSettings = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:#"キャンセル"
otherButtonTitles:#"2スロット カメラ",#"2スロット カメラロール", nil];
[secondTwoSlotSettings show];
}
-(IBAction) firstTwoSlotSettings: (id)sender{
UIAlertView *firstTwoSlotSettingsMessage = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:#"キャンセル"
otherButtonTitles:#"1スロット",#"2スロット", nil];
[firstTwoSlotSettingsMessage show];
}
-(IBAction) oneSlotSettings: (id)sender{
UIAlertView *oneSlotSettingsMessage = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:#"キャンセル"
otherButtonTitles:#"カメラ",#"カメラロール", nil];
[oneSlotSettingsMessage show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"キャンセル"])
{
NSLog(#"キャンセル was selected.");
}
else if([title isEqualToString:#"はい"])
{
NSLog(#"はい was selected.");
}
else if([title isEqualToString:#"2スロット カメラ"])
{
NSLog(#"2スロット カメラ was selected.");
}
else if([title isEqualToString:#"1スロット"])
{
NSLog(#"1スロット was selected.");
}
else if([title isEqualToString:#"2スロット"])
{
NSLog(#"2スロット was selected.");
UIAlertView *oneSlotSettingsMessage = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:#"キャンセル"
otherButtonTitles:#"2スロット カメラ",#"2スロット カメラロール", nil];
[oneSlotSettingsMessage show];
}
else if([title isEqualToString:#"カメラ"])
{
NSLog(#"カメラ was selected.");
}
else if ([title isEqualToString:#"カメラロール"])
{
NSLog(#"カメラロール was selected.");
}
Here is my code:
I was only able to show the firstTwoSlotSettings and oneSlotSettings. but when I try to show the secondTwoSlotSettings it vanish.
No It is not possible with Apple's UIAlertView. When you present an alertView ,it will come modal to all other views and dismiss any other Alerts already present in the screen.
One thing you can do is to create a custom View as your alert and animates its present/dismiss action? Then you have the control to do whatever you want..

Resources