Iboutlet已连接但仍显示为空

发布于 2024-10-07 14:38:32 字数 164 浏览 4 评论 0原文

我是 iPhone 开发新手,正在练习一些。正如许多教程所述,我将通过 IB 制作的 uilabel 连接到代码中的 IBOutlet,但在尝试设置它的文本时,它仍然说它为空?我在 .h 类上定义了 IBOutlet 对象,并通过 IB 很好地连接了它,没有出现问题,但不知道为什么它仍然为空。任何帮助将不胜感激。

I'm new at iPhone development and was practicing a bit. I connected a uilabel I made through IB to an IBOutlet in my code as many tutorials said but upon trying to set text of it it's still saying it's null? I defined the IBOutlet object on my .h class and connected it fine through IB without a problem but idk why it's still null. Any help would be greatly appreciated.

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

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

发布评论

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

评论(2

七婞 2024-10-14 14:38:32

好的,首先,让我们删除一些无关的内容,专注于您需要的核心内容。

#import "CalculatorBrain.h"

@interface CalculatorViewController : UIViewController 
{
    CalculatorBrain* _calculatorModel;
    UILabel *display;
}

- (IBAction) digitPressed:(UIButton *)sender;

@property (nonatomic, retain) IBOutlet UILabel *display;

@end


#import "CalculatorViewController.h"

@implementation CalculatorViewController
@synthesize display;

- (void)dealloc
{
    [display release], display = nil;
    [_calculatorModel release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (! _calculatorModel)
    {
        _calculatorModel = [[CalculatorBrain alloc] init];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"display is: %@", display);
}

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *currentDigit = [[sender titleLabel] text];
    [display setText:[NSString stringWithFormat:@"%@", currentDigit]];
}

@end

让我们知道当您在 InterfaceBuilder 中设置标签(显示)和操作(digitPressed:) 时会发生什么。

Okay, first things first, let's remove some of the extraneous stuff and focus on the core of what you need.

#import "CalculatorBrain.h"

@interface CalculatorViewController : UIViewController 
{
    CalculatorBrain* _calculatorModel;
    UILabel *display;
}

- (IBAction) digitPressed:(UIButton *)sender;

@property (nonatomic, retain) IBOutlet UILabel *display;

@end


#import "CalculatorViewController.h"

@implementation CalculatorViewController
@synthesize display;

- (void)dealloc
{
    [display release], display = nil;
    [_calculatorModel release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (! _calculatorModel)
    {
        _calculatorModel = [[CalculatorBrain alloc] init];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"display is: %@", display);
}

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *currentDigit = [[sender titleLabel] text];
    [display setText:[NSString stringWithFormat:@"%@", currentDigit]];
}

@end

Let us know what happens when you've set the label (display) and the action (digitPressed:) in InterfaceBuilder.

还在原地等你 2024-10-14 14:38:32

尝试将您的属性更改为复制(或保留,但复制对于这种情况更惯用):

@property (copy) IBOutlet UILabel *display;

您的分配属性没有递增字符串的引用计数,因此不能保证在您需要它时它仍然存在。

Try changing your property to copy (or retain, but copy is more idiomatic for this situation):

@property (copy) IBOutlet UILabel *display;

Your assign property is not incrementing the reference count for the string, thus there is no guarantee that it will still exist by the time you need it.

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