0

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

以下功能是我需要实现的:

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

这是我的可达性类:

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($0))
        }

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

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

        return isReachable && !needsConnection

    }
}

这是我的视图控制器:

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()

        }

    }
}
4

1 回答 1

3

只需调用performSegueWithIdentifier(identifier: String, sender: AnyObject?)您想要的函数到一个新的视图控制器。确保使用与identifier故事板中的 segue 相同的字符串。

尝试以下方法:

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)
        }
    }
}
于 2016-01-20T22:15:22.013 回答