如何在我的iOS应用程序中添加UIWebView? [英] How do I add a UIWebView to my iOS app?

查看:120
本文介绍了如何在我的iOS应用程序中添加UIWebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,当第一个屏幕上按下按钮时,有些工作并生成一个URL,然后

I have an app at the moment that when a button is pushed on the first screen does some work and makes a URL, and then does

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:currentURL]];

使用我的网址启动Safari。我希望从这里启动webview,以便用户可以使用它做一些自定义的事情。我仍然有一段时间了解iOS中的MVC并需要帮助。我的项目是最小的,包含一个AppDelegate.h / m和一个ViewController.h / m,视图控制器的.m是执行此Safari启动的功能所在。

which launches Safari with my URL. I want instead to have a webview launch from here so the user can do some custom things with it. I am still having a hell of a time understanding MVC in iOS and need help. My project is minimal and consists of an AppDelegate.h/m and a ViewController.h/m, the.m of the view controller is where the function that does this Safari launch lives.

任何人都可以帮我理解如何做我想做的事吗?

Can anyone help me understand how to do what I'm trying to d?

谢谢......

推荐答案

最简单的方法是在按下按钮时添加UIWebView。将此方法添加到ViewController.m并在按下按钮时执行此操作。

The simplest way is just to add a UIWebView when the button gets pressed. Add this method to your ViewController.m and have this be performed when the button gets pressed.

以编程方式:

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

使用Interface Builder:

Using Interface Builder:

1)将UIWebView对象添加到您的界面。

1) Add a UIWebView object to your interface.

2)将hidden属性设置为选中(在Interface Builder的Attributes Inspector窗口中) 。您将隐藏它直到您想要显示它。

2) Set the "hidden" property to checked (in the "Attributes Inspector" window in Interface Builder). You'll keep it hidden until you want to show it.

3)将以下代码添加到ViewController.h,低于其他@property行:

3) Add the following code to your ViewController.h, below the other @property lines:

@property (nonatomic, retain) IBOutlet UIWebView *webView;

4)在ViewController.m中添加@synthesize下面的以下行

4) Add the following line below the @synthesize in your ViewController.m

@synthesize webView;

并添加 [webView release]; in dealloc方法。

And add [webView release]; in the dealloc method.

5)返回IB并单击File's Owner,并将webView插座连接到您创建的webView。

5) Go back into IB and click on File's Owner, and connect the webView outlet to the webView you created.

6)添加以下方法,而不是上面显示的方法(对于编程示例):

6) Add the following method instead of the method I showed above (for the programmatic example):

//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方法来响应按下按钮,但它听起来你已经按下按钮了,所以我现在不打扰了。

You could set up an IBAction method to respond to the button press, but it sounds like you already have the button press working, so I wouldn't bother with that now.

只要在Web视图上方添加一个按钮,你就可以子类化网页视图并在那里添加按钮,或者只是在您的笔尖或编程中定义一个单独的按钮,并隐藏webView以摆脱它。

As far as adding a button above the web view, you can either subclass web view and add the button there, or just have a separate button you define in your nib or programmatically and have that hide the webView to "get rid of it".

这篇关于如何在我的iOS应用程序中添加UIWebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