对于我正在开发的程序,我创建了一个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:终止调用抛出异常”