查看 iOS 的工厂
View factory for iOS
我正在尝试为 iOS 创建一个视图工厂,但遇到了一些麻烦。视图工厂应该能够创建多个 UIImageViews、MKMapViews 或 UIWebViews 并将它们垂直添加到视图中。我的 Web 视图有问题,因为我必须等待 webViewDidFinishLoad
。如果这些方法是在实现 UIWebViewDelegate
的 ViewController 中实现的,并且将 webviews 的委托设置为控制器,则一切正常。问题是我想将所有这些放在一个单独的 class 中,但 webViewDidFinishLoad
从未被调用过。有没有办法做到这一点?
如果您需要任何代码,请告诉我
代码:
零件已添加
- (void)addPart
{
if (self.viewPartStrings.count != 0) {
NSString *viewPartString = [self.viewPartStrings objectAtIndex:0];
NSString *viewPartIdentifier = [self getViewPartIdentifier:viewPartString];
NSString *viewPartDefinition = [self getViewPartDefinition:viewPartString];
if ([viewPartIdentifier isEqualToString:kWeb]) {
[self createTextWebViewPart:viewPartIdentifier];
} else if ([viewPartIdentifier isEqualToString:kMap]) {
[self createMapViewPart:viewPartDefinition];
} else if ([viewPartIdentifier isEqualToString:kImage]) {
[self createImageViewPart:viewPartDefinition];
}
}
}
已创建视图:
- (void)createTextWebViewPart:(NSString *)viewPartText
{
WebViewPart *webViewPart = [[WebViewPart alloc]initWithFrame:[self getViewPartFrameForHeight:0]];
[webViewPart createViewForText:viewPartText];
[webViewPart setDelegate:self];
}
- (void)createImageViewPart:(NSString *)viewPartDefinition
{
//create image
}
- (void)createMapViewPart:(NSString *)viewPartDefinition
{
//create map
}
但这就是重点。永远不会调用此方法。如果 class 扩展了 NSObject 而不是如果 class 扩展了 UIView:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];
int height = frame.size.height + 70;
[self addToView:webView forHeight:height];
}
编辑: 我也尝试在控制器中这样做:
@interface DetailController ()
@property (nonatomic, strong) ViewPartsFactory *viewPartsFactory;
@end
然后:
- (void)setViewForModel:(DealModel *)model
{
//...
self.viewPartsFactory = [[ViewPartsFactory alloc]initWithViewParts: [descriptionParser getViewParts]];
UIView *view = [self.viewPartsFactory getView]
[scrollView addSubview:view];
//..
}
和
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self.viewPartsFactory webViewDidFinishLoad:webView];
}
将 webview 委托给控制器,但仍然没有。
我会用超级伪代码来回答这个问题,希望它能让您了解我的建议。
// ViewPartsFactory.h
-(void)webViewDidFinishLoad; // implement your logic in this method
// Ensure you DO NOT set the delegate anywhere here, otherwise it'll override when you set it in the view controller.
//--------------------------------------------------//
// DetailController.m
@property (nonatomic, weak) id <UIWebViewDelegate> delegate;
DetailController <UIWebViewDelegate>
-(void)viewDidLoad {
self.delegate = viewPartsFactory;
}
我正在尝试为 iOS 创建一个视图工厂,但遇到了一些麻烦。视图工厂应该能够创建多个 UIImageViews、MKMapViews 或 UIWebViews 并将它们垂直添加到视图中。我的 Web 视图有问题,因为我必须等待 webViewDidFinishLoad
。如果这些方法是在实现 UIWebViewDelegate
的 ViewController 中实现的,并且将 webviews 的委托设置为控制器,则一切正常。问题是我想将所有这些放在一个单独的 class 中,但 webViewDidFinishLoad
从未被调用过。有没有办法做到这一点?
如果您需要任何代码,请告诉我
代码: 零件已添加
- (void)addPart
{
if (self.viewPartStrings.count != 0) {
NSString *viewPartString = [self.viewPartStrings objectAtIndex:0];
NSString *viewPartIdentifier = [self getViewPartIdentifier:viewPartString];
NSString *viewPartDefinition = [self getViewPartDefinition:viewPartString];
if ([viewPartIdentifier isEqualToString:kWeb]) {
[self createTextWebViewPart:viewPartIdentifier];
} else if ([viewPartIdentifier isEqualToString:kMap]) {
[self createMapViewPart:viewPartDefinition];
} else if ([viewPartIdentifier isEqualToString:kImage]) {
[self createImageViewPart:viewPartDefinition];
}
}
}
已创建视图:
- (void)createTextWebViewPart:(NSString *)viewPartText
{
WebViewPart *webViewPart = [[WebViewPart alloc]initWithFrame:[self getViewPartFrameForHeight:0]];
[webViewPart createViewForText:viewPartText];
[webViewPart setDelegate:self];
}
- (void)createImageViewPart:(NSString *)viewPartDefinition
{
//create image
}
- (void)createMapViewPart:(NSString *)viewPartDefinition
{
//create map
}
但这就是重点。永远不会调用此方法。如果 class 扩展了 NSObject 而不是如果 class 扩展了 UIView:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];
int height = frame.size.height + 70;
[self addToView:webView forHeight:height];
}
编辑: 我也尝试在控制器中这样做:
@interface DetailController ()
@property (nonatomic, strong) ViewPartsFactory *viewPartsFactory;
@end
然后:
- (void)setViewForModel:(DealModel *)model
{
//...
self.viewPartsFactory = [[ViewPartsFactory alloc]initWithViewParts: [descriptionParser getViewParts]];
UIView *view = [self.viewPartsFactory getView]
[scrollView addSubview:view];
//..
}
和
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self.viewPartsFactory webViewDidFinishLoad:webView];
}
将 webview 委托给控制器,但仍然没有。
我会用超级伪代码来回答这个问题,希望它能让您了解我的建议。
// ViewPartsFactory.h
-(void)webViewDidFinishLoad; // implement your logic in this method
// Ensure you DO NOT set the delegate anywhere here, otherwise it'll override when you set it in the view controller.
//--------------------------------------------------//
// DetailController.m
@property (nonatomic, weak) id <UIWebViewDelegate> delegate;
DetailController <UIWebViewDelegate>
-(void)viewDidLoad {
self.delegate = viewPartsFactory;
}