0

How do I change to another view controller when the alert view appears? ideally when I press the 'OK' button I want it to programatically change to another view controller.

The following function is what I need to implement:

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

Here is my reachability 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($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

    }
}

Here is my 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 Answers1

3

Just call the performSegueWithIdentifier(identifier: String, sender: AnyObject?) function where you want to segue to a new view controller. Make sure you use the same string identifier as the segue in your storyboard.

Give the following a try:

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

EDIT:

Using viewDidLoad() is too early in the process to present the UIAlertController. Try moving your alert into 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)
        }
    }
}
Mr Beardsley
  • 3,743
  • 22
  • 28
  • The alert controller no longer appears now when their is no internet connection whereas before it appeared. My previous code was set with the 'AlertView'. – Gurvier Singh Dhillon Jan 20 '16 at 22:39
  • Can you update your original question with the entire class where you are trying to show the alert? – Mr Beardsley Jan 20 '16 at 22:43
  • Also, UIAlertView was deprecated in iOS8. It is better to move onto UIAlertController if you can drop support for iOS7. – Mr Beardsley Jan 20 '16 at 22:49
  • I used the code you provided, however when their is no internet connection the AlertController does not appear, whereas before when I used the AlertView it did. – Gurvier Singh Dhillon Jan 20 '16 at 23:12
  • Here is the project download link and I have implemented everything correctly but still no luck. http://www.gamefront.com/files/25443758/TEST.zip – Gurvier Singh Dhillon Jan 20 '16 at 23:31
  • I downloaded your test project and got it working. Since your view controller is the very first one, I needed to move the alert to `viewDidAppear`. I apologize it worked for me in one of my test projects where the view controller I used was not the first. I updated the code above. Try that. Note, your app will crash if you do not have a segue in your storyboard that has the identifier you supply to `performSegueWithIdentifier`. – Mr Beardsley Jan 21 '16 at 01:31
  • Thank you it works great! I have marked this question as answered now cheers. – Gurvier Singh Dhillon Jan 21 '16 at 12:07