alertView: clickedButtonAtIndex: not called. Other delegate methods get called - ios

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

Related

Show UIAlertView in delegate fails

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.

Spritekit - Replay Button

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.

How to respond to UIImagePickerController Generated UIAlertView

I am using UIImagePickerController to capture video from my app and i have set video maximum duration to 30 seconds. When that 30 seconds limit is reached. I get an alert with a message "maximum video recording limit reached" produced by UIImagePickerController and it stops capturing video.
What I want is that I want to respond to that alert that is generated automatically when 30 seconds limit is reached. I want to perform some action when "OK" button of that alert is pressed. I have implemented all the delegate methods of UIAlertView but it does come in any method when I press OK button.
Please help me how I can respond to that alert?
You can't use all those delegate methods because you didn't initiate the UIAlertView so you can't set his delegate...
The only thing I can think about is to do somethong like listening to the UIWindowDidBecomeVisibleNotification to detect when an alert is shown and to the UIWindowDidBecomeHiddenNotification notification to detect when it disappears.
You should know that those notification will fire for all kind of components that uses their own UIWindow such as UIActionSheet or the keyboard, so you need to make sure this is the right one (maybe check to see if there is a UIAlertView in one of the subviews..)
Set yourself as a delegate of your UIImagePickerController, and implement the UIImagePickerControllerDelegate protocol. Specifically, the following method:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)
Use UIAlertViewDelegateProtocol
Form docs
alertView:clickedButtonAtIndex:
Sent to the delegate when the user
clicks a button on an alert view.
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex Parameters alertView The
alert view containing the button. buttonIndex The index of the button
that was clicked. The button indices start at 0. Discussion The
receiver is automatically dismissed after this method is invoked.
Availability Available in iOS 2.0 and later. Declared In UIAlertView.h
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
// do stuff for button index 0,ie cancel button and sthe same for other button indeces
}
}
Please refer this tutorials : http://mobile.tutsplus.com/tutorials/iphone/uialertview/ you can get more ideal about this :
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Hello World!"
message:#"This is your first UIAlertview message."
delegate:self
cancelButtonTitle:#"Button 1"
otherButtonTitles:#"Button 2", #"Button 3", nil];
[message show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Button 1"])
{
NSLog(#"Button 1 was selected.");
}
else if([title isEqualToString:#"Button 2"])
{
NSLog(#"Button 2 was selected.");
}
else if([title isEqualToString:#"Button 3"])
{
NSLog(#"Button 3 was selected.");
}
}

ios - I have a 2-button UIAlertView, how do I know which button is pressed? [closed]

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.

clickedButtonAtIndex method is not called

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.

Resources