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()
}
}
}
當警報控制器沒有互聯網連接時,現在不再顯示警報控制器,而是出現之前。我以前的代碼是用'AlertView'設置的。 –
您是否可以在您想要顯示警報的整個班級中更新原始問題? –
另外,UIAlertView在iOS8中已棄用。如果你可以放棄對iOS7的支持,最好轉到UIAlertController。 –