首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >alertView:(UIAlertView *)应用程序委托中不工作的alertView方法

alertView:(UIAlertView *)应用程序委托中不工作的alertView方法
EN

Stack Overflow用户
提问于 2014-06-23 02:50:49
回答 3查看 3.8K关注 0票数 4

我在AppDelegate.m文件中设置了UIAlertView。

但是当我选择警报视图上的按钮时。

代码语言:javascript
复制
-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex

不起作用。

我在AppDelegate.h文件中设置了UIAlertViewDelegate。

和我的助理代表。

代码语言:javascript
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:        (NSDictionary *)launchOptions
        {

         [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];

            NSString *remoteHostName = REACHABILITY_HOST;
            _hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
            [_hostReachability startNotifier];

            return YES;
        }

        - (void) reachabilityChanged:(NSNotification *)note
        {
         if( [Reachability isExistNetwork] == NotReachable)
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil         message:@"my message" delegate:nil cancelButtonTitle:@"ok"         otherButtonTitles:@"set",nil];
                [alertView show];

            }

        }

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

            switch (buttonIndex) {

                case 0:
                    NSLog(@"ok!");
                    break;

                    // set
                case 1:

                    NSLog(@"set!");
                    NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"];
                    [[UIApplication sharedApplication] openURL:url];
                    break;

            }
        }

但是

代码语言:javascript
复制
-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex

没有输入此方法。

有人知道发生了什么吗?谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-06-23 02:54:32

您的代码没有正确设置警报视图的委托。

您需要更改以下行,以便委托是适当的对象(例如,self):

代码语言:javascript
复制
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil         message:@"my message" delegate:nil cancelButtonTitle:@"ok"         otherButtonTitles:@"set",nil];
票数 9
EN

Stack Overflow用户

发布于 2014-06-23 04:42:49

你必须更换线路

代码语言:javascript
复制
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"my message" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:@"set",nil];
[alertView show];

由这一行

代码语言:javascript
复制
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"my message" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"set",nil];
[alertView show];

要调用委托方法,必须将委托设置为self。

票数 2
EN

Stack Overflow用户

发布于 2014-06-23 04:25:38

检查.h文件中的委托引用。戴上UIAlertViewDelegate

代码语言:javascript
复制
@interface YourViewController : UIViewController<UIAlertViewDelegate>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24357810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档