Ipad Can a';UIButton';在'中打开网页;UIWebView';在同一个';UIView';?

Ipad Can a';UIButton';在'中打开网页;UIWebView';在同一个';UIView';?,ipad,uiview,uiwebview,uibutton,push,Ipad,Uiview,Uiwebview,Uibutton,Push,我一直在为iPhone/iPad设备做这个项目,在完成项目的iPhone部分后,我突然想到改变iPad项目的风格 我的设想: 我有一个视图,其中包含链接到网站的UIButtons。最初我计划这些按钮使用一个按钮序列来打开单独的视图,其中已经有一个UIWebView来打开网页。但后来我想也许我可以使用UIButtons在父视图中打开所需的网页 我的问题: 是否可以使用UIButton加载网页,但在与用于加载网页的UIButton位于同一视图中的UIWebView中 提前感谢大家,我认为这应该是可能

我一直在为iPhone/iPad设备做这个项目,在完成项目的iPhone部分后,我突然想到改变iPad项目的风格

我的设想:

我有一个视图,其中包含链接到网站的UIButtons。最初我计划这些按钮使用一个按钮序列来打开单独的视图,其中已经有一个UIWebView来打开网页。但后来我想也许我可以使用UIButtons在父视图中打开所需的网页

我的问题:

是否可以使用UIButton加载网页,但在与用于加载网页的UIButton位于同一视图中的UIWebView中

提前感谢大家,我认为这应该是可能的,但目前还没有想到任何事情。

当然是可能的(真的……为什么不呢?)。您只有一个
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];
}
-(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"];
}
现在,如果您不使用InterfaceBuilder,您可以在代码中构造webView和按钮,然后将它们添加到mainView中

最后,如果您计划有很多按钮,您可以通过将加载部分分解为一个单独的方法来优化代码,只需从IBActions传递url即可。大概是这样的:

// 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];
}
-(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"];
}

谢谢你,伙计,我知道我忽略了一些简单的事情