![](/img/trans.png)
[英]Web page button click in UIWebView works on Simulate not on iOS device
[英]Open web page in UIWebView after click on the button
我需要幫助。 我有兩個按鈕,每個按鈕都需要在UIWebView中打開不同的網頁(例如:button1打開網站apple.com,button2打開google.com)。 我找不到任何對我有幫助的教程。 尋求幫助(我使用情節提要) 。
有我的代碼,但出了點問題,因為我只看到黑屏(單擊按鈕后)
TabulkaViewController.m
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(10, 240, 300, 35);
[myButton setTitle:@"Buttonone" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: myButton];
}
-(void) myButtonClick:(NSString *)myString
{
NSURL *url = [NSURL URLWithString:@"http://www.apple.com/"];
WebViewController *viewWeb = [[WebViewController alloc] initWithURL:url andTitle:@"Apple"];
[self presentViewController:viewWeb animated:YES completion:nil];
}
WebViewController.h
#import <UIKit/UIKit.h>
@interface WebViewController : UIViewController <UIWebViewDelegate>
{
NSURL *theURL;
NSString *theTitle;
IBOutlet UINavigationItem *webTitle;
}
- (id)initWithURL:(NSURL *)url;
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string;
@property (strong, nonatomic) IBOutlet UIWebView *viewWeb;
@end
WebViewController.m
#import "WebViewController.h"
@implementation WebViewController
- (void)viewDidLoad
{
[super viewDidLoad];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[_viewWeb loadRequest:requestObject];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string {
if( self = [super init] ) {
theURL = url;
theTitle = string;
}
return self;
}
-(id)initWithURL:(NSURL *)url {
return [self initWithURL:url andTitle:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
_viewWeb.delegate = nil;
[_viewWeb stopLoading];
}
@end
使用情節提要時,如果將項目文件上載到某處,將很有幫助。
但是根據經驗,您需要將webView委托設置為類WebViewController
您可以通過在viewDidLoad中添加以下代碼來做到這一點:
self.webView.delegate = self;
那么您必須實現shouldStartLoadWithRequest委托調用,這是一個好的開始。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
讓我知道是否可以解決。
編輯:檢查項目后,將WebViewController- > viewDidLoad更改為此:
- (void)viewDidLoad
{
[super viewDidLoad];
// init webview
_viewWeb = [[UIWebView alloc] initWithFrame:self.view.frame];
_viewWeb.delegate = self;
// add it to this controller's view
[self.view addSubview:_viewWeb];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[self.viewWeb loadRequest:requestObject];
}
您的_viewWeb並未按照我的懷疑進行初始化,請參閱有關如何初始化它以及如何將其添加到WebViewController的評論
EDIT2:添加了使Web視圖全屏顯示的約束,建議您閱讀有關Apple文檔中自動布局的內容
- (void)viewDidLoad
{
[super viewDidLoad];
// init webview
_viewWeb = [[UIWebView alloc] initWithFrame:self.view.frame];
_viewWeb.delegate = self;
// add it to this controller's view
[self.view addSubview:_viewWeb];
// add constraints
NSDictionary* dict = @{@"viewWeb":_viewWeb};
_viewWeb.translatesAutoresizingMaskIntoConstraints = NO;
NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[viewWeb]-0-|" options:0 metrics:nil views:dict];
[self.view addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[viewWeb]-0-|" options:0 metrics:nil views:dict];
[self.view addConstraints:constraints];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[self.viewWeb loadRequest:requestObject];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.