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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 02-16-2011, 05:44 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default Giving Multiple AlertViews Actions

Hi I need to know how to give two differnt alert views the same action how can I do that Ive been trying but It just doesnt work

heres my code for the first and second AlertViews


- (IBAction)pushButton:(id)sender
{

//Freebies Redeem Alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Redeeming A Freebie" message:@"Press Redeem To recieve your Service" delegate:self cancelButtonTitle:@"No Thanks" otherButtonTitles:@"Redeem", nil];
[alert show];
[alert release];
}


#pragma mark -
#pragma mark - UIAlertViewDelegate



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)
{
//NSLog(@"ok");
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1bw" ofType:@"png"]];
[image1bw setImage:img];
[img release];

btn1.hidden = YES;
}

else (buttonIndex == 0);
{
//NSLog(@"cancel");
}
}





// End of code for first card



- (IBAction)pushButton1:(id)sender
{

//Freebies Redeem Alert
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Your Redeeming A Freebie" message:@"Press Redeem To recieve your Service" delegate:self cancelButtonTitle:@"No Thanks" otherButtonTitles:@"Redeem", nil];
[alert1 show];
[alert1 release];
}


#pragma mark -
#pragma mark - UIAlertViewDelegate

- (void)alert1:(UIAlertView *)alertView1 clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)
{
//NSLog(@"ok");
UIImage *img1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2bw" ofType:@"png"]];
[image2bw setImage:img1];
[img1 release];

}

else (buttonIndex == 0);
{
//NSLog(@"cancel");
}
}
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 06:03 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You can not name delegate methods as you wish. alertView:clickedButtonAtIndex: is the name and it has to be like that. Only one method can exist in the delegate and it will be called for every alertview. To differentiate between alertviews, set their Tag property to different values (integers), then in the clickedButtonAtIndex method check the tag value to know which alertview invoked the method, then check buttonIndex to know what action to do.
baja_yu is offline   Reply With Quote
Old 02-16-2011, 06:07 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
You can not name delegate methods as you wish. alertView:clickedButtonAtIndex: is the name and it has to be like that. Only one method can exist in the delegate and it will be called for every alertview. To differentiate between alertviews, set their Tag property to different values (integers), then in the clickedButtonAtIndex method check the tag value to know which alertview invoked the method, then check buttonIndex to know what action to do.
How can I do that
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 06:11 PM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

[alert1 setTag:1];
[alert2 setTag:2];

then in clickedButtonAtIndex check if ([alertview tag] == 1) and so on.
baja_yu is offline   Reply With Quote
Old 02-16-2011, 06:20 PM   #5 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
[alert1 setTag:1];
[alert2 setTag:2];

then in clickedButtonAtIndex check if ([alertview tag] == 1) and so on.
How would that be put into this bit of code


- (void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)
{
//NSLog(@"ok");
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1bw" ofType:@"png"]];
[image1bw setImage:img];
[img release];

btn1.hidden = YES;
}

else (buttonIndex == 0);
{
//NSLog(@"cancel");
}
}
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 06:32 PM   #6 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You may want to read a good book with basics on C/ObjC. That's a trivial task using an if statement.

Also, that else clause as I've commented twice now... and [code] tags.
baja_yu is offline   Reply With Quote
Old 02-16-2011, 06:35 PM   #7 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
You may want to read a good book with basics on C/ObjC. That's a trivial task using an if statement.

Also, that else clause as I've commented twice now... and [code] tags.
Is there a way you can show me where to put the [alert1 setTag:1]; code
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 06:41 PM   #8 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You set the tag before showing the alert view.
baja_yu is offline   Reply With Quote
Old 02-16-2011, 06:44 PM   #9 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
You set the tag before showing the alert view.
Like This?

- (IBAction)pushButton:(id)sender
{
[alert setTag:1];
//Freebies Redeem Alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Redeeming A Freebie" message:@"Press Redeem To recieve your Service" delegate:self cancelButtonTitle:@"No Thanks" otherButtonTitles:@"Redeem", nil];
[alert show];
[alert release];




}
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 06:57 PM   #10 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Did you try to run that?

And again, code tags, please!
baja_yu is offline   Reply With Quote
Old 02-16-2011, 07:04 PM   #11 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Did you try to run that?

And again, code tags, please!
Ya It did not work
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 07:11 PM   #12 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Although I know what the problem is from the previous code, saying "it didn't work" is worse than saying nothing at all. You have to explain your problem, did you get an error, which (exact message), on which line of code etc. If not, what did the code do, what did you expect it to do and so on.

In your case you are trying to set the tag property of the alert object that you did not yet declare or initialize. You need to set it after.
baja_yu is offline   Reply With Quote
Old 02-16-2011, 07:16 PM   #13 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Although I know what the problem is from the previous code, saying "it didn't work" is worse than saying nothing at all. You have to explain your problem, did you get an error, which (exact message), on which line of code etc. If not, what did the code do, what did you expect it to do and so on.

In your case you are trying to set the tag property of the alert object that you did not yet declare or initialize. You need to set it after.
ok the error message is this

'alert' Undeclared

on this code
[alert setTag:1];
how can this be fixed

Last edited by Pjgdog; 02-16-2011 at 07:23 PM.
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 07:25 PM   #14 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Quote:
Originally Posted by Pjgdog View Post
ok the error message is this

'alert' Undeclared

on this code
[alert setTag:1];
how can this be fixed
Oh and How do I declare it
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 07:27 PM   #15 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You should really get a book on basics of programming, intro to Objective C or something like that. Just copy/pasting code will only get you frustrated and you'll pick up bad habits and learn wrong.

