gpt4 book ai didi

ios - 我找不到在 UIAlertView 中按下的按钮

转载 作者:行者123 更新时间:2023-11-29 10:56:03 26 4
gpt4 key购买 nike

对于我正在开发的程序,我创建了一个 Utilities 类,其中包含一个带有取消按钮的警报 View 。我想创建一个 NSLog 在按下取消按钮时向我提供建议。包含alert View 的方法在另一个类中调用,即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: terminate called throwing an exception"

最佳答案

你想要的委托(delegate)方法是...

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

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

如果你改回它应该工作。只要您已正确连接代理。

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

编辑

好的,在进一步的对话之后......

将您的界面更改为...

@interface Utilities : NSObject <UIAlertViewDelegate>

-(BOOL) isOnline;
@end

iPhoneHttpserverViewcontroller的接口(interface)改成...

@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
}

关于ios - 我找不到在 UIAlertView 中按下的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18375307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com