如何通过 AlertView 执行 segue 切换到另一个 ViewController?

How do I switch to another ViewController with perform segue via AlertView?

当警报视图出现时,如何切换到另一个视图控制器?理想情况下,当我按下 'OK' 按钮时,我希望它以编程方式更改为另一个视图控制器。

下面的函数是我需要实现的:

    func shouldPerformSegueWithIdentifier(_ identifier: String,
                                   sender sender: AnyObject?) -> Bool

这是我的可达性class:

import Foundation
import SystemConfiguration

public class ReachabilityNotification {

    class func isConnectedToNetwork() -> Bool {

        var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
        zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
        zeroAddress.sin_family = sa_family_t(AF_INET)

        let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
            SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer([=12=]))
        }

        var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
        if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
            return false
        }

        let isReachable = flags == .Reachable
        let needsConnection = flags == .ConnectionRequired

        return isReachable && !needsConnection

    }
}

这是我的 ViewController:

import UIKit
import Foundation

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if Reachability.isConnectedToNetwork() == true {
            print("Internet connection OK")
        } else {
            print("Internet connection FAILED")
            let alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
            alert.show()

        }

    }
}

只需调用 performSegueWithIdentifier(identifier: String, sender: AnyObject?) 函数,您就可以在其中切换到新的视图控制器。确保您使用与故事板中的 segue 相同的字符串 identifier

试试以下方法:

if ReachabilityNotification.isConnectedToNetwork() == true {
    print("Internet connection OK")
} else {
    print("Internet connection FAILED")

    var connectionAlert = UIAlertController(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: UIAlertControllerStyle.Alert)

    connectionAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
      performSegueWithIdentifier("SegueIdentifier", sender: self)
      }))

    presentViewController(connectionAlert, animated: true, completion: nil)
}

编辑:

在流程中使用 viewDidLoad() 还为时过早,无法呈现 UIAlertController。尝试将警报移至 viewDidlAppear.

import UIKit
import Foundation

class ViewController: UIViewController {

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        if Reachability.isConnectedToNetwork() == true {
            print("Internet connection OK")
        } else {
            print("Internet connection FAILED")

            let connectionAlert = UIAlertController(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: UIAlertControllerStyle.Alert)

            connectionAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { [unowned self] (action: UIAlertAction!) in
                self.performSegueWithIdentifier("NoConnection", sender: self)
            }))

            presentViewController(connectionAlert, animated: true, completion: nil)
        }
    }
}