[英]Runtime error when category is added to the UIWebView
我只是將UIWebView+CategoryTest
添加到UIWebView
,使用自定義方法hlz_layoutSubviews
使它的layoutSubviews
hlz_layoutSubviews
,但是UIWebView
在我的項目中從未使用過。 當我構建並運行該應用程序時,執行[self hlz_layoutSubviews];
時會顯示運行時錯誤[self hlz_layoutSubviews];
CategoryTest [29881:2122091]-[UIWindow hlz_layoutSubviews]:無法識別的選擇器已發送到實例0x7f8e73d311e0
我想念什么嗎?
Single View Application
模板創建一個空項目。 UIWebView+CategoryTest
添加到UIWebView
。 UIWebView+CategoryTest.m
的代碼: ```
@implementation UIWebView (CategoryTest)
+ (void)load {
// Swizzle the `layoutSubviews` method.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method originalMethod = class_getInstanceMethod(self, @selector(layoutSubviews));
Method swizzledMethod = class_getInstanceMethod(self, @selector(hlz_layoutSubviews));
method_exchangeImplementations(originalMethod, swizzledMethod);
});
}
- (void)hlz_layoutSubviews {
[self hlz_layoutSubviews];
}
@end
```
Breakpoint navigator
為All Exceptions
添加Exception Breakpoint
。 經過與其他開發人員的討論,結果是,我在UIWebView中泛化的layoutSubviews
方法是由其超類UIView
。 因此,方法的交換也影響UIView的其他子類,例如UIWindow
。 結果,將layoutSubviews
發送到UIWindow
實例時,實際上將發送到hlz_layoutSubviews
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.