ios 在导航控制器中第二次单击后自定义委托为空
ios custom delegate is null after second click in navigation controller
我有一个自定义的 UIWebView
(EpubWebView),一个自定义的 NSURLCache
(EpubCache) 用于处理请求。
我创建了一个自定义委托来处理请求。
EpubCache.h
@protocol EpubCacheDelegate <NSObject>
@required
- (NSCachedURLResponse *)hadleRequest:(NSURLRequest *)request;
@end
@interface EpubCache : NSURLCache
@property (nonatomic, weak) id <EpubCacheDelegate> cacheDelegate;
@end
EpubCache.m
import "EpubCache.h"
@interface EpubCache ()
@end
@implementation EpubCache
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
return [self.cacheDelegate hadleRequest:request];
}
@end
EpubWebView .h
@interface EpubWebView : UIWebView <UIWebViewDelegate, EpubCacheDelegate>
@property (strong, nonatomic) EpubCache *mLocalCache;
@end
EpubWebView.m
- (void) localInit
{
self.mLocalCache = [[EpubCache alloc] init];
self.mLocalCache.cacheDelegate = self;
[NSURLCache setSharedURLCache:self.mLocalCache];
}
- (NSCachedURLResponse *)hadleRequest:(NSURLRequest *)request
{
// return handled request
}
另一方面,我有一个 navigationcontroller
和一个 tableview
,目标视图控制器有这个 webview。
当我 运行 应用程序并单击 tableview
中的一个项目时,一切都很好并且 delegate
按预期工作。
如果我单击返回并单击 tableview 中的其他项目,事情就会变得 错误 ,cachedResponseForRequest
被 调用 但是hadleRequest
不会,我检查后发现 delegate
是 null!
我不明白这里发生了什么。
如有任何帮助,我们将不胜感激。
更新 1
EpubWebView.m
- (id)init
{
self = [super init];
if (self)
{
[self localInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self localInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
[self localInit];
}
return self;
}
更新 2
调出包含 EpubWebView
的视图控制器的表视图的 segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
BookViewController *bookController = segue.destinationViewController;
bookController.mBook = booksList[indexPath.row];
}
和BookViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
mWebView = [[EpubWebView alloc] initWithFrame:self.mainView.frame];
[self.mainView addSubView:mWebView];
[mWebView setBook:self.mBook];
}
您需要做一些修改。首先,删除你的“localInit
”方法,然后在 EPubWebView 中创建一个新函数:
- (void) setCache: (EpubCache *)localCache
{
localCache.cacheDelegate = self;
}
现在,您可以通过 .m 文件中的这些行在 BookViewController 中创建并保存缓存:
@interface BookViewController ()
@property (strong, nonatomic) EpubCache *mLocalCache;
@end
并将 BookViewController 的 viewDidLoad 方法更改为:
- (void) viewDidLoad {
self.mLocalCache = [[EpubCache alloc] init];
// only need to do this once, at viewDidLoad time
[NSURLCache setSharedURLCache:self.mLocalCache ];
[super viewDidLoad];
mWebView = [[EpubWebView alloc] initWithFrame:self.mainView.frame];
[mWebView setCache:self.mLocalCache];
[self.mainView addSubView:mWebView];
[mWebView setBook:self.mBook];
}
我有一个自定义的 UIWebView
(EpubWebView),一个自定义的 NSURLCache
(EpubCache) 用于处理请求。
我创建了一个自定义委托来处理请求。
EpubCache.h
@protocol EpubCacheDelegate <NSObject>
@required
- (NSCachedURLResponse *)hadleRequest:(NSURLRequest *)request;
@end
@interface EpubCache : NSURLCache
@property (nonatomic, weak) id <EpubCacheDelegate> cacheDelegate;
@end
EpubCache.m
import "EpubCache.h"
@interface EpubCache ()
@end
@implementation EpubCache
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
return [self.cacheDelegate hadleRequest:request];
}
@end
EpubWebView .h
@interface EpubWebView : UIWebView <UIWebViewDelegate, EpubCacheDelegate>
@property (strong, nonatomic) EpubCache *mLocalCache;
@end
EpubWebView.m
- (void) localInit
{
self.mLocalCache = [[EpubCache alloc] init];
self.mLocalCache.cacheDelegate = self;
[NSURLCache setSharedURLCache:self.mLocalCache];
}
- (NSCachedURLResponse *)hadleRequest:(NSURLRequest *)request
{
// return handled request
}
另一方面,我有一个 navigationcontroller
和一个 tableview
,目标视图控制器有这个 webview。
当我 运行 应用程序并单击 tableview
中的一个项目时,一切都很好并且 delegate
按预期工作。
如果我单击返回并单击 tableview 中的其他项目,事情就会变得 错误 ,cachedResponseForRequest
被 调用 但是hadleRequest
不会,我检查后发现 delegate
是 null!
我不明白这里发生了什么。
如有任何帮助,我们将不胜感激。
更新 1
EpubWebView.m
- (id)init
{
self = [super init];
if (self)
{
[self localInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self localInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
[self localInit];
}
return self;
}
更新 2
调出包含 EpubWebView
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
BookViewController *bookController = segue.destinationViewController;
bookController.mBook = booksList[indexPath.row];
}
和BookViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
mWebView = [[EpubWebView alloc] initWithFrame:self.mainView.frame];
[self.mainView addSubView:mWebView];
[mWebView setBook:self.mBook];
}
您需要做一些修改。首先,删除你的“localInit
”方法,然后在 EPubWebView 中创建一个新函数:
- (void) setCache: (EpubCache *)localCache
{
localCache.cacheDelegate = self;
}
现在,您可以通过 .m 文件中的这些行在 BookViewController 中创建并保存缓存:
@interface BookViewController ()
@property (strong, nonatomic) EpubCache *mLocalCache;
@end
并将 BookViewController 的 viewDidLoad 方法更改为:
- (void) viewDidLoad {
self.mLocalCache = [[EpubCache alloc] init];
// only need to do this once, at viewDidLoad time
[NSURLCache setSharedURLCache:self.mLocalCache ];
[super viewDidLoad];
mWebView = [[EpubWebView alloc] initWithFrame:self.mainView.frame];
[mWebView setCache:self.mLocalCache];
[self.mainView addSubView:mWebView];
[mWebView setBook:self.mBook];
}