Xcode 5 在带有 WebView 的新视图控制器中加载 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19277391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 5 Loading a URL in a new View Controller with a WebView in it
提问by Alioo
It seems that all the methods in earlier versions of Xcode are not working for me.
似乎早期版本的 Xcode 中的所有方法都不适用于我。
What I have right now:
I have a main menu with a button that segues into another view with a different controller with a WebView in it (has a property so the URL to load can be set)
我现在
拥有的:我有一个带有按钮的主菜单,该按钮可以连接到另一个带有 WebView 的不同控制器的视图(有一个属性,因此可以设置要加载的 URL)
What I want: When I load that view from that button I want to pass a string from the first view to the new view that represents the URL and load it in the web view. Basically I want a generic WebView view that can load different URLs passed by the views that segue to it.
我想要什么:当我从该按钮加载该视图时,我想将第一个视图中的字符串传递给表示 URL 的新视图,并将其加载到 Web 视图中。基本上我想要一个通用的 WebView 视图,它可以加载由连接到它的视图传递的不同 URL。
Here is the code for the WebView H file
这是 WebView H 文件的代码
#import <UIKit/UIKit.h>
@interface BRWebView : UIViewController
{
IBOutlet UIWebView *requestWebView;
}
@property (strong, nonatomic) NSString *loadUrl;
@end
In the ViewController for the MainMenu there is this for the button listener
在 MainMenu 的 ViewController 中,按钮侦听器有这个
-(IBAction)requestButton:(id)sender
{
BRWebView *web = [self.storyboard instantiateViewControllerWithIdentifier:@"BRWebView"];
web.loadUrl = @"Websitename.com";
[self presentModalViewController:web animated:YES];
}
And in the BRWebView.m file there is this
在 BRWebView.m 文件中有这个
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:_loadUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[requestWebView loadRequest:request];
}
回答by Jordan Montel
EDIT after code in the question :
在问题中的代码后编辑:
In the ViewController
replace your code by :
在ViewController
将您的代码替换为:
- (IBAction)requestButton:(id)sender
{
BRWebView *web = [self.storyboard instantiateViewControllerWithIdentifier:@"BRWebView"];
web.loadUrl = @"http://websitename.com";
[self presentViewController:web animated:YES completion:nil];
}
and in the BRWebView.m
remove your code in viewDidLoadand add viewWillAppear:
并在viewDidLoad 中BRWebView.m
删除您的代码并添加viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:_loadUrl]];
// Load the request in the UIWebView
[_requestWebView loadRequest:requestObj];
}
Old answer :
旧答案:
You need to pass your URL data with a property like this (without navigation controller):
您需要使用这样的属性(没有导航控制器)传递您的 URL 数据:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"])
{
// Get reference to the destination view controller
ViewControllerB *viewControllerB = segue.destinationViewController;
// Pass URL
viewControllerB.webviewURL = @"http://myurl.com";
}
}
with navigation controller :
带导航控制器:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"])
{
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
ViewControllerB *viewControllerB = (ViewControllerB *)navController.topViewController;
// Pass URL
viewControllerB.webviewURL = @"http://myurl.com";
}
}
On your second view .h:
在您的第二个视图 .h 中:
@property (nonatomic, strong) NSString *webviewURL;
second view .m:
第二个视图 .m:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:_webviewURL]];
// Load the request in the UIWebView
[self.webView loadRequest:requestObj];
}
回答by JeroValli
You should set the URL(NSString) property before perform the segue to your destination view controller, and load the request on the 'viewWillAppear' callback:
您应该在对目标视图控制器执行转场之前设置 URL(NSString) 属性,并在“viewWillAppear”回调上加载请求:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.loadUrl) {
[self.requestWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.loadUrl]]];
}
}
回答by Anand Kr. Avasthi
//Easy code for web view
//网页视图的简单代码
- (void)viewDidLoad {
[super viewDidLoad]; // self.myWebView.delegate=self;
NSString *urlString=@"http://www.google.com";
NSURL *url=[NSURL URLWithString:urlString];
NSURLRequest *obj=[NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:obj]; // Do any additional setup after loading the view, typically from a nib.
}