2017-04-06 44 views
1
  1. 我已經介紹了webview,但我按回來按鈕視圖控制器並沒有被解僱。
  2. 當我把beakpoint放在取消按鈕上時,它不會被觸發。

這裏是我的代碼:在swift3中加載webview時後退按鈕卡住了嗎?

//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) 
    } 
+0

在您發表評論'loadWebView()''中viewDidLoad方法()',然後按返回鍵會發生什麼,還是它的工作原理? –

+0

你使用'push'或'modal'來轉到'webView'控制器嗎? –

+1

如何異步加載網址? –

回答

0
  1. 假設你有兩個視圖控制器,一個是FirstViewController而另一個是SecondViewController(web視圖)。

  2. 在Main.storyboard中,選擇FirstViewController並將其嵌入爲NavigationController。

  3. 現在爲Main.storyboard中的SecondViewController分配一個Storyboard ID,假設Storyboard ID是「SecondVC」。

  4. 在FirstViewController.swift添加代碼按鈕動作:

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

    navigationController?.pushViewController(firstController, animated: true)

  5. 然後在SecondViewController.swift文件中添加web視圖代碼。

    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 
    } 
    
+0

webview加載其內容但取消按鈕卡住了。當我評論我加載webview的代碼其工作正常.. – akshay

+1

你嘗試添加viewDidLoad()函數中的代碼?你覆蓋它嗎?因爲在loadView()和viewDidLoad()中添加代碼很重要。 –

+0

是啊,我已經做到了這一點,但它不工作 – akshay