最简单的方法是在按下按钮时添加 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;
And add [webView release];
在dealloc方法中。
5)返回IB并单击File's Owner,然后将webView出口连接到您创建的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 视图并在其中添加按钮,或者只是在笔尖中或以编程方式定义一个单独的按钮,然后隐藏 webView 以“摆脱它”。