【问题标题】:Wait UIAlertView Button等待 UIAlertView 按钮
【发布时间】:2012-10-25 16:14:58
【问题描述】:

我的应用程序中有此代码:

if (self.uiSwtichState == TRUE)
        {
            if ([CLLocationManager locationServicesEnabled])
            {
                [self.textString stringByAppendingString:[[[SMSLocationModel alloc] init] getLocation]];

            }
        }
            MFMessageComposeViewController *sms = [[MFMessageComposeViewController alloc] init];
sms.messageComposeDelegate = self;
sms.body = self.textString;
sms.recipients = self.arrayOfNumbers;
[self presentModalViewController:sms animated:YES];

这段代码被放入一个名为“send”的方法中。 可以想象,getLocation 方法获取当前用户位置,并且在应用程序第一次运行时,屏幕上会出现一个 UIAlertView,询问用户是否允许应用程序获取他的位置。 问题是当这个 UIAlertView 出现时,她立即被短信 Vie Controller (MFMessageComposeViewController) 取代,这样用户就没有足够的时间在 UIAlertView 中回答 YES 或 NO。

有一种方法可以等待用户按下 UIAlertView 中的按钮,然后呈现 ModalViewController?

谢谢!

【问题讨论】:

    标签: iphone objective-c location uialertview wait


    【解决方案1】:

    您可以使用 CLLocationManagers 委托方法等到授权状态​​更改。如果您无权使用位置服务,请将 sms viewController 保存到实例变量中。然后请位置经理授权您。如果授权状态发生更改,则显示该视图控制器。

    // somewhere else
    self.locationManager.delegate = self;
    
    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
        NSLog(@"New Authorization State: %d", status);
        if (self.viewControllerToPresentAfterLocationDidChange) {
            if (status == kCLAuthorizationStatusAuthorized) {
                // set location of viewController
                [self presentViewController:self.viewControllerToPresentAfterLocationDidChange animated:YES completion:NULL];
            }
            else {
                // user did not authorize you to use location services
            }
            self.viewControllerToPresentAfterLocationDidChange = nil;
        }
    }
    
    
    - (IBAction)buttonPressed:(id)sender {
        UIViewController *vc = [[UIViewController alloc] init];
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
            // authorized already, no alert will appear.
    
            // set location of vc
            [self presentViewController:vc animated:YES completion:NULL];
        }
        else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
            self.viewControllerToPresentAfterLocationDidChange = vc;
            // access location so alert will show
        }
        else {
            // not authorized or restricted.
        }
    }
    

    【讨论】:

      【解决方案2】:

      也许你可以使用这个委托:

      - (void)didPresentAlertView:(UIAlertView *)alertView{
      }
      

      例如,初始化一个布尔值可以防止显示此模式视图控制器。然后如果关闭,则更改布尔值以允许呈现此视图控制器。

      【讨论】:

        【解决方案3】:

        是的,您可以实现AlertViewDelegate
        请参阅UIAlertViewDelegate Documentation 了解更多信息。

        然后您可以使用alertView:clickedButtonAtIndex: 方法并在用户单击警报视图时显示您的MessageComposer

        【讨论】:

        • 您不能将自己设置为 iOS 系统警报的代表(例如请求许可的代表)。如果没有委托,所有这些委托方法都不会被调用。
        猜你喜欢
        • 2013-08-02
        • 2013-03-31
        • 2013-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多