Cocoa Touch:创建和添加自定义视图

发布于 2024-09-04 08:41:21 字数 1595 浏览 8 评论 0原文

我在 cocoa touch 中创建了一个由 UIView 超类化的自定义视图,并在我的主控制器中初始化它,然后将其作为子视图添加到主视图中,但是当我将其添加到主视图中时,它再次调用我的初始化方法并导致无限循环。我创建自定义视图是否错误? 这是 mainView

- (void)loadView {
    UIImage* tempImage = [UIImage imageNamed: @"image1.jpg"];
    CustomImageContainer *testImage = [[CustomImageContainer alloc] initWithImage: tempImage andLabel: @"test image" onTop: true atX: 10 atY: 10];
    [self.view addSubview: testImage];
}

和 CustomImageContainer

-(CustomImageContainer *) initWithImage: (UIImage *)imageToAdd andLabel: (NSString *)text onTop: (BOOL) top atX: (int) x_cord atY: (int) y_cord{
    UIImageView *imageview_to_add = [[UIImageView alloc] initWithImage: imageToAdd];
    imageview_to_add.frame = CGRectMake(0, 0, imageToAdd.size.width, imageToAdd.size.height);
    UILabel *label_to_add = [[UILabel alloc] init];
    label_to_add.text = text;
    label_to_add.alpha = 50;
    label_to_add.backgroundColor = [UIColor blackColor];
    label_to_add.textColor = [UIColor whiteColor];
    [self addSubview: imageview_to_add];
    self.frame = CGRectMake(x_cord, y_cord, imageToAdd.size.width, imageToAdd.size.height);
    if (top) {
        label_to_add.frame = CGRectMake(0, 0, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height);
        //[self addSubview: label_to_add];
    }
    else {
        label_to_add.frame = CGRectMake(0,.2 * imageview_to_add.frame.size.height, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height);
    }
    [self addSubview: label_to_add];
    [super  init];
    return self;
}

I create a custom view in cocoa touch that is superclassed by UIView and in my main controller I initialize it and then add it as a subview to the main view, but when I add it to the main view it calls my initializer method again and causes an infinite loop. Am I going about creating my custom view wrong?
Here is the mainView

- (void)loadView {
    UIImage* tempImage = [UIImage imageNamed: @"image1.jpg"];
    CustomImageContainer *testImage = [[CustomImageContainer alloc] initWithImage: tempImage andLabel: @"test image" onTop: true atX: 10 atY: 10];
    [self.view addSubview: testImage];
}

and the CustomImageContainer

-(CustomImageContainer *) initWithImage: (UIImage *)imageToAdd andLabel: (NSString *)text onTop: (BOOL) top atX: (int) x_cord atY: (int) y_cord{
    UIImageView *imageview_to_add = [[UIImageView alloc] initWithImage: imageToAdd];
    imageview_to_add.frame = CGRectMake(0, 0, imageToAdd.size.width, imageToAdd.size.height);
    UILabel *label_to_add = [[UILabel alloc] init];
    label_to_add.text = text;
    label_to_add.alpha = 50;
    label_to_add.backgroundColor = [UIColor blackColor];
    label_to_add.textColor = [UIColor whiteColor];
    [self addSubview: imageview_to_add];
    self.frame = CGRectMake(x_cord, y_cord, imageToAdd.size.width, imageToAdd.size.height);
    if (top) {
        label_to_add.frame = CGRectMake(0, 0, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height);
        //[self addSubview: label_to_add];
    }
    else {
        label_to_add.frame = CGRectMake(0,.2 * imageview_to_add.frame.size.height, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height);
    }
    [self addSubview: label_to_add];
    [super  init];
    return self;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天赋异禀 2024-09-11 08:41:21

为什么将 [super init] 语句放在初始化程序的末尾?子类化时,通常将此语句放在方法的开头。

对于 UIView 子类,指定在代码中创建视图时的初始化程序是 initWithFrame:,因此您应该在添加标签和图像之前调用它。您可以使用图像来计算自定义视图所需的框架。

-(CustomImageContainer *) initWithImage: (UIImage *)imageToAdd andLabel: (NSString *)text onTop: (BOOL) top atX: (int) x_cord atY: (int) y_cord{
    // The view will gets its frame to the size of the image
    UIImageView *imageview_to_add = [[UIImageView alloc] initWithImage: imageToAdd];

    // Call the designated initializer
    CGRect frame = CGRectMake(x_cord, y_cord, imageToAdd.size.width, imageToAdd.size.height);
    self = [super initWithFrame:frame];

    [self addSubview: imageview_to_add];

    UILabel *label_to_add = [[UILabel alloc] init];
    label_to_add.text = text;
    label_to_add.alpha = 50;
    label_to_add.backgroundColor = [UIColor blackColor];
    label_to_add.textColor = [UIColor whiteColor];

    if (top) {
        label_to_add.frame = CGRectMake(0, 0, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height);
    }
    else {
        label_to_add.frame = CGRectMake(0,.2 * imageview_to_add.frame.size.height, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height);
    }
    [self addSubview: label_to_add];

    return self;
}

如果仍然存在无限循环,请暂停调试器并在堆栈跟踪中搜索循环方法模式。此模式将告诉您代码进入无限循环的位置。

Why did you put the [super init] statement at the end of the initializer ? When subclassing, you usually put this statement at the start of method.

For UIView subclasses, the designated initializer when creating views in code is initWithFrame:, so you should call it before adding the label and the image. You can use the image to compute the frame needed by the custom view.

-(CustomImageContainer *) initWithImage: (UIImage *)imageToAdd andLabel: (NSString *)text onTop: (BOOL) top atX: (int) x_cord atY: (int) y_cord{
    // The view will gets its frame to the size of the image
    UIImageView *imageview_to_add = [[UIImageView alloc] initWithImage: imageToAdd];

    // Call the designated initializer
    CGRect frame = CGRectMake(x_cord, y_cord, imageToAdd.size.width, imageToAdd.size.height);
    self = [super initWithFrame:frame];

    [self addSubview: imageview_to_add];

    UILabel *label_to_add = [[UILabel alloc] init];
    label_to_add.text = text;
    label_to_add.alpha = 50;
    label_to_add.backgroundColor = [UIColor blackColor];
    label_to_add.textColor = [UIColor whiteColor];

    if (top) {
        label_to_add.frame = CGRectMake(0, 0, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height);
    }
    else {
        label_to_add.frame = CGRectMake(0,.2 * imageview_to_add.frame.size.height, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height);
    }
    [self addSubview: label_to_add];

    return self;
}

If you still have an infinite loop, pause the debugger and search for the recurrent method pattern in the stack trace. This pattern will gives you where the code enters the infinite loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文