You declared it one line below: UIAlertView *alert = ...
baja_yu is offline   Reply With Quote
Old 02-16-2011, 07:35 PM   #16 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Ive got this code but I still get the error message saying alert Undelcared

- (IBAction)pushButton1id)sender {

[alert setTag:2];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Redeeming A Freebie" message:@"Press Redeem To recieve your Service" delegate:nil cancelButtonTitle:@"No Thanks" otherButtonTitles:@"Redeem", nil];
[alert show];
[alert release];
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 07:37 PM   #17 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

How is that different from your previous code? You are still trying to set the tag before you declare the alertview.

And again, [code] tags, PLEASE! When you post source code write:

[code]

...some code here

[/ code]

(remove the space between / and code there) and it will look like this

Code:
.... some code here
baja_yu is offline   Reply With Quote
Old 02-16-2011, 07:43 PM   #18 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
How is that different from your previous code? You are still trying to set the tag before you declare the alertview.

And again, [code] tags, PLEASE! When you post source code write:

[code]

...some code here

[/ code]

(remove the space between / and code there) and it will look like this

Code:
.... some code here

ok so is this better?

- (IBAction)pushButton:(id)sender
{

//Freebies Redeem Alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Redeeming A Freebie" message:@"Press Redeem To recieve your Service" delegate:self cancelButtonTitle:@"No Thanks" otherButtonTitles:@"Redeem", nil];
[alert show];
[alert release];

[alert setTag:1];



}
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 07:50 PM   #19 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

And what about the [code] tags in the post?!

As for your code, you are now setting the tag after you release alert, which might work but isn't good practice to do, set the tag after you declare it, and before you show the alert.
baja_yu is offline   Reply With Quote
Old 02-16-2011, 07:51 PM   #20 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

I think I got the Tags set but I cant quite get the other Alert View To do Its Action

heres the code

- (void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex

{
([alertView tag] == 1);
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)
{
//NSLog(@"ok");
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1bw" ofType:@"png"]];
[image1bw setImage:img];
[img release];

btn1.hidden = YES;
}

else (buttonIndex == 0);
{
//NSLog(@"cancel");
}


([alertView tag] == 2);
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)
{
//NSLog(@"ok");
UIImage *img1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2bw" ofType:@"png"]];
[image2bw setImage:img1];
[img1 release];

btn2.hidden = YES;
}

else (buttonIndex == 0);
{
//NSLog(@"cancel");
}
}

Last edited by Pjgdog; 02-16-2011 at 07:53 PM.
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 07:54 PM   #21 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

As I said, if you have more than one alert view, be sure to set their tags to different numbers before showing them, so you can later identify them by those numbers when a button is clicked. The same way you check the buttonIndex to see which button was pressed, you need to check the tag to see which alertview is in question.
baja_yu is offline   Reply With Quote
Old 02-16-2011, 08:00 PM   #22 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
As I said, if you have more than one alert view, be sure to set their tags to different numbers before showing them, so you can later identify them by those numbers when a button is clicked. The same way you check the buttonIndex to see which button was pressed, you need to check the tag to see which alertview is in question.
Didnt I Do that by saying

([alertView tag] == 2);
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)
{
//NSLog(@"ok");
UIImage *img1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2bw" ofType:@"png"]];
[image2bw setImage:img1];
[img1 release];

btn2.hidden = YES;
}

else (buttonIndex == 0);
{
//NSLog(@"cancel");
}
}
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 08:07 PM   #23 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

I've told you this three times already, there is no condition after else, and certainly no semi-colon. I'm surprised that the code even gets passed compiler.

It's either

Code:
} else {
  //stuff
}

or

} else if (condition) {
   //stuff
}
I'm not answering any more until you start putting [code] tags around the code in your posts.
baja_yu is offline   Reply With Quote
Old 02-16-2011, 08:13 PM   #24 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 112
Pjgdog is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
I've told you this three times already, there is no condition after else, and certainly no semi-colon. I'm surprised that the code even gets passed compiler.

It's either

Code:
} else {
  //stuff
}

or

} else if (condition) {
   //stuff
}
I'm not answering any more until you start putting [code] tags around the code in your posts.
Ok I got the first one to work but now Im facing another Problom

When I use My Second alert view the action does not execute

here is my code

[code]


- (void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex
{

if ([alertView tag] == 1)
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)
{
//NSLog(@"ok");
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1bw" ofType:@"png"]];
[image1bw setImage:img];
[img release];

btn1.hidden = YES;
}

else (buttonIndex == 0);
{
//NSLog(@"cancel");
}

}

if ([alertView tag] == 2)
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)
{
//NSLog(@"ok");
UIImage *img1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2bw" ofType:@"png"]];
[image2bw setImage:img1];
[img1 release];

btn2.hidden = YES;
}

else (buttonIndex == 0);
{
//NSLog(@"cancel");
}
}
}

[code]
Pjgdog is offline   Reply With Quote
Old 02-16-2011, 08:30 PM   #25 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

:S

You didn't close the code tags properly. Read post #17 again.
You didn't fix the else blocks. Read post #23 again.

Also, imageWithContentsOfFile returns an autoreleased object, you should't be releasing them.
baja_yu 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
» Online Users: 368
11 members and 357 guests
cpsclicker, dre, Error404, gmarro, jeroenkeij, Kirkout, Music Man, PavelMik, teebee74, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

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