I created an alert view with three buttons -"Sign Up","Donate","Cancel"- when u click on either sign up or donate, safari should open with the specific website. However when I open the app, and click any of the buttons, safari doesn't open or do anything and the buttons work as though they were different versions of the cancel button. Here is my code:
In BlahBlah.h:
#interface BlahBlah: UIViewController <UITextFieldDelegate, UIAlertViewDelegate> {
}
#end
In BlahBlah.m:
#define donate_tag 0
#import "BlahBlah.h"
#implementation BlahBlah
...
- (void) donate {
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Donate"
message:#"Function doesn't work yet :(" delegate:nil cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Sign Up",#"Donate",nil];
alert2.tag = donate_tag;
[alert2 show];
[alert2 release];
}
...
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag == donate_tag && buttonIndex == 1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
else if(alertView.tag == donate_tag && buttonIndex == 2){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.yahoo.com"]];
}
}
...
#end
You need to tell the alert who handles the actions of the buttons, that is to say, who is the "delegate" of the alert view.
Since everything related to the UIAlertView is within your "BlahBlah" view controller, after creating alert2, do something like this:
alert2.delegate = self;
or declare it in your UIAlertView instantiation:
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Donate"
message:#"Function doesn't work yet :(" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Sign Up",#"Donate",nil];
Related
I am calling a UIAlertView within it's own delegate and it is failing. The code is simple:
#interface ViewController () <UIAlertViewDelegate>
#property (nonatomic, strong) UIAlertView *alertView;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.alertView = [[UIAlertView alloc] initWithTitle:#"Howdy"
message:#"Here's the alert"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[self.alertView show]; // this shows the
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[self.alertView show]; // this does not show the alert again!
}
}
#end
However, when I remove:
[self.alertView show]
and replace it with:
[self.alertView performSelector:#selector(show) withObject:nil afterDelay:0.01]
it works.
This seems to indicate that the original UIAlertVIew is not completely dismissed even though I am inside the delegate method alertView:didDismissWithButtonIndex:.
While this is working, it does not seem right. Can anyone tell me what I am doing wrong?
You are probably right, but I don't see why would you show the same alert again. Since you usually do not require to keep a reference to an alert, you could just write a method like:
- (void)showAlert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Howdy"
message:#"Here's the alert"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
}
It would also be better if you would call this from viewDidAppear instead of viewDidLoad.
How can I create a UIAlertView with some text and a button that will restart the game that I'm creating.
Thanks in advance,
Best Regards, Louis.
Try this one :
[[CCDirector sharedDirector] pause];
UIAlertView *pauseAlert = [[UIAlertView alloc] initWithTitle:#"Game Paused" message:nil delegate:self cancelButtonTitle:#"CANCEL" otherButtonTitles:#"RESTART", nil];
[pauseAlert show];
Delegate Method..
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"Cancel..");
}
else if (buttonIndex == 1)
{
NSLog(#"restart..");
[[CCDirector sharedDirector] resume];
}
}
You need to detect whether a button has been tapped on the UIAlertView. For this, you will need to implement the UIAlertViewDelegate protocol.
Follow these steps:
1 - Implement the protocol. In the scene's .h file
#interface MyScene : SKScene <UIAlertViewDelegate> //This will implement the protocol.
2 - Declare and instantiate the UIAlertView. Set the delegate.
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Restart?" message:#"Do you want to restart the level?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"Yes", nil];
[alert show];
3 - Implement the delegate method
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ([alertView.title isEqualToString:#"Restart?"] && buttonIndex == 1)
{
//Code for restarting the level here.
}
}
Please refer to this tutorial for an explanation on how UIAlertView works.
Below is the action and alert view. Why Won't This Work When The User Taps The Button?
Alert/Action
-(IBAction)myButton
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"" message:#"Call (804) 378-7120?"
delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES",nil];
[alert show];
[alert release];
}
What To Do With User Input
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1)
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel://(804) 378-7120"]];
}
else
{
//Do whatever you want
}
}
As far as I remember, the index of the 1st other button is 0. So you say
if(buttonIndex==0)
{...open you URL...}
else
{
NSLog(#\"cancel\");
}
Per Apple documentation for Phone Links, the format is:
tel:1-408-555-5555
Similar to an email link, there is no //, and notice the use of hyphens, not parenthesis or space.
use this method when the user hit the YES option.
**-(void)phoneCall
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:099123456"]];
}**
Change the number 099123456 to the number what you want to call.
Hope this result useful.
I've got a problem,
I added Flurry SDK in my iOS app and want to show a full screen add each time when the user click in the "Ok" button of an uiAlertView. The problem is that it doesn't work.
So, I tried try to do it without the AlertView and it works perfectly.
Someone knows why it doesn't work after clicking on an AlertView.
// The call of the alertview
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"TitlePopupNewGame", nil) message:nil delegate:self cancelButtonTitle:NSLocalizedString(#"CancelPopupNewGame", nil) otherButtonTitles:NSLocalizedString(#"YesPopupNewGame", nil), nil];
myAlert.tag = 1;
myAlert.delegate = self;
[myAlert show];
// Alertview method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 1 && buttonIndex == 0){
[self showInterstitial];
}
}
// ShowInterstitial method
-(void) showInterstitial{
if ([FlurryAds adReadyForSpace: #"Full screen banner"]) {
[FlurryAds displayAdForSpace: #"Full screen banner"
onView:mv];
[FlurryAds fetchAdForSpace:#"Full screen banner"
frame:mv.frame size:FULLSCREEN];
} else {
// fetch an ad
[FlurryAds fetchAdForSpace:#"Full screen banner"
frame:mv.frame size:FULLSCREEN];
}
}
Thanks
I have created a tab in my app, where the user could call the client directly from within the app.
But I wont show his number, i want to display: "Do you want to call Client?" instead of "Do you want to call 000-000-000"
I've seen this solution in another app, but dont have a clue how to do this.
The openURL call does not automatically prompt the user for anything.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:7205551212"]];
It's good practice to confirm with the user, but it's up to you to determine the message. To show the alert, do something like this:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Confirmation" message:#"Do you want to call Client?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alertView show];
[alertView release];
Then make the actual phone call in a UIAlertViewDelegate method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:7205551212"]];
}
}