2

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..

4

4 回答 4

1

我目前正在研究类似的实现,并想与您分享一个想法:也许使用 NSNotification 在满足您的委托条件时触发,可以在您的 VC 中侦听并适当处理,与堆栈顶部的警报视图。

于 2012-12-27T09:33:32.590 回答
1
@interface urAppDelegate : NSObject <UIApplicationDelegate,UIAlertViewDelegate>

如果你合成了alert_not然后像这样使用它自己:

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];
于 2011-12-22T08:52:54.317 回答
0

您应该为此使用 alertViewCancel 方法。

- (void)alertViewCancel:(UIAlertView *)alertView
{
    NSLog(@"text");
}
于 2011-12-22T10:07:59.773 回答
0

定义如下:

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

并警告为:

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

这里设置delegate为定义的关键字,即appDelegate。

希望这可以帮助。

于 2016-02-04T14:03:59.263 回答