Give Value to UIWebkit and change instantiate ViewController [Swift 4]












2















I have a tableView in my first ViewController. One of its functions is this one



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row (indexPath.row) selected")

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
self.navigationController?.pushViewController(webViewController, animated: true)
}


As you can see I want to change the viewController when an row is selected and based on the selection I want to present a Website on the next ViewController. I don't know how to pass the row value to the next ViewController or set the webView in this function.



I want to open my urls like this, but probably in a if-else statement for each row one url.



@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

let url = URL(string:"https://www.google.com")
let request = URLRequest(url: url!)

angebotWebView.load(request)

}


Thanks for your help










share|improve this question

























  • how are you loading the url in your current implementation of webViewController?

    – hardik parmar
    Nov 23 '18 at 12:34











  • @hardikparmar updated

    – Thomas22
    Nov 23 '18 at 12:39











  • I've added my answer. Please check

    – hardik parmar
    Nov 23 '18 at 12:48
















2















I have a tableView in my first ViewController. One of its functions is this one



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row (indexPath.row) selected")

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
self.navigationController?.pushViewController(webViewController, animated: true)
}


As you can see I want to change the viewController when an row is selected and based on the selection I want to present a Website on the next ViewController. I don't know how to pass the row value to the next ViewController or set the webView in this function.



I want to open my urls like this, but probably in a if-else statement for each row one url.



@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

let url = URL(string:"https://www.google.com")
let request = URLRequest(url: url!)

angebotWebView.load(request)

}


Thanks for your help










share|improve this question

























  • how are you loading the url in your current implementation of webViewController?

    – hardik parmar
    Nov 23 '18 at 12:34











  • @hardikparmar updated

    – Thomas22
    Nov 23 '18 at 12:39











  • I've added my answer. Please check

    – hardik parmar
    Nov 23 '18 at 12:48














2












2








2








I have a tableView in my first ViewController. One of its functions is this one



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row (indexPath.row) selected")

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
self.navigationController?.pushViewController(webViewController, animated: true)
}


As you can see I want to change the viewController when an row is selected and based on the selection I want to present a Website on the next ViewController. I don't know how to pass the row value to the next ViewController or set the webView in this function.



I want to open my urls like this, but probably in a if-else statement for each row one url.



@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

let url = URL(string:"https://www.google.com")
let request = URLRequest(url: url!)

angebotWebView.load(request)

}


Thanks for your help










share|improve this question
















I have a tableView in my first ViewController. One of its functions is this one



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row (indexPath.row) selected")

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
self.navigationController?.pushViewController(webViewController, animated: true)
}


As you can see I want to change the viewController when an row is selected and based on the selection I want to present a Website on the next ViewController. I don't know how to pass the row value to the next ViewController or set the webView in this function.



I want to open my urls like this, but probably in a if-else statement for each row one url.



@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

let url = URL(string:"https://www.google.com")
let request = URLRequest(url: url!)

angebotWebView.load(request)

}


Thanks for your help







ios swift xcode






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 12:39







Thomas22

















asked Nov 23 '18 at 12:30









Thomas22Thomas22

1108




1108













  • how are you loading the url in your current implementation of webViewController?

    – hardik parmar
    Nov 23 '18 at 12:34











  • @hardikparmar updated

    – Thomas22
    Nov 23 '18 at 12:39











  • I've added my answer. Please check

    – hardik parmar
    Nov 23 '18 at 12:48



















  • how are you loading the url in your current implementation of webViewController?

    – hardik parmar
    Nov 23 '18 at 12:34











  • @hardikparmar updated

    – Thomas22
    Nov 23 '18 at 12:39











  • I've added my answer. Please check

    – hardik parmar
    Nov 23 '18 at 12:48

















how are you loading the url in your current implementation of webViewController?

– hardik parmar
Nov 23 '18 at 12:34





how are you loading the url in your current implementation of webViewController?

– hardik parmar
Nov 23 '18 at 12:34













@hardikparmar updated

– Thomas22
Nov 23 '18 at 12:39





