iPhone创建UILabel的函数失败并崩溃

发布于 2024-09-17 21:14:20 字数 1521 浏览 6 评论 0原文

我有一个带有滚动视图的视图。我使用代码将标签添加到滚动视图。由于有很多冗余代码,我尝试将标签创建移动到返回 UILabel 的单独函数中。这是该函数:

- (UILabel *)   f_MakeLabelWithL:(float)MyLeft T:(float)MyTop W:(float)MyWidth H:(float)MyHeight Align:(UITextAlignment)MyAlign 
                      Font:(UIFont *)MyFont TextColor:(UIColor *)MyTextColor BGColor:(UIColor *)MyBGColor {

CGRect rect = CGRectMake(MyLeft, MyTop, MyWidth, MyHeight);
UILabel *label = [[[UILabel alloc] initWithFrame:rect] autorelease];

label.adjustsFontSizeToFitWidth = YES;
label.backgroundColor = MyBGColor;
label.font = MyFont;
label.numberOfLines = 0;
label.textAlignment = MyAlign;
label.textColor = MyTextColor;

return label;
}

我在返回后设置标签的文本。或者说我是这么想的。事实上,被告知具有蓝色背景的标签显示为纯黑色矩形。背景清楚的人就完全清楚了。更糟糕的是:滚动视图不会滚动,并且尝试使其滚动会导致应用程序崩溃,而调试器控制台中没有任何解释。

然而,所有这些代码都可以内联运行得很好。

有人知道为什么吗?

更新:将代码内联固定了标签的显示。但我还使用一个函数来创建图像:

- (UIImageView *) f_MakeImageWithL:(float)MyLeft T:(float)MyTop W:(float)MyWidth H:(float)MyHeight File:(NSString *)MyFile {
CGRect rect = CGRectMake(MyLeft, MyTop, MyWidth, MyHeight);
UIImageView *oImageView = [[[UIImageView alloc] initWithFrame:rect] autorelease];

NSString *s = [[Isystant f_DocumentsPath] stringByAppendingPathComponent:MyFile];
UIImage *oImage = [UIImage imageWithContentsOfFile:s];
[oImageView setImage:oImage];
oImageView.backgroundColor = [UIColor redColor];
oImageView.opaque = YES; // explicitly opaque for performance

return oImageView;
}

它正在创建图像,但冻结滚动/崩溃问题并没有消失,直到我也将此代码放回内联。

I have a view with a scrollview. I use code to add labels to the scrollview. Since there's a lot of redundant code, I tried to move the label creation to a separate function that returns a UILabel. Here' that function:

- (UILabel *)   f_MakeLabelWithL:(float)MyLeft T:(float)MyTop W:(float)MyWidth H:(float)MyHeight Align:(UITextAlignment)MyAlign 
                      Font:(UIFont *)MyFont TextColor:(UIColor *)MyTextColor BGColor:(UIColor *)MyBGColor {

CGRect rect = CGRectMake(MyLeft, MyTop, MyWidth, MyHeight);
UILabel *label = [[[UILabel alloc] initWithFrame:rect] autorelease];

label.adjustsFontSizeToFitWidth = YES;
label.backgroundColor = MyBGColor;
label.font = MyFont;
label.numberOfLines = 0;
label.textAlignment = MyAlign;
label.textColor = MyTextColor;

return label;
}

I set the text of the label after it is returned. Or so I intended. In fact, a label that is told to have a blue background appears as a solid black rectangle. One with a clear background is entirely clear. Worse than that: the scrollview won't scroll, and attempting to make it do so crashes the app without any explanation in the debugger console.

Yet all this code works inline just fine and dandy.

Anyone know why?

Update: Putting the code inline fixed the display of the labels. But I was also using a function to create images:

- (UIImageView *) f_MakeImageWithL:(float)MyLeft T:(float)MyTop W:(float)MyWidth H:(float)MyHeight File:(NSString *)MyFile {
CGRect rect = CGRectMake(MyLeft, MyTop, MyWidth, MyHeight);
UIImageView *oImageView = [[[UIImageView alloc] initWithFrame:rect] autorelease];

NSString *s = [[Isystant f_DocumentsPath] stringByAppendingPathComponent:MyFile];
UIImage *oImage = [UIImage imageWithContentsOfFile:s];
[oImageView setImage:oImage];
oImageView.backgroundColor = [UIColor redColor];
oImageView.opaque = YES; // explicitly opaque for performance

return oImageView;
}

It was creating the images, but the frozen scroll/crash problem didn't go away until I put this code back inline also.

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

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

发布评论

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

评论(2

小傻瓜 2024-09-24 21:14:20

在我看来,你过度释放了 UILabel。如果标签在此之前从未在任何地方保留过(并且您已在 makeImageWithL 方法中自动释放了它),则在将其添加到滚动视图后,您不必释放该标签。

这可能就是为什么当你将其内联时它会起作用......因为在这种情况下,我敢打赌你正在删除自动释放,对吧?

-S

Looks to me like you are over-releasing the UILabel. You shouldn't have to release the label after adding it to your scrollview if it was never retained anywhere prior to that (and you've autoreleased it in the makeImageWithL method).

That's probably why it is working when you put it inline... because in that scenario, I would bet you are removing the autorelease, right?

-S

行雁书 2024-09-24 21:14:20

您是否将退回的标签保留在某处?

Do you retain your returned Label somewhere?

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