2016-01-20 38 views
0

当警报视图出现时,如何更改为另一个视图控制器?理想情况下,当我按下“确定”按钮时,我希望它以编程方式更改为另一个视图控制器。如何通过AlertView切换到执行segue的另一个ViewController?

下面的功能是什么,我需要实现:

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

     } 

    } 
} 

回答

3

只要打电话给performSegueWithIdentifier(identifier: String, sender: AnyObject?)函数你想要继续到一个新的视图控制器。确保在故事板中使用与字符串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) 
     } 
    } 
} 
+0

当警报控制器没有互联网连接时,现在不再显示警报控制器,而是出现之前。我以前的代码是用'AlertView'设置的。 –

+0

您是否可以在您想要显示警报的整个班级中更新原始问题? –

+0

另外,UIAlertView在iOS8中已弃用。如果你可以放弃对iOS7的支持,最好转到UIAlertController。 –

相关问题