Ios 如何通过AlertView切换到另一个具有perform segue的ViewController?
当警报视图出现时,如何更改为另一个视图控制器?理想情况下,当我按下“确定”按钮时,我希望它以编程方式更改为另一个视图控制器 我需要实现以下功能:Ios 如何通过AlertView切换到另一个具有perform segue的ViewController?,ios,swift,segue,viewcontroller,alertview,Ios,Swift,Segue,Viewcontroller,Alertview,当警报视图出现时,如何更改为另一个视图控制器?理想情况下,当我按下“确定”按钮时,我希望它以编程方式更改为另一个视图控制器 我需要实现以下功能: func shouldPerformSegueWithIdentifier(_ identifier: String, sender sender: AnyObject?) -> Bool 这是我的可达性类: import Foundation import Syst
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()
}
}
}
只需调用
performsguewithidentifier(identifier:String,sender:AnyObject?)
函数,即可切换到新的视图控制器。确保使用与序列图像板中的序列相同的字符串标识符
尝试以下方法:
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
。尝试将警报移动到viewdilaper
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)
}
}
}
只需调用performsguewithidentifier(identifier:String,sender:AnyObject?)
函数,即可切换到新的视图控制器。确保使用与序列图像板中的序列相同的字符串标识符
尝试以下方法:
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
。尝试将警报移动到viewdilaper
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)
}
}
}
警报控制器现在不再在其未连接internet时显示,而在它出现之前则不再显示。我以前的代码是用“AlertView”设置的。您能用您试图显示警报的整个类更新您的原始问题吗?此外,UIAlertView在iOS8中被弃用。如果您可以放弃对iOS7的支持,最好转到UIAlertController。我使用了您提供的代码,但是当他们没有internet连接时,AlertController不会出现,而以前我使用AlertView时,AlertController出现了。这是项目下载链接,我已经正确地实现了所有功能,但仍然不走运。警报控制器现在不再在其未连接internet时显示,而在它出现之前则不再显示。我以前的代码是用“AlertView”设置的。您能用您试图显示警报的整个类更新您的原始问题吗?此外,UIAlertView在iOS8中被弃用。如果您可以放弃对iOS7的支持,最好转到UIAlertController。我使用了您提供的代码,但是当他们没有internet连接时,AlertController不会出现,而以前我使用AlertView时,AlertController出现了。这是项目下载链接,我已经正确地实现了所有功能,但仍然不走运。