非常简单的 iPhone 应用程序在 UILabel settext 上崩溃
我有一个非常简单的应用程序。我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:虽然这个答案在技术上是正确的,但它根本没有回答问题:(抱歉
<警告-这是一个猜测>
如果您在设置标签文本时发生崩溃,那么它告诉我您已经设置了过去要标记的值,但它没有被正确保留,
我猜您有这样的代码:
当您应该有类似
or
( 的代码时,第一个使用属性来保留标签。第二个不会自动释放。第一个是推荐的方式)
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 :
when you should have code like
or
( the first one uses the property to retain to label. The second one doesn't autorelease it. The first one is the recommended way )
仔细检查您在 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.