0

アラート ビューが表示されたときに別のビュー コントローラーに変更するにはどうすればよいですか? 理想的には、「OK」ボタンを押したときに、プログラムで別のView Controllerに変更したいのです。

次の関数は、実装する必要があるものです。

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

        }

    }
}
4

1 に答える 1

3

performSegueWithIdentifier(identifier: String, sender: AnyObject?)新しいView Controllerにセグエしたい関数を呼び出すだけです。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)
        }
    }
}
于 2016-01-20T22:15:22.013 に答える