非常简单的 iPhone 应用程序在 UILabel settext 上崩溃

发布于 2024-10-10 09:34:40 字数 721 浏览 0 评论 0原文

我有一个非常简单的应用程序。我在 IB 中有一个按钮和一个标签。我有一个用于 onClick 的 IBAction,它在标签上调用 setText。标签有一个出口。 IB 中的一切都是相连的。它在模拟器中第一次使应用程序崩溃。当我再次启动它时,它会设置文本。然后下次再崩溃。它总是在实际设备上崩溃。这应该很简单,但我不确定我做错了什么。

谢谢。

在我的 .h 文件中:

#import <UIKit/UIKit.h>

@interface UntitledViewController : UIViewController {
IBOutlet UILabel *label;
IBOutlet UIButton *button;
}

@property (nonatomic, retain) UILabel *label;

-(IBAction) onClick1: (id) sender;

@end

和在 .m 文件中:

- (IBAction) onClick1: (id) sender
{
    //[label setText:@"Hello World!"];
    label.text = @"Hello World!";
    //[button setTitle:@"Clicked" forState:UIControlStateNormal];
}

抱歉,我是该网站的新手。如何获取崩溃日志和堆栈?谢谢。

I have a very simple application. I have a button and a label in IB. I have an IBAction for onClick that calls setText on the label. There's an outlet for the label. Everything is connected in IB. It crashes the app the first time in the simulator. When I launch it again, it sets the text. Then crashes again next time. It always crashes on the actual device. This should be simple, but I'm not sure what I'm doing wrong.

Thanks.

in my .h file :

#import <UIKit/UIKit.h>

@interface UntitledViewController : UIViewController {
IBOutlet UILabel *label;
IBOutlet UIButton *button;
}

@property (nonatomic, retain) UILabel *label;

-(IBAction) onClick1: (id) sender;

@end

and in the .m:

- (IBAction) onClick1: (id) sender
{
    //[label setText:@"Hello World!"];
    label.text = @"Hello World!";
    //[button setTitle:@"Clicked" forState:UIControlStateNormal];
}

Sorry, I'm new to the site. How do I get the crash log and the stack? Thanks.

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

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

发布评论

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

评论(2

橘香 2024-10-17 09:34:40

编辑:虽然这个答案在技术上是正确的,但它根本没有回答问题:(抱歉


<警告-这是一个猜测>

如果您在设置标签文本时发生崩溃,那么它告诉我您已经设置了过去要标记的值,但它没有被正确保留,

我猜您有这样的代码:

label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)] autorelease];

当您应该有类似

// Option 1
self.label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)] autorelease];

or

// Option 2
label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)];

( 的代码时,第一个使用属性来保留标签。第二个不会自动释放。第一个是推荐的方式)

EDIT : While this answer is technically correct, it doesn't answer the question at all :( Sorry


< warning - this is a guess >

If you're getting a crash setting the label's text then it tells me that you have set a value to label in the past but it's not been retained correctly.

I'm guessing you have code like this :

label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)] autorelease];

when you should have code like

// Option 1
self.label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)] autorelease];

or

// Option 2
label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)];

( the first one uses the property to retain to label. The second one doesn't autorelease it. The first one is the recommended way )

菩提树下叶撕阳。 2024-10-17 09:34:40

仔细检查您在 IB 中设置的标签连接。
在调试器中在 label.text = @"Hello World!";
行上放置断点
并确保这里的标签不为零。
如果它为零,则说明您没有在 IB 中为其设置连接。

Double check you set connection for label in IB.
Put breakpoint in debugger on line label.text = @"Hello World!";
And make sure label is not nil here.
If it is nil you didn't set connection in IB for it.

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