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

时间:2016-01-20 22:02:34

标签: ios swift segue viewcontroller alertview

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

我需要实现以下功能:

    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

    }
}

这是我的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()

        }

    }
}

1 个答案:

答案 0 :(得分:3)

只需将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)
        }
    }
}