Reputation: 25
I am looking for a quicker method to build my application. As i have a series of buttons that are all going to open a UIWebView. But, I want to be able to use only One (1) WebView. I also want to be able to have the Buttons in a different View Controller.
So my question is, How would you get buttons to go from one view controller into another view controller that has a web viewer in it, and still be able to get different weblink's to load from the buttons?
I am still new to the programing community. I am doing this all as self teaching also.
Thank You guys!
Upvotes: 0
Views: 768
Reputation: 4163
Create one xib which will contain one UIWebView and buttons and in target method of buttons, you assign the new url are reload the webView
As mentioned below
-(void)reloadWebVieWithUrl:(NSString *)strUrl
{
NSURL *url = [NSURL URLWithString:strUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.wbView loadRequest:request];
}
- (IBAction)buttonAction:(UIButton *)sender {
NSString *url = @"";
switch (sender.tag) {
case 100:
{
url = @"https://www.google.co.in/";
break;
}
case 101:
{
url = @"https://in.yahoo.com/";
break;
}
case 102:
{
url = @"https://www.facebook.com/";
break;
}
case 103:
{
url = @"http://stackoverflow.com/";
break;
}
default:
break;
}
[self reloadWebVieWithUrl:url];
}
In Swift
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet var wbView: UIWebView!
var strUrl = ""
@IBAction func buttonAction(sender: UIButton) {
switch (sender.tag){
case 100:
strUrl = "https://www.google.co.in/"
case 101:
strUrl = "https://in.yahoo.com/"
case 102:
strUrl = "https://www.facebook.com/"
case 103:
strUrl = "http://stackoverflow.com/"
default:
break;
}
reloadWebViewWithUrl(strUrl);
}
func reloadWebViewWithUrl(strUrl: NSString){
var url = NSURL(string: strUrl);
var request = NSURLRequest(URL: url);
wbView.loadRequest(request);
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Upvotes: 2
Reputation: 251
You don't need multiple webviews or VC's to do this.
You can just change the NSURL to reflect the website you want to navigate the user to.
So for example the action for button one could be :
NSString *webSite = "@http://google.com";
NSURL *url = [NSURL URLWithString:webSite];
NSURLRequest *request = [NSUrLRequest requestWithURL:url];
[webView loadRequest:request];
Then for button two - use the same code, using the same webView and just change the string to the website you want to navigate the user to.
Hope this helps.
Upvotes: 0