gpt4 book ai didi

cocoa-touch - cocoa touch : Creating and Adding Custom View

转载 作者:行者123 更新时间:2023-12-03 17:20:40 25 4
gpt4 key购买 nike

我在 cocoa touch 中创建了一个由 UIView 父类(super class)的自定义 View ,并在我的主 Controller 中初始化它,然后将它作为 subview 添加到主 View 中,但是当我将它添加到主 View 时,它再次调用我的初始化方法并导致一个无限循环。我要创建我的自定义 View 错误吗?
这是主 View

- (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;
}

最佳答案

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

对于 UIView子类,代码中创建 View 时指定的初始化器是initWithFrame: ,所以你应该在添加标签和图像之前调用它。您可以使用图像来计算自定义 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;
}

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

关于cocoa-touch - cocoa touch : Creating and Adding Custom View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2990887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com