【发布时间】:2017-04-05 19:28:17
【问题描述】:
我是 Swift 和 iOS 开发的新手。我一直在尝试为互联网连接创建一个应用程序范围的检查,允许用户重试(重新加载当前视图)。下面的checkInternetConnection() 函数在我的BaseUIViewController 类中的viewWillAppear() 期间被调用。
Reachability.isConnectedToNetwork() 连接检查工作正常。但到目前为止,我还没有找到一种在用户按下警报中的“重试”按钮后重新加载当前视图的方法。事实上,我还没有找到在任何情况下重新加载当前视图的方法。
我知道我在下面的尝试中做错了(因为有一个编译错误告诉我),是我在这一行传递了一个 UIViewController 对象而不是 string 作为参数: self.performSegueWithIdentifier(activeViewController, sender:self),但我怀疑这不是我唯一的错误。
func checkInternetConnection() {
if (Reachability.isConnectedToNetwork()) {
print("Internet connection OK")
} else {
print("Internet connection FAILED")
let alert = UIAlertController(title: NSLocalizedString("Error!", comment: "Connect: error"), message: NSLocalizedString("Could not connect", comment: "Connect: error message"), preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Retry", comment: "Connect: retry"), style: .Default, handler: { action in
print("Connection: Retrying")
let navigationController = UIApplication.sharedApplication().windows[0].rootViewController as! UINavigationController
let activeViewController: UIViewController = navigationController.visibleViewController!
self.performSegueWithIdentifier(activeViewController, sender:self)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
}
【问题讨论】:
-
它的
performSegueWithIdentifier不是performSegueWithViewController所以你必须使用 segue 的标识符(字符串)(你可以在故事板上设置它),或者你可以简单地使用presentViewController来呈现它。 -
谢谢,但我不能在这里输入字符串值,因为我不知道用户在哪个视图上收到了警报(因此,我不知道当他们按“重试”)。它需要是动态的。
-
然后根据你拥有的下一个 vc 创建一个动态字符串并放在那里...?
-
调用
self.presentViewController(activeViewController, animated: true, completion: nil)而不是self.performSegueWithIdentifier(activeViewController, sender:self)在运行时崩溃。 -
@Tj3n 我想试试,但我可能需要一个例子。
标签: ios swift uiviewcontroller segue uialertcontroller