1

对于我正在开发的程序,我创建了一个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 的错误消息

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString alertView:didDismissWithButtonIndex:]: unrecognized selector sent to instance 0x7558670' *** First throw call stack: (0x1809012 0x162ee7e 0x18944bd 0x17f8bbc 0x17f894e 0xb30e13 0x778d66 0x778f04 0x2027d8 0x1dd4014 0x1dc47d5 0x17afaf5 0x17aef44 0x17aee1b 0x268f7e3 0x268f668 0x73affc 0x2133 0x20c5) libc++abi.dylib:终止调用抛出异常”

4

3 回答 3

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
}
于 2013-08-22T08:34:21.770 回答
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]
于 2013-08-22T08:34:25.443 回答
-2

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

[button addTarget:self action:@selector(selector:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
于 2013-08-22T08:50:06.773 回答