最简单的方法就是在按钮被按下时添加一个 UIWebView。将此方法添加到您的 ViewController.m 并在按下按钮时执行此方法。
以编程方式:
//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; //Change self.view.bounds to a smaller CGRect if you don't want it to take up the whole screen
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
[self.view addSubview:webView];
[webView release];
}
使用界面生成器:
1) 将 UIWebView 对象添加到您的界面。
2) 将“隐藏”属性设置为选中(在 Interface Builder 的“属性检查器”窗口中)。在您想显示它之前,您会一直隐藏它。
3) 将以下代码添加到您的 ViewController.h 中,在其他 @property 行下方:
@property (nonatomic, retain) IBOutlet UIWebView *webView;
4) 在 ViewController.m 中的 @synthesize 下方添加以下行
@synthesize webView;
并在dealloc方法中添加[webView release];。
5) 回到 IB 并点击 File's Owner,然后将 webView outlet 连接到您创建的 webView。
6) 添加以下方法而不是我上面显示的方法(对于程序示例):
//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
self.webView.hidden = NO;
}
你可以设置一个 IBAction 方法来响应按钮按下,但听起来你已经让按钮按下工作了,所以我现在不会打扰。
至于在 Web 视图上方添加一个按钮,您可以子类化 Web 视图并在其中添加该按钮,或者只在您的 nib 中定义一个单独的按钮或以编程方式隐藏 webView 以“摆脱它”。