@hardikparmar updated

– Thomas22
Nov 23 '18 at 12:39













I've added my answer. Please check

– hardik parmar
Nov 23 '18 at 12:48





I've added my answer. Please check

– hardik parmar
Nov 23 '18 at 12:48












3 Answers
3






active

oldest

votes


















2














Create a String Variable in Your webViewController:



var urlString: String = ""  // here

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

let url = URL(string: urlString) // put here
let request = URLRequest(url: url!)
angebotWebView.load(request)
OR
if let urlString = urlString, let url = URL(string: urlString) {
let request = URLRequest(url: url)
angebotWebView.load(request)
}



}


And in your this method:-



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row (indexPath.row) selected")

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
webViewController. urlString = yourUrlArray[indexPath.row] // call it here
self.navigationController?.pushViewController(webViewController, animated: true)
}





share|improve this answer





















  • 2





    You made my day Sir. Thank you very much :)

    – Thomas22
    Nov 23 '18 at 12:55











  • The force unwrapping is not a good practice in general. (url! in this code)

    – hardik parmar
    Nov 23 '18 at 13:00











  • yes it is updated by using your code ;D

    – wings
    Nov 23 '18 at 13:11



















1














You can get the url out of the viewDidLoad and then change it's value when you are creating the instance in the didSelect like this:



@IBOutlet weak var webView: WKWebView!

var urlString:String?

override func viewDidLoad() {
super.viewDidLoad()

if let urlString = urlString, let url = URL(string: urlString) {
let request = URLRequest(url: url)
angebotWebView.load(request)
}

}


in the tableView's didSelect method:



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row (indexPath.row) selected")

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
webViewController.urlString = tableViewDataSource[indexPath.row]
self.navigationController?.pushViewController(webViewController, animated: true)
}


Here, the tableViewDataSource is the source you are using to display the list.






