もちろん可能です(本当に...なぜですか?)。といくつかのサブビューUIView
があるだけです。次に、次のようなことを行うことができます。UIWebView
UIButton
// Suppose that self.mainView is the main container (and an IBOutlet)
// and self.webView is the UIWebView (also an IBOutlet)
// and of course your UIButtons (connected to IBActions)
-(IBAction)visitSiteA:(id)sender
{
NSString *urlAddress = @”http://www.siteA.com”;
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
-(IBAction)visitSiteB:(id)sender
{
NSString *urlAddress = @”http://www.siteB.com”;
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
これで、InterfaceBuilderを使用しない場合は、コードでwebViewとボタンを作成し、それらをmainViewに追加するだけで済みます。
最後に、多数のボタンを計画している場合は、読み込み部分を別のメソッドに分割し、IBActionsからURLを渡すだけで、コードを最適化できます。このようなもの:
-(void)loadUrlAddress:(NSString *)urlAddress
{
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
-(IBAction)visitSiteA:(id)sender
{
[self loadUrlAddress:@"http://www.siteA.com"];
}