检测视图是否因为按下后退按钮而加载

时间:2017-05-26 12:44:57

标签: ios swift uinavigationcontroller

是否可以检测是否因为按下后退按钮而出现视图。例如,使用此层次结构VC1 -> webViewController -> VC3,如果在VC3我按下后退按钮,我想在webViewController出现时检测到。我的目标是在shouldStartLoadWith方法中使用它,以避免在按下后退按钮时刷新页面。

2 个答案:

答案 0 :(得分:0)

请在webViewController

中使用此功能
  - (void)webViewDidFinishLoad:(UIWebView *)webView
     {
        //This Check is for if still webview is Loading the page or content
        if (webView.isLoading) {
        NSLog(@"your webview is still loading");
        return;
}
    else {
        //this is for when its finish loading
        NSLog(@"your webview is loaded completely");
}
     }

答案 1 :(得分:0)

您可以使用closure进行此类回调。

示例

class WebViewController: UIViewController
{
    @IBAction func onTapButton(_ sender: UIButton)
    {
        let controller = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "VC3") as! VC3
        controller.completionHandler = {
            //Code to execute after back button is pressed.
        }
        self.navigationController?.pushViewController(controller, animated: true)
    }
}

class VC3: UIViewController
{
    var completionHandler : (()->())?

    override func viewWillDisappear(_ animated: Bool)
    {
        if self.navigationController?.viewControllers.index(of: self) == nil
        {
            //Back Button pressed
            self.completionHandler?()
        }
        super.viewWillDisappear(animated)
    }
}