当然是可能的(真的......为什么不呢?)。你只有一个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"];
}