在swift3中加载webview时后退按钮卡住了吗?

时间:2017-04-06 05:18:41

标签: ios swift webview

  1. 我已经提交了webview但我按下后退按钮视图控制器没有被解雇。
  2. 当我将取消按钮放在beakpoint上时。它不会被触发。
  3. 这是我的代码:

     //MARK: - View Life Cycle
    
        override func viewDidLoad() {
            super.viewDidLoad()
           loadWebView()
        }
    
        //MARK: - Methods
        func loadWebView() {
            let url = URL(string: "http://www.lipsum.com/")
                //(URL(string: Constant.MedataApi.BaseUrl)?.appendingPathComponent(Constant.termsAndConditions))!
            let request = NSURLRequest.init(url: url! as URL)
            webView.loadRequest(request as URLRequest)
            //webView.layer.borderWidth = 1.0
            //webView.layer.borderColor = UIColor.red.cgColor
        }
    
    //MARK: - IBActions () -        
    
        @IBAction func cancelButtonClicked(_ sender: Any) {
    
            self.dismiss(animated: true, completion: nil)
        }
    

1 个答案:

答案 0 :(得分:0)

  1. 假设您有两个View Controller,一个是FirstViewController,另一个是SecondViewController(webView)。

  2. 在Main.storyboard中,选择FirstViewController并将其嵌入为NavigationController。

  3. 现在将一个Storyboard ID分配给Main.storyboard中的SecondViewController,假设Storyboard ID为“SecondVC”。

  4. 在FirstViewController.swift中添加按钮操作的代码:

    let firstController = self.storyboard?.instantiateViewController(withIdentifier: "SecondVC") as! SecondViewController

    navigationController?.pushViewController(firstController, animated: true)

  5. 然后在SecondViewController.swift文件中添加webView代码。

    override func viewDidLoad() {
    super.viewDidLoad()
    
    // Do any additional setup after loading the view.
    
    let url = URL(string: "http://www.lipsum.com/")
    let request = URLRequest(url: url!)
    webView.loadRequest(request)
    webView.scalesPageToFit = true
    }