Give Value to UIWebkit and change instantiate ViewController [Swift 4]
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
add a comment |
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
how are you loading the url in your current implementation ofwebViewController
?
– 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
add a comment |
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
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
ios swift xcode
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 ofwebViewController
?
– 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
add a comment |
how are you loading the url in your current implementation ofwebViewController
?
– 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
add a comment |
3 Answers
3
active
oldest
votes
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)
}
2
You made my day Sir. Thank you very much :)
– Thomas22
Nov 23 '18 at 12:55
Happy coding :D.. upvote it if you want
– wings
Nov 23 '18 at 12:59
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
add a comment |
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.
add a comment |
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)
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
}
2
You made my day Sir. Thank you very much :)
– Thomas22
Nov 23 '18 at 12:55
Happy coding :D.. upvote it if you want
– wings
Nov 23 '18 at 12:59
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
add a comment |
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)
}
2
You made my day Sir. Thank you very much :)
– Thomas22
Nov 23 '18 at 12:55
Happy coding :D.. upvote it if you want
– wings
Nov 23 '18 at 12:59
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
add a comment |
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)
}
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)
}
edited Nov 23 '18 at 13:32
answered Nov 23 '18 at 12:45
wingswings
1,082523
1,082523
2
You made my day Sir. Thank you very much :)
– Thomas22
Nov 23 '18 at 12:55
Happy coding :D.. upvote it if you want
– wings
Nov 23 '18 at 12:59
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
add a comment |
2
You made my day Sir. Thank you very much :)
– Thomas22
Nov 23 '18 at 12:55
Happy coding :D.. upvote it if you want
– wings
Nov 23 '18 at 12:59
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
Happy coding :D.. upvote it if you want
– wings
Nov 23 '18 at 12:59
Happy coding :D.. upvote it if you want
– wings
Nov 23 '18 at 12:59
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 23 '18 at 12:54
answered Nov 23 '18 at 12:45
hardik parmarhardik parmar
687414
687414
add a comment |
add a comment |
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)
}
add a comment |
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)
}
add a comment |
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)
}
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)
}
edited Nov 23 '18 at 13:27
answered Nov 23 '18 at 12:46
Anas MeharAnas Mehar
1407
1407
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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