尝试展示<a> on </a> <a> while a presentation is in progress

时间:2018-02-27 12:48:48

标签: swift xcode segue swift4

i know this has been asked before but i'm trying to figure out the issue with my project, as the title states (a) is trying to present on (a) i have checked all segue triggers to see if i accidentally set a segue to go to the same view controller that it is already on but this is not the case.

view controller 1 code

import UIKit
import UserNotifications

class NotificationViewController: UIViewController {

    let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications

    let current = UNUserNotificationCenter.current()

    @IBAction func Notification(_ sender: Any) {

        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in})

        var i = 0

        while i < 1{

        current.getNotificationSettings(completionHandler: { (settings) in
            if settings.authorizationStatus == .notDetermined {
                // Notification permission has not been asked yet, go for it!
            }

            if settings.authorizationStatus == .denied {
                i = i + 1
                DispatchQueue.main.async {
                    self.performSegue(withIdentifier: "ToLocation", sender: self)
                    // Notification permission was previously denied, go to settings & privacy to re-enable
                }

            }

            if settings.authorizationStatus == .authorized {
                i = i + 1
                DispatchQueue.main.async {
                 self.performSegue(withIdentifier: "ToLocation", sender: self)
                    // Notification permission was already granted
                }

            }
        })
        }

        }

    override func viewDidLoad() {

        current.getNotificationSettings(completionHandler: { (settings) in
            if settings.authorizationStatus == .notDetermined {
                // Notification permission has not been asked yet, go for it!
            }

            if settings.authorizationStatus == .denied {
                DispatchQueue.main.async {
                    self.performSegue(withIdentifier: "ToLocation", sender: self)
                    // Notification permission was previously denied, go to settings & privacy to re-enable
                }

            }

            if settings.authorizationStatus == .authorized {
                DispatchQueue.main.async {
                    self.performSegue(withIdentifier: "ToLocation", sender: self)
                    // Notification permission was already granted
                }

            }
        })

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

view controller 2 code

    import UIKit
import CoreLocation

class LocationViewController: UIViewController {

    @IBOutlet weak var textview: UITextView!

    let locationManager = CLLocationManager()

    @IBAction func OnLocation(_ sender: Any) {

        locationManager.delegate = self as? CLLocationManagerDelegate

        var i = 0

        while i < 1{

            switch CLLocationManager.authorizationStatus() {
            case .notDetermined:
                // Request when-in-use authorization initially
                locationManager.requestWhenInUseAuthorization()
                break

            case .restricted, .denied:
                i = i + 1
                DispatchQueue.main.async {
                    self.performSegue(withIdentifier: "ToLogin", sender: self)
                }
                // Disable location features
                //disableMyLocationBasedFeatures()
                break

            case .authorizedWhenInUse:
                i = i + 1
                DispatchQueue.main.async {
                    self.performSegue(withIdentifier: "ToLogin", sender: self)
                }
                // Enable basic location features
                //enableMyWhenInUseFeatures()
                break

            case .authorizedAlways:
                // Enable any of your app's location features
               // enableMyAlwaysFeatures()
                break
            }
        }
    }


    override func viewDidLoad() {
        func locationManager(_ manager: CLLocationManager,
                             didChangeAuthorization status: CLAuthorizationStatus) {   switch status {

        case .restricted, .denied:
            self.performSegue(withIdentifier: "ToLogin", sender: self)
            break

        case .authorizedWhenInUse:
            self.performSegue(withIdentifier: "ToLogin", sender: self)
            break

        case .authorizedAlways:
            self.performSegue(withIdentifier: "ToLogin", sender: self)
            break

        case .notDetermined:
            break
            }
        }
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

error message

018-02-27 23:06:41.534749+1030 Request[27358:1351259] Warning: Attempt to present on while a presentation is in progress!

2 个答案:

答案 0 :(得分:0)

您的错误消息非常明确while a presentation is in progress。这意味着您尝试从另一个LocationViewController展示LocationViewController,但它仍未提供。

将您的演示逻辑移至viewWillAppear方法。它会有所帮助

<强>更新

1)将现有的segue从通知按钮移至故事板中的LocationViewController

2)将NotificationViewController中的segue添加到LocationViewController,如图所示 enter image description here

3)设置此segue的名称 enter image description here

现在尝试运行你的项目

答案 1 :(得分:0)

不不不不不。这不是方法。对于UNUserNotification 中的 completionHandler 方法的 completionHandler 结果参数,很抱歉在后台线程上执行。

此功能的提示

@IBAction func Notification(_ sender: Any) 

*应以小写

开头

*提供一个名称,表明其完成的操作可以通过该名称推断

*更改发件人的类型(如果您知道的话),这样您可以在不进行强制转换的情况下显式调用该对象的属性或方法。

继续执行函数范围内的其余代码。 requestAuthorization 负有此唯一责任 - 请求权限 - 响应为 didAllow 错误。你永远不会检查这个并继续启动另一个块,响应也是另一个线程

底线:您在循环中调用 requestAuthorization 然后 getNotificationSettings (为什么?),如果此代码执行30%的时间,您将获得很多好运。

所以你应该分开一些涉及权限的代码,阅读一些关于GRASP原则的内容,同时阅读每一章here