2014-06-17 60 views
0

我需要幫助。我有兩個按鈕,我需要每個按鈕來在我的UIWebView中打開不同的網頁(例如:button1打開網站apple.com,button2打開google.com)。我找不到任何幫助我的教程。 Thx尋求幫助(我使用Storyboard)點擊按鈕後在UIWebView中打開網頁

有我的代碼,但什麼是錯的,因爲我只看到一個黑色的屏幕

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 
+0

我看來像你沒有實際添加web視圖作爲一個子視圖的任何地方?您正在加載請求,但不會在屏幕上顯示。 – brandonscript

+0

他是使用presentViewController:animated:completion: – Lucas

回答

0

當使用故事板時,如果您將項目文件上傳到某處,將會有所幫助。

但是從經驗,你需要的網頁視圖的委託設置爲你的類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視圖採取全屏加限制,我建議你閱讀機在蘋果文檔關於自動佈局

- (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]; 
} 
+0

當我使用self.viewWeb = self;在WebViewController(在viewDidLoad),我有警告:不兼容的指針類型從WebViewController分配給UIWebView:/ – Alien

+0

對不起,我應該這樣做:self.webView.delegate = self,我會編輯答案 – Mike

+0

我試過了,但仍然一個黑屏:( – Alien

相關問題