我找不到UIAlertView中按下的按钮

时间:2013-08-22 08:31:46

标签: ios objective-c uialertview

对于我正在开发的程序,我创建了一个包含带取消按钮的警报视图的类Utilities。我想创建一个NSLog,在按下取消按钮时通知我。 包含警报视图的方法在另一个类中调用,即ViewController.m文件,但此时不显示任何日志。你能帮我理解我做错了吗?

这是Utilities.h文件:

@interface Utilities : UIViewController <UIAlertViewDelegate>
    -(BOOL) isOnline;
@end

这是Utilities.m文件:

-(BOOL) isOnline {
    Boolean online = false;
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];
    NetworkStatus status = [reachability currentReachabilityStatus];

    if(status == ReachableViaWiFi) {
        NSLog(@"WI FI");
        online = true;
    } else {
        NSLog(@"NO WI FI");
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                          message:@"You are not connected to a wireless network. Please connect your device."
                                                         delegate:self
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
        [message show];    
    }

    return online;
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex::(NSInteger)buttonIndex
{
    if(buttonIndex==alertView.cancelButtonIndex) {

          NSLog(@"Clicked the button Ok");
    }
}

这就是我在ViewController类中调用方法的地方:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     //Add gradient background
     CAGradientLayer *bgLayer = [BackgroundLayer blueGradient];
 bgLayer.frame = self.view.bounds;
     [self.view.layer insertSublayer:bgLayer atIndex:0];
     [self initialize];
     Utilities* utilities = [[Utilities alloc] init]; //create an istance of the class Utilities for use their methods in this class
     [utilities isOnline]; //call the method isOnline from the class Utilities
}

更新:此处有错误消息,现在返回给我Xcode

  

由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFString alertView:didDismissWithButtonIndex:]:无法识别的选择器发送到实例0x7558670'       ***第一次抛出调用堆栈:       (0x1809012 0x162ee7e 0x18944bd 0x17f8bbc 0x17f894e 0xb30e13 0x778d66 0x778f04 0x2027d8 0x1dd4014 0x1dc47d5 0x17afaf5 0x17aef44 0x17aee1b 0x268f7e3 0x268f668 0x73affc 0x2133 0x20c5)       libc ++ abi.dylib:terminate调用抛出异常“

3 个答案:

答案 0 :(得分:3)

您想要的委托方法是......

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

不确定原因,但您已将方法名称更改为buttonPressed:...

如果你改回来它应该有效。只要您正确连接了代理人。

正如其他地方所述,您可能希望使用alertView:didDismissWithButtonIndex:来避免在警报视图仍然可见时更改UI。

修改

好的,进一步的对话......

将您的界面更改为...

@interface Utilities : NSObject <UIAlertViewDelegate>

    -(BOOL) isOnline;
@end

iPhoneHttpserverViewcontroller的界面更改为...

@property (nonatomic, strong) Utilities *utilities;

并将viewWillAppear更改为...

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     //Add gradient background
     CAGradientLayer *bgLayer = [BackgroundLayer blueGradient];
 bgLayer.frame = self.view.bounds;
     [self.view.layer insertSublayer:bgLayer atIndex:0];
     [self initialize];

     // I've edited these next two lines to use the property.
     self.utilities = [[Utilities alloc] init]; //create an istance of the class Utilities for use their methods in this class
     [self.utilities isOnline]; //call the method isOnline from the class Utilities
}

答案 1 :(得分:1)

buttonPressed:clickedButtonAtIndex:

不是委托方法,正确的方法是

alertView:clickedButtonAtIndex:

然而,通常人们使用

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

因为您不希望在警报仍然可见时执行其他UI操作。

还要确保使用以下内容进行索引比较

if (buttonIndex == alert.cancelButtonIndex) { ...

修改

由于utilities被用作委托,您必须保留它们,否则它们将被取消分配

@interface ViewController : ... ()

@property (nonatomic, strong, readwrite) UIUtilities* utilities.

@end

...

self.utilities = [[Utilities alloc] init];
[self.utilities isOnline]

答案 2 :(得分:-2)

请使用自定义提醒视图并添加“取消”或“确定”之类的按钮。所以你可以实现你的目标。对于

[button addTarget:self action:@selector(selector:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];