我要切换我的应用程序的一部分:我将实现一个图片的web查看(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];
如果你知道怎么做......对我来说会很好,
谢谢!
答案 0 :(得分:4)
在你的h文件中声明:
UIWebView *contentWebView;
并告诉您的viewController您将实现<UIWebViewDelegate>
在viewDidLoad中添加:
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/"]]];
答案 1 :(得分:1)
根据需要调整框架,如果将像imageview这样的东西添加到webview,那么你必须使用bringubviewtofront函数。
- (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];
}