我想向应用程序中存在的所有 View 添加一个 UIWebView。我没有加载和显示 Web View 。有人可以帮助我吗?
为了让它工作,我创建了一个共享 Web View 类。
//.h Implementation
@interface WebViewAdds : UIWebView {
}
+ (WebViewAdds *) sharedWebView ;
@end
WebViewAdds *g_sharedWebView ;
@implementation WebViewAdds
+(WebViewAdds *) sharedWebView
{
if (g_sharedWebView == nil) {
g_sharedWebView = [[WebViewAdds alloc] initWithFrame:CGRectMake(0, 400, 320, 60)];
[g_sharedWebView setDelegate:self];
NSString *urlAddress = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[g_sharedWebView loadRequest:requestObj];
}
return g_sharedWebView;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
- (void)dealloc {
[super dealloc];
}
@end
在所有的 View Controller 中,我调用的是和
-(UIWebView *) testWebView {
return [WebViewAdds sharedWebView] ;
}
-(void) viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.testWebView];
}
最佳答案
我看到了两个问题。
首先是你没有真正正确地做单例。 This SO question里面有一些很好的信息。
第二个问题是你有一个 UIWebView 的子类并且你正在将它的委托(delegate)设置为它自己。理想情况下,委托(delegate)是提供额外行为的单独类。
关于iphone - 所有 View 共享 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5225623/