Ios 从Swift 2.3中的UIAlertView导航到UIViewController

Ios 从Swift 2.3中的UIAlertView导航到UIViewController,ios,swift,uiviewcontroller,segue,uialertcontroller,Ios,Swift,Uiviewcontroller,Segue,Uialertcontroller,我是Swift和iOS开发的新手。我一直在尝试创建一个应用程序范围的互联网连接检查,允许用户重试(重新加载当前视图)。下面的checkInternetConnection()函数是在myBaseUIViewController类中的viewWillDisplay()期间调用的 可达性.isconnectedtonework()连接检查工作正常。但到目前为止,我还没有找到在用户按下警报中的“重试”按钮后重新加载当前视图的方法。事实上,在任何情况下,我都无法找到重新加载当前视图的方法 我知道在下面的

我是Swift和iOS开发的新手。我一直在尝试创建一个应用程序范围的互联网连接检查,允许用户重试(重新加载当前视图)。下面的
checkInternetConnection()
函数是在my
BaseUIViewController
类中的
viewWillDisplay()
期间调用的

可达性.isconnectedtonework()
连接检查工作正常。但到目前为止,我还没有找到在用户按下警报中的“重试”按钮后重新加载当前视图的方法。事实上,在任何情况下,我都无法找到重新加载当前视图的方法

我知道在下面的尝试中我做错了一件事(因为有一个编译错误告诉我这样做),那就是我将
UIViewController
对象作为参数传递,而不是此行的
string
self.performsguewithidentifier(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)
    }
}

BaseUIViewController
可以执行应用程序范围的连接检查,因此您的所有viewcontroller都将继承自
BaseUIViewController

在连接检查之后,每个ViewController将具有不同的行为

您可以做的一件事是,在
BaseUIViewController
中定义一个块,在连接检查失败后执行操作

下面是一个例子:

class BaseViewUIController: UIViewController {

    var connectivityBlock: (() -> Void)?

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        checkInternetConnection()
    }


    func checkInternetConnection() {
        if Reachability.isConnectedToNetwork() {
            print("Internet connection OK")
        } else {
            if let b = connectivityBlock {
                b()
            }
        }
    }
}
在继承的ViewController中:

class MyViewController: BaseUIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        connectivityBlock = {
            print("Do View Refresh Here.")
        }
    }
}
然后,在签入
BaseUIViewController
后,将执行
connectivityBlock
中的代码,以便每个VC可以对其进行不同的处理


您还可以在
BaseUIViewController
中定义一个函数,每个子类将覆盖该函数,在连接检查失败后,调用该函数。
BaseUIViewController
可以执行应用程序范围内的连接检查,因此,所有ViewController都将从
BaseUIViewController
继承

在连接检查之后,每个ViewController将具有不同的行为

您可以做的一件事是,在
BaseUIViewController
中定义一个块,在连接检查失败后执行操作

下面是一个例子:

class BaseViewUIController: UIViewController {

    var connectivityBlock: (() -> Void)?

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        checkInternetConnection()
    }


    func checkInternetConnection() {
        if Reachability.isConnectedToNetwork() {
            print("Internet connection OK")
        } else {
            if let b = connectivityBlock {
                b()
            }
        }
    }
}
在继承的ViewController中:

class MyViewController: BaseUIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        connectivityBlock = {
            print("Do View Refresh Here.")
        }
    }
}
然后,在签入
BaseUIViewController
后,将执行
connectivityBlock
中的代码,以便每个VC可以对其进行不同的处理


您还可以在
BaseUIViewController
中定义一个函数,每个子类将覆盖该函数,在连接检查失败后,调用该函数。

尝试使用应用程序的窗口导航

let destinationVC  = UIApplication.shared.delegate?.window
destinationVC .present(activeViewController, animated: true, completion: nil)

尝试使用应用程序的窗口导航

let destinationVC  = UIApplication.shared.delegate?.window
destinationVC .present(activeViewController, animated: true, completion: nil)


它的
performsguewithidentifier
不是
performsguewithviewcontroller
,因此您必须使用segue的标识符(字符串)(您可以在故事板上设置),或者您可以简单地使用
presentViewController
来显示它。谢谢,但我不能在这里键入字符串值,因为我不知道用户在哪个视图上收到了警报(因此,我不知道当用户按“重试”时向用户显示哪个视图)。它需要是动态的。然后根据你拥有的下一个vc创建一个动态字符串并放在那里…?调用
self.presentViewController(activeViewController,动画:true,完成:nil)
而不是
self.performsgueWithIdentifier(activeViewController,发送者:self)
在运行时崩溃。@Tj3n我想试试看,但是我可能需要一个例子。它的
performsguewithidentifier
不是
performsguewithviewcontroller
,所以你必须使用segue的标识符(字符串)(你可以在故事板上设置),或者你可以简单地使用
presentViewController
来显示它。谢谢,但我不能在这里键入字符串值,因为我不知道用户在哪个视图上收到了警报(因此,我不知道当用户按“重试”时向用户显示哪个视图)。它需要是动态的。然后根据你拥有的下一个vc创建一个动态字符串并放在那里…?调用
self.presentViewController(activeViewController,动画:true,完成:nil)
而不是
self.performsgueWithIdentifier(activeViewController,发送者:self)
在运行时崩溃。@Tj3n我想试试看,但我可能需要一个例子。谢谢你的建议。我不确定每个ViewController需要不同的行为,但我想这不会有什么坏处。但是,我收到一个编译错误,无法在我的
testingViewController:BaseUIViewController
中的
connectivityBlock()
的定义中将类型为“testingViewController.type”的值转换为预期的参数类型“UIViewController”。定义包含一行:
self.presentViewController(testingViewController,动画:false,completion:nil)
并直接写在
super.viewDidLoad()
的下面,就像您的示例一样。简言之,您的建议可能是合理的,但我原来的问题仍然存在。我需要重新加载当前的ViewController。这应该在你已经写了
print(“Do View Refresh Here.”)的地方完成。
你是正确的,你可以在块中重新加载当前VC,就好像你正在正常地重新加载/刷新你的视图一样。是的,如果我想重新加载整个当前VC,你能把它添加到你发布的代码中吗?对不起,我没有完全理解“重新加载整个当前VC“,您可以在代码块中以任何方式重新加载VC的视图,即显示网络错误视图。感谢您的建议。”。我不确定每个ViewController需要不同的行为,但我想这不会有什么坏处。但是,我收到一个编译错误,无法将“testingViewController.type”类型的值转换为定义o处的预期参数类型“UIViewController”