share|improve this answer

































    0














    First your need to create var in your webViewController e.g



    var url: String?


    then call webViewController like below



    if let vc = storyboard.instantiateViewController(withIdentifier: "webViewController") as? webViewController {
    vc.url = //indexPath.row
    self.navigationController?.pushViewController(vc, animated: true)

    }





    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446766%2fgive-value-to-uiwebkit-and-change-instantiate-viewcontroller-swift-4%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Create a String Variable in Your webViewController:



      var urlString: String = ""  // here

      @IBOutlet weak var webView: WKWebView!

      override func viewDidLoad() {
      super.viewDidLoad()

      let url = URL(string: urlString) // put here
      let request = URLRequest(url: url!)
      angebotWebView.load(request)
      OR
      if let urlString = urlString, let url = URL(string: urlString) {
      let request = URLRequest(url: url)
      angebotWebView.load(request)
      }



      }


      And in your this method:-



      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      print("Row (indexPath.row) selected")

      let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

      let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
      webViewController. urlString = yourUrlArray[indexPath.row] // call it here
      self.navigationController?.pushViewController(webViewController, animated: true)
      }





      share|improve this answer





















      • 2





        You made my day Sir. Thank you very much :)

        – Thomas22
        Nov 23 '18 at 12:55











      • The force unwrapping is not a good practice in general. (url! in this code)

        – hardik parmar
        Nov 23 '18 at 13:00











      • yes it is updated by using your code ;D

        – wings
        Nov 23 '18 at 13:11
















      2














      Create a String Variable in Your webViewController:



      var urlString: String = ""  // here

      @IBOutlet weak var webView: WKWebView!

      override func viewDidLoad() {
      super.viewDidLoad()

      let url = URL(string: urlString) // put here
      let request = URLRequest(url: url!)
      angebotWebView.load(request)
      OR
      if let urlString = urlString, let url = URL(string: urlString) {
      let request = URLRequest(url: url)
      angebotWebView.load(request)
      }



      }


      And in your this method:-



      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      print("Row (indexPath.row) selected")

      let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

      let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
      webViewController. urlString = yourUrlArray[indexPath.row] // call it here
      self.navigationController?.pushViewController(webViewController, animated: true)
      }





      share|improve this answer





















      • 2





        You made my day Sir. Thank you very much :)

        – Thomas22
        Nov 23 '18 at 12:55











      • The force unwrapping is not a good practice in general. (url! in this code)

        – hardik parmar
        Nov 23 '18 at 13:00











      • yes it is updated by using your code ;D

        – wings
        Nov 23 '18 at 13:11














      2












      2








      2







      Create a String Variable in Your webViewController:



      var urlString: String = ""  // here

      @IBOutlet weak var webView: WKWebView!

      override func viewDidLoad() {
      super.viewDidLoad()

      let url = URL(string: urlString) // put here
      let request = URLRequest(url: url!)
      angebotWebView.load(request)
      OR
      if let urlString = urlString, let url = URL(string: urlString) {
      let request = URLRequest(url: url)
      angebotWebView.load(request)
      }



      }


      And in your this method:-



      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      print("Row (indexPath.row) selected")

      let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

      let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
      webViewController. urlString = yourUrlArray[indexPath.row] // call it here
      self.navigationController?.pushViewController(webViewController, animated: true)
      }





      share|improve this answer















      Create a String Variable in Your webViewController:



      var urlString: String = ""  // here

      @IBOutlet weak var webView: WKWebView!

      override func viewDidLoad() {
      super.viewDidLoad()

      let url = URL(string: urlString) // put here
      let request = URLRequest(url: url!)
      angebotWebView.load(request)
      OR
      if let urlString = urlString, let url = URL(string: urlString) {
      let request = URLRequest(url: url)
      angebotWebView.load(request)
      }



      }


      And in your this method:-



      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      print("Row (indexPath.row) selected")

      let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

      let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
      webViewController. urlString = yourUrlArray[indexPath.row] // call it here
      self.navigationController?.pushViewController(webViewController, animated: true)
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 23 '18 at 13:32

























      answered Nov 23 '18 at 12:45









      wingswings

      1,123524




      1,123524








      • 2





        You made my day Sir. Thank you very much :)

        – Thomas22
        Nov 23 '18 at 12:55











      • The force unwrapping is not a good practice in general. (url! in this code)

        – hardik parmar
        Nov 23 '18 at 13:00











      • yes it is updated by using your code ;D

        – wings
        Nov 23 '18 at 13:11














      • 2





        You made my day Sir. Thank you very much :)

        – Thomas22
        Nov 23 '18 at 12:55











      • The force unwrapping is not a good practice in general. (url! in this code)

        – hardik parmar
        Nov 23 '18 at 13:00











      • yes it is updated by using your code ;D

        – wings
        Nov 23 '18 at 13:11








      2




      2





      You made my day Sir. Thank you very much :)

      – Thomas22
      Nov 23 '18 at 12:55





      You made my day Sir. Thank you very much :)

      – Thomas22
      Nov 23 '18 at 12:55













      The force unwrapping is not a good practice in general. (url! in this code)

      – hardik parmar
      Nov 23 '18 at 13:00





      The force unwrapping is not a good practice in general. (url! in this code)

      – hardik parmar
      Nov 23 '18 at 13:00













      yes it is updated by using your code ;D

      – wings
      Nov 23 '18 at 13:11





      yes it is updated by using your code ;D

      – wings
      Nov 23 '18 at 13:11













      1














      You can get the url out of the viewDidLoad and then change it's value when you are creating the instance in the didSelect like this:



      @IBOutlet weak var webView: WKWebView!

      var urlString:String?

      override func viewDidLoad() {
      super.viewDidLoad()

      if let urlString = urlString, let url = URL(string: urlString) {
      let request = URLRequest(url: url)
      angebotWebView.load(request)
      }

      }


      in the tableView's didSelect method:



      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      print("Row (indexPath.row) selected")

      let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

      let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
      webViewController.urlString = tableViewDataSource[indexPath.row]
      self.navigationController?.pushViewController(webViewController, animated: true)
      }


      Here, the tableViewDataSource is the source you are using to display the list.






      share|improve this answer






























        1














        You can get the url out of the viewDidLoad and then change it's value when you are creating the instance in the didSelect like this:



        @IBOutlet weak var webView: WKWebView!

        var urlString:String?

        override func viewDidLoad() {
        super.viewDidLoad()

        if let urlString = urlString, let url = URL(string: urlString) {
        let request = URLRequest(url: url)
        angebotWebView.load(request)
        }

        }


        in the tableView's didSelect method:



        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Row (indexPath.row) selected")

        let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
        webViewController.urlString = tableViewDataSource[indexPath.row]
        self.navigationController?.pushViewController(webViewController, animated: true)
        }


        Here, the tableViewDataSource is the source you are using to display the list.






        share|improve this answer




























          1












          1








          1







          You can get the url out of the viewDidLoad and then change it's value when you are creating the instance in the didSelect like this:



          @IBOutlet weak var webView: WKWebView!

          var urlString:String?

          override func viewDidLoad() {
          super.viewDidLoad()

          if let urlString = urlString, let url = URL(string: urlString) {
          let request = URLRequest(url: url)
          angebotWebView.load(request)
          }

          }


          in the tableView's didSelect method:



          func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          print("Row (indexPath.row) selected")

          let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

          let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
          webViewController.urlString = tableViewDataSource[indexPath.row]
          self.navigationController?.pushViewController(webViewController, animated: true)
          }


          Here, the tableViewDataSource is the source you are using to display the list.






          share|improve this answer















          You can get the url out of the viewDidLoad and then change it's value when you are creating the instance in the didSelect like this:



          @IBOutlet weak var webView: WKWebView!

          var urlString:String?

          override func viewDidLoad() {
          super.viewDidLoad()

          if let urlString = urlString, let url = URL(string: urlString) {
          let request = URLRequest(url: url)
          angebotWebView.load(request)
          }

          }


          in the tableView's didSelect method:



          func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          print("Row (indexPath.row) selected")

          let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

          let webViewController = mainStoryboard.instantiateViewController(withIdentifier: "webViewController") as! webViewController
          webViewController.urlString = tableViewDataSource[indexPath.row]
          self.navigationController?.pushViewController(webViewController, animated: true)
          }


          Here, the tableViewDataSource is the source you are using to display the list.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 12:54

























          answered Nov 23 '18 at 12:45









          hardik parmarhardik parmar

          707415




          707415























              0














              First your need to create var in your webViewController e.g



              var url: String?


              then call webViewController like below



              if let vc = storyboard.instantiateViewController(withIdentifier: "webViewController") as? webViewController {
              vc.url = //indexPath.row
              self.navigationController?.pushViewController(vc, animated: true)

              }





              share|improve this answer






























                0














                First your need to create var in your webViewController e.g



                var url: String?


                then call webViewController like below



                if let vc = storyboard.instantiateViewController(withIdentifier: "webViewController") as? webViewController {
                vc.url = //indexPath.row
                self.navigationController?.pushViewController(vc, animated: true)

                }





                share|improve this answer




























                  0












                  0








                  0







                  First your need to create var in your webViewController e.g



                  var url: String?


                  then call webViewController like below



                  if let vc = storyboard.instantiateViewController(withIdentifier: "webViewController") as? webViewController {
                  vc.url = //indexPath.row
                  self.navigationController?.pushViewController(vc, animated: true)

                  }





                  share|improve this answer















                  First your need to create var in your webViewController e.g



                  var url: String?


                  then call webViewController like below



                  if let vc = storyboard.instantiateViewController(withIdentifier: "webViewController") as? webViewController {
                  vc.url = //indexPath.row
                  self.navigationController?.pushViewController(vc, animated: true)

                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 13:27

























                  answered Nov 23 '18 at 12:46









                  Anas MeharAnas Mehar

                  1507




                  1507






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446766%2fgive-value-to-uiwebkit-and-change-instantiate-viewcontroller-swift-4%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini