2016-01-20 63 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。 –

相關問題