ios - 继承 MKAnnotationView 并从 initWithFrame : overloads 调用 init 时无限循环

标签 ios objective-c uiview

有这个 XCTestCase 案例:

- (void)testAllInitializersConfigureTheView {
    BIStationAnnotationView *withFrame = [[BIStationAnnotationView alloc] initWithFrame:CGRectNull];
    XCTAssertTrue(CGRectEqualToRect(withFrame.frame, CGRectMake(0.f, 0.f, 30.f, 40.f)), @"Frame should be fixed");
}

测试 MKAnnotationView 的子类:

- (id)init {
    if (self = [super init]) {
        self.frame = _myFrame;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    return self = [self init];
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    return self = [self init];
}

我得到了一个无限循环,因为 initWithFrame 调用了 initinit 调用了 initWithFrame

enter image description here

谁能解释一下为什么?

最佳答案

我猜 [UIView init] 正在调用 [self initWithFrame:CGRectZero],所以它正在调用您自己的 initWithFrame 方法,因为您有重载了它。

要解决你的问题,你应该简单地做同样的事情:)。 init 应该调用 initWithFrame 而不是相反。

关于ios - 继承 MKAnnotationView 并从 initWithFrame : overloads 调用 init 时无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31613428/

相关文章:

ios - 解释为什么 NSURL 在 nil 上抛出错误

objective-c - 如何获取没有导航栏的 UIView 的高度?

ios - 记录 UIScrollView 的 subview 显示未添加的 subview

android - 如何知道Web SQL已经存在?

ios - 采取一定数量的步骤后通知。 swift 2.0

iphone - 将 NSURLCredentialStorage 与同步 NSURLConnection 结合使用

objective-c - youtube 全屏视频不调用 viewWillAppear 或 viewWillRotate

ios - 向 iOS 应用程序添加新语言

ios - 如何在某个 CGPoint 创建 UIView?

iphone - View 隐藏在 UINavigationBar iOS 7 下方