没有界面生成器的 uiwebview

发布于 2024-11-18 09:50:28 字数 699 浏览 1 评论 0原文

我必须切换我的应用程序的一部分:我将实现一个图片视图(imageview)的网络视图。

所以我尝试用界面生成器来制作它,但是错误太多,所以我更喜欢使用代码!

对于图片,我使用这些行:

image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
self.imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,64,320,367);
[self.view addSubview:imageView];
[self.imageView release];

现在,我尝试使用此代码,但没有显示任何内容:

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

...

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.fr"]]];
[self.view addSubview:webView];

如果你知道如何制作...对我来说会很好,

谢谢!

I've to switch a part of my app: I will implement a web view insread of a picture (imageview).

So I tried to make it with the interface builder but there is too many errors so I prefer to use code!

for the pictures, I user those lines :

image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
self.imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,64,320,367);
[self.view addSubview:imageView];
[self.imageView release];

Now, I tried to use this code but nothing's showed:

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

...

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.fr"]]];
[self.view addSubview:webView];

If you know how to make .... would be nice for me,

THANKS!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各空 2024-11-25 09:50:28

在你的 h 文件中声明:

UIWebView *contentWebView;  

并告诉你的 viewController 你将实现

在 vi​​ewDidLoad 中添加:

    contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    contentWebView.delegate = self;
    contentWebView.multipleTouchEnabled = YES;
    contentWebView.scalesPageToFit = NO;    
    [self.view addSubview:contentWebView];

在 dealloc 中不要忘记释放 webView:

[contentWebView release];

并实现委托方法:

#pragma mark -
#pragma mark WebViewDelegates

- (void)webViewDidStartLoad:(UIWebView *)webView{   


}

- (void)webViewDidFinishLoad:(UIWebView *)webView{  

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

}

并且您需要加载一个请求。将 webView 添加到视图后,您可以在 viewDidLoad 中执行此操作:

[contentWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]]];

Declare in your h file:

UIWebView *contentWebView;  

And also tell to your viewController that you will implement <UIWebViewDelegate>

In viewDidLoad Add:

    contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    contentWebView.delegate = self;
    contentWebView.multipleTouchEnabled = YES;
    contentWebView.scalesPageToFit = NO;    
    [self.view addSubview:contentWebView];

In dealloc don't forget to release the webView:

[contentWebView release];

And implement the delegate methods:

#pragma mark -
#pragma mark WebViewDelegates

- (void)webViewDidStartLoad:(UIWebView *)webView{   


}

- (void)webViewDidFinishLoad:(UIWebView *)webView{  

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

}

And also you need to load an request. You can doit in viewDidLoad after you have added the webView to your view:

[contentWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]]];
痴梦一场 2024-11-25 09:50:28

根据需要调整框架,如果添加像 imageview 这样的东西作为 webview 的子视图,那么你必须使用 Bringsubviewtofront 函数。

        - (void) initUIWebView {
            NSLog(@"DetailViewController->initUIWebView {");

                UIWebView *aWebView;

                    //  init and create the UIWebView
                aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
                aWebView.autoresizesSubviews = YES;
                aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

                    //set the web view delegates for the web view to be itself
                [aWebView setDelegate:self];

                    //Set the URL to go to for your UIWebView
                NSString *urlAddress = @”http://www.google.com”;

                    //Create a URL object.
                NSURL *url = [NSURL URLWithString:urlAddress];

                    //URL Requst Object
                NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

                    //load the URL into the web view.
                [aWebView loadRequest:requestObj];

                    //add the web view to the content view
                [self.view addSubview:aWebView];

                [aWebView release], aWebView = nil;

            }

              - (void)viewDidLoad {

                  [self initUIWebView]; // <<<<<<<<<<<< this calls the UIWebView Function
                  [super viewDidLoad];

              }

adjust the frame as u needed and if add anything like imageview as subview to webview then u have to use bringsubviewtofront function.

        - (void) initUIWebView {
            NSLog(@"DetailViewController->initUIWebView {");

                UIWebView *aWebView;

                    //  init and create the UIWebView
                aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
                aWebView.autoresizesSubviews = YES;
                aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

                    //set the web view delegates for the web view to be itself
                [aWebView setDelegate:self];

                    //Set the URL to go to for your UIWebView
                NSString *urlAddress = @”http://www.google.com”;

                    //Create a URL object.
                NSURL *url = [NSURL URLWithString:urlAddress];

                    //URL Requst Object
                NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

                    //load the URL into the web view.
                [aWebView loadRequest:requestObj];

                    //add the web view to the content view
                [self.view addSubview:aWebView];

                [aWebView release], aWebView = nil;

            }

              - (void)viewDidLoad {

                  [self initUIWebView]; // <<<<<<<<<<<< this calls the UIWebView Function
                  [super viewDidLoad];

              }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文