私はIOSとObjectiveCを初めて使用します。シナリオは、UIWebviewを含む2つのビューコントローラーを(セグで)開く2つのボタンがあることです。1つのUIWebViewでそれを行う方が良いと思ったので、webvewのリクエストオブジェクトを渡し、1つのwebviewコントローラーのみを使用しようとしました。
そのため、wwwBtn(サイトを開くUIButton)とfbBtn(FacebookのURLに移動するUIButton)を取得し、viewControllerとUIWebViewを含むwwwWebViewControllerを取得しました。
これが私がそれをした方法です。
viewController.hファイル:
@interface ViewController : UIViewController {
UIButton *wwwBtn;
UIButton *fbButton;
}
@property (retain, nonatomic) IBOutlet UIButton *wwwBtn;
@property (retain, nonatomic) IBOutlet UIButton *fbBtn;
ViewController.mファイル:
#import "ViewController.h"
#import "wwwWebViewController.h"
@implementation ViewController
@synthesize wwwBtn;
@synthesize fbBtn;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"web"]) {
NSString *urlstr=@"http ://www.google.com";
wwwWebViewController *vc = [segue destinationViewController];
vc.urlStr = urlstr;
} else if ([[segue identifier] isEqualToString:@"fb"]) {
NSString *urlstr = @"http://www.facebook.com";
wwwWebViewController *vc = [segue destinationViewController];
vc.urlStr = urlstr;
}
}
wwwWebViewController.hファイル:
@interface wwwWebViewController : UIViewController {
UIWebView *webView;
}
@property (retain, nonatomic) IBOutlet UIWebView *webView;
wwwWebViewController.mファイル:
- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[super viewDidLoad];
}