我需要幫助。我有兩個按鈕,我需要每個按鈕來在我的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
我看來像你沒有實際添加web視圖作爲一個子視圖的任何地方?您正在加載請求,但不會在屏幕上顯示。 – brandonscript
他是使用presentViewController:animated:completion: – Lucas