René Josefsen
René Josefsen

Reputation: 139

UIAlertView in appdelegate.m does not work

I have been googling this problem for almost a whole day now, without getting any closer to a solution, so i would like to ask you guys.. :)

I'm working on an iOS app, which should connect to a mbed over WiFi and give the user a dialog if it connects and if it doesn't and if not, then give the user the possibility to retry. My problem is now that i have implemented the connecting method in appdelegate.m and it is from here I would like to show the alerts..

The alerts it self works fine, but I have problems detecting when a button is pressed, the clickedButtonAtIndex is not being called.

I have added the UIAlertViewDelegate in the appdelegate.h, like so:

@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate, UIAlertViewDelegate> 

and have set the delegate to self, in the alertview, like so:

alert_NOT = [[UIAlertView alloc] initWithTitle:@"Not connected!" message:message_to_user delegate:self cancelButtonTitle:@"Try again" otherButtonTitles: nil];
    [alert_NOT show];
    [alert_NOT release]

and the clickedButtonAtIndex looks like

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"test");

}

So I would love to see the word "test" in the log when a button is pressed in the alertview, but nothing happens.

Update: Tried implementing it in my "FirstViewController.m" and there it works :S but I would very much like to have it in the appdelegate.m if possible..

Upvotes: 2

Views: 4053

Answers (4)

@interface urAppDelegate : NSObject <UIApplicationDelegate,UIAlertViewDelegate>

If you synthesized the alert_not then use it like this with self:

self.alert_NOT = [[UIAlertView alloc] initWithTitle:@"Not connected!" message:message_to_user delegate:self cancelButtonTitle:@"Try again" otherButtonTitles: nil];

[alert_NOT show];
[alert_NOT release];

Upvotes: 1

Satish A
Satish A

Reputation: 584

Define as below:

#define appDelegate ((AppDelegate*)[UIApplication sharedApplication].delegate)

and alert as:

UIAlertView *alert_NOT = [[UIAlertView alloc] initWithTitle:@"Not connected!" message:message_to_user delegate:appDelegate cancelButtonTitle:@"Try again" otherButtonTitles: nil];
[alert_NOT show];

Here set delegate as defined keyword, i.e., appDelegate.

Hope this helps.

Upvotes: 0

Sarreph
Sarreph

Reputation: 2015

I'm currently looking into a similar implementation and would like to share an idea I had with you: perhaps using an NSNotification that fires when your delegate's conditions are met, which can be listened for in your VC(s) and handled appropriately, with an alert view, at the top of the stack.

Upvotes: 1

trapper
trapper

Reputation: 11993

You should use the alertViewCancel method for this.

- (void)alertViewCancel:(UIAlertView *)alertView
{
    NSLog(@"text");
}

Upvotes: 0

Related Questions