如何通过 AlertView 切换到另一个 ViewController 并执行 segue?
Posted
技术标签:
【中文标题】如何通过 AlertView 切换到另一个 ViewController 并执行 segue?【英文标题】:How do I switch to another ViewController with perform segue via AlertView? 【发布时间】:2016-01-20 22:02:34 【问题描述】:当警报视图出现时如何切换到另一个视图控制器?理想情况下,当我按下“确定”按钮时,我希望它以编程方式更改为另一个视图控制器。
以下功能是我需要实现的:
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
这是我的视图控制器:
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】:只需调用performSegueWithIdentifier(identifier: String, sender: AnyObject?)
函数,您就可以在其中转入一个新的视图控制器。确保您使用与故事板中的 segue 相同的字符串 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)
【讨论】:
警报控制器在没有互联网连接时不再出现,而在它出现之前。我之前的代码是使用“AlertView”设置的。 你能用你试图显示警报的整个班级更新你的原始问题吗? 另外,ios8 中不推荐使用 UIAlertView。如果可以放弃对 iOS7 的支持,最好转到 UIAlertController。 我使用了您提供的代码,但是当它们没有互联网连接时,AlertController 不会出现,而在我使用 AlertView 之前它会出现。 这是项目下载链接,我已经正确实现了所有内容,但仍然没有运气。 gamefront.com/files/25443758/TEST.zip以上是关于如何通过 AlertView 切换到另一个 ViewController 并执行 segue?的主要内容,如果未能解决你的问题,请参考以下文章