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.
Related
That's really weird. I've never seen anything like this. I'm showing an alert view in UIViewController category method:
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
for(NSString *buttonTitle in otherButtonTitles){
[alertView addButtonWithTitle:buttonTitle];
}
objc_setAssociatedObject(self, &kVCActionHandleAlertCancelBlockKey, cancelBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &kVCActionHandleAlertOtherBlockKey, otherBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
[alertView show];
and -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex is never called!
But other delegate methods, - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
and - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex are successfully called! What the heck? Did you ever see anything like this?
The cause was that I had already -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex defined in my view controller class, but was expecting the same method in category to fire. So the conclusion I think is to never provide delegate methods in categories. It may be dangerous
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.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have this 2-button alert view:
UIAlertView* message = [[UIAlertView alloc]
initWithTitle: #"Delete?" message: #"This business will be deleted permenently." delegate: nil
cancelButtonTitle: #"Cancel" otherButtonTitles: #"Delete", nil];
[message show];
I also have this method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Delete"])
{
NSLog(#"Button DELETE was selected.");
}
else if([title isEqualToString:#"Cancel"])
{
NSLog(#"Button CANCEL was selected.");
}
}
and I added this to the .h file:
<UIAlertViewDelegate>
Right now when either button is pressed, it just closes the dialog. That is ok for cancel, but how do I know when the delete button is pressed?
Thanks!
You have to implement the – alertView:clickedButtonAtIndex: method of the UIAlertViewDelegate. You also have to set the delegate when initialising the alert view.
E.g.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
//Do something
} else if (buttonIndex == 1) {
//Do something else
}
}
The cancel button's index is 0.
You are passing nil to the delegate parameter when you create the alert view. You need to pass self instead. As you have it now, the clickedButtonAtIndex: method is never called.
UIAlertView* message = [[UIAlertView alloc]
initWithTitle: #"Delete?"
message: #"This business will be deleted permenently."
delegate: self
cancelButtonTitle: #"Cancel"
otherButtonTitles: #"Delete", nil];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == alertView.cancelButtonIndex) {
// Cancel was tapped
} else if (buttonIndex == alertView.firstOtherButtonIndex) {
// The other button was tapped
}
}
message.delegate = self;
...
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"Button %d was clicked", buttonIndex);
}
and the class must be declared to meet the UIAlertViewDelegate protocol.
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];
when the user click on a button on the UIAlertView the clickedButtonAtIndex method is supposed to be called, however, it doesn't :
in the .h i have called the UIAlertView protocol :
#interface RechercherViewController : UIViewController<UIAlertViewDelegate> {
//code
}
and in .m file i have this :
-(void)requestFailed:(ASIHTTPRequest *)request
{
//quand il y aura un échec lors de l'envoie de la requête
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"TopStation"
message :#"Your internet connexion is down"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
NSLog(#"buttonIndex 0");
} else if(buttonIndex == 1) {
NSLog(#"buttonIndex 1");
}
}
nothing is shown in the log file, please help, thx in advance :)
delegate:nil is the problem. Pass self as the delegate to initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:.
You're passing nil as the delegate argument. Pass self instead.