PDA

View Full Version : Open AlertView from AlterView




Josh Kahane
Jul 3, 2010, 12:36 PM
Hi

I have tried an failed. I would like to do the following in my app:

Have an alertview pop up then when you press a specific button on that alter it disappears and a new alert pops up. Much like when you delete an app. Its asks if you sure, if you say yes, a second one pops up asking you to rate the app.

Understand? Anyway All I have managed is to get one alertview popping up, but I cant get the second one to follow. If someone could demonstrate how to get it going I would much appreciate it.

If you want to see what I have done already, just say.

Any help much appreciated, thanks.



dejo
Jul 3, 2010, 12:50 PM
Some code would help, that's for sure.

Josh Kahane
Jul 3, 2010, 01:34 PM
-(IBAction)openAlert{
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:@"Submit Score" message:@"Choose below to submit your score to global leaderboards, locally, or both." delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Global", @"Local", @"Both", nil];
[successAlert show];
[successAlert release];
[connectionString release];
}

-(void)successAlert:(UIAlertView *)successAlert clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1){

UIAlertView *onlineSuccessAlert = [[UIAlertView alloc] initWithTitle:@"Successfully Submitted!" message:@"You have successfully submitted your score to the online leaderboards!" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[onlineSuccessAlert show];
[onlineSuccessAlert release];
}
}

So basically, I am saying make this UIAlertView, then if I press the button with index 1, make this second UIAlertView. Not sure why it doesn't work.

chown33
Jul 3, 2010, 01:51 PM
The delegate is nil. What are the consequences?

Josh Kahane
Jul 3, 2010, 01:56 PM
The delegate is nil. What are the consequences?

Same result. I have tried putting all sorts of things into the if statement for index 1, things as simple as changing an integer and no result.

Well I simplified slightly my code for viewing here, but I can show you the entire chunk if you like, just takes a bit more thinking on your end, hehe.

Here is exactly what I have done for the UIAlertView part of my code (in full):

-(IBAction)submitScore{
NSString *connectionString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];

//Do this if your not connected ot internet/offline.
if([connectionString length] == 0){

//Leave spaces in failureAlert message! These allow the texfield to line up correctly and not overlap.
UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle:@"Offline - Submit Local Score" message:@"While offline you can only submit your score locally. This will not be seen by the player. " delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit Score Locally", nil];

nameField = [[UITextField alloc] initWithFrame:CGRectMake(12, 110, 260, 25)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[failureAlert addSubview:nameField];
nameField.placeholder = @"Enter Name Here...";
nameField.borderStyle = UITextBorderStyleLine;
nameField.delegate = self;

[failureAlert show];
[failureAlert release];
}

//Do this if the player is connected to the internet.
else{
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:@"Submit Score"
message:@"Choose below to submit your score to global leaderboards, locally, or both."
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Global", @"Local", @"Both", nil];
[successAlert show];
[successAlert release];
[connectionString release];
}
}

-(void)successAlert:(UIAlertView *)successAlert clickedButtonAtIndex:(NSInteger)buttonIndex {

if(buttonIndex == 1){
[OFHighScoreService setHighScore: score forLeaderboard:@"371803" onSuccess:OFDelegate () onFailure:OFDelegate ()];

UIAlertView *onlineSuccessAlert = [[UIAlertView alloc] initWithTitle:@"Successfully Submitted!"
message:@"You have successfully submitted your score to the online leaderboards!"
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[onlineSuccessAlert show];
[onlineSuccessAlert release];
}

if(buttonIndex == 2){
[highScoresArray addObject:[NSString stringWithFormat:@"%@ - %i", nameField.text, score]];

UIAlertView *offlineAlert = [[UIAlertView alloc] initWithTitle:@"Submit Local Score" message:@"Enter your name below to submit a local score." delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit Score Locally", nil];

nameField = [[UITextField alloc] initWithFrame:CGRectMake(12, 110, 260, 25)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[offlineAlert addSubview:nameField];
nameField.placeholder = @"Enter Name Here...";
nameField.borderStyle = UITextBorderStyleLine;
nameField.delegate = self;

[offlineAlert show];
[offlineAlert release];
}

if(buttonIndex == 3){
[OFHighScoreService setHighScore: score forLeaderboard:@"371803" onSuccess:OFDelegate () onFailure:OFDelegate ()];
[highScoresArray addObject:[NSString stringWithFormat:@"%@ - %i", nameField.text, score]];

UIAlertView *offlineAlert = [[UIAlertView alloc] initWithTitle:@"Submit Local Score" message:@"Enter your name below to submit a local score." delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit Score Locally", nil];

nameField = [[UITextField alloc] initWithFrame:CGRectMake(12, 110, 260, 25)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[offlineAlert addSubview:nameField];
nameField.placeholder = @"Enter Name Here...";
nameField.borderStyle = UITextBorderStyleLine;
nameField.delegate = self;

[offlineAlert show];
[offlineAlert release];
}

}

So above I am first checking if I can connect to the internet, which works great, giving out a specific alert view depending on if your connected or not.

Then I have written the specific button functions for the alert view if your online with the successAlert method. Inside that I want each button to give a certain output, certain ones, giving a second alert view with a new message as I asked about originally. Hope it makes sense.

Josh Kahane
Jul 3, 2010, 02:22 PM
After looking over that, its important to point out I haven't finished it. The contents of the if statements aren't to important, whats more important is nothing is being done inside of them, whatever it is.

chown33
Jul 3, 2010, 02:38 PM
First, if you expect your code to be called when a button is clicked, then you must not pass a nil delegate. There are no magic callbacks. You must specify a non-nil delegate. All the code you posted shows a nil delegate. I don't think you fully understand the consequences.

Second, you must implement the button callback exactly as its name is given in the UIAlertViewDelegate reference. You don't get to rename it to successAlert:clickedButtonAtIndex:. It must be named exactly as the UIAlertViewDelegate reference says it should be.

Your code looks like you've never used UIAlertView with a delegate before. I advise reading the UIAlertViewDelegate reference, which begins:
The UIAlertViewDelegate protocol defines the methods a delegate of a UIAlertView object should implement. The delegate implements the button actions and any other custom behavior. Some of the methods defined in this protocol are optional.
...
If you add your own buttons to an alert view, the delegate must implement the alertView:clickedButtonAtIndex: message to respond when those buttons are clicked; otherwise, your custom buttons do nothing.

[emphasis added]

Josh Kahane
Jul 3, 2010, 02:54 PM
Ok thanks Chown. I will read into it to get some deeper knowledge. I made the delegates self (I changed them to nil after you inquired what would happen if I did) and also instead of using if statements I am using the following:

switch (buttonIndex) {

case 0:
break:

case1:
//code here
break:

}

Thats got it working, but as you said I will read up more on the subject. If I am to do, best do it the right way.

Thanks for the help.

dejo
Jul 3, 2010, 03:09 PM
I made the delegates self...
But do you understand why you should do that? And do you understand the consequences of setting both UIAlertView delegates to self?

Josh Kahane
Jul 3, 2010, 03:12 PM
But do you understand why you should do that? And do you understand the consequences of setting both UIAlertView delegates to self?

Partially, but not entirely. Which is why I am going to go and look at this in more depth, as there is little point in being able to do something but not understand it.

dejo
Jul 3, 2010, 03:15 PM
Partially, but not entirely. Which is why I am going to go and look at this in more depth, as there is little point in being able to do something but not understand it.
Smart thinking. And hopefully these questions give you something more to think about as you learn about delegation.