Objective c UILabel不显示

Objective c UILabel不显示,objective-c,cocoa-touch,Objective C,Cocoa Touch,我正试图通过编程方式添加UILabel(我用IB完成了这项工作,而且效果很好)。但是我不知道为什么它没有出现。。我通过编程添加了一个按钮,效果很好。我希望按钮用随机引用更新标签。怎么了 .h文件: @interface SecondViewController : UIViewController { NSArray *citatDatabas; } @property (weak, nonatomic) IBOutlet UIButton *helloButton; @proper

我正试图通过编程方式添加UILabel(我用IB完成了这项工作,而且效果很好)。但是我不知道为什么它没有出现。。我通过编程添加了一个按钮,效果很好。我希望按钮用随机引用更新标签。怎么了

.h文件:

@interface SecondViewController : UIViewController
{
    NSArray *citatDatabas;

}

@property (weak, nonatomic) IBOutlet UIButton *helloButton;
@property (weak, nonatomic) IBOutlet UIButton *citatKnapp;
@property (weak, nonatomic) IBOutlet UILabel *label1;

- (void)helloButtonPressed;
- (IBAction)citatKnappPressed:(id)sender;

@end
实施:

@implementation SecondViewController
@synthesize helloButton, citatKnapp, label1;

- (void)loadView {
    [super loadView]; 
citatDatabas = [NSArray arrayWithObjects:
                    [NSString stringWithFormat:@"%@", @"People ask me, would you rather be feared or loved, um easy, I want people to be afriad of how much they love me - Michael Scott" ],
                    [NSString stringWithFormat:@"%@", @"You miss 100% of all the shoots you dont take - Wayne Greatsky - Michael Scott" ],
                    [NSString stringWithFormat:@"%@", @"DONT PANIC - hitchhiker's guide to the galaxy" ],
                    [NSString stringWithFormat:@"%@", @"Give someone a mask and they'll show their true face - Oscar Wilde" ],
                    [NSString stringWithFormat:@"%@", @"Given enough time, hydrogen starts to wonder where it came from, and where it is going" ],
                    [NSString stringWithFormat:@"%@", @"You have just begun reading the sentence you just finished reading" ],
                    nil];



    self.citatKnapp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.citatKnapp.frame = CGRectMake(98, 162, 124, 37);
    [self.citatKnapp setTitle:@"Tryck för citat" forState:UIControlStateNormal];
    [self.citatKnapp addTarget:self action:@selector(citatKnappPressed:)     forControlEvents:UIControlEventTouchUpInside];

    self.citatKnapp.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

    [self.view addSubview:self.citatKnapp];

    self.label1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 71, 200, 83)];

    self.label1.frame = CGRectMake(20, 71, 200, 83);
    self.label1.text = @"hej";
    self.label1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.label1];


}

- (IBAction)citatKnappPressed:(id)sender{

    self.label1.text = [citatDatabas objectAtIndex:
                  (random() % [citatDatabas count])];
}

@end

原因是在loadView中,您的视图未初始化。是否通过XIB/故事板连接

如果没有,

只需将视图初始化为方法中添加标签的第一行

self.view = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];

然后写下你剩下的代码

你的标签将被释放,使用
@property(强,非原子)IBOutlet UILabel*label1


如果在分配label1后在任何一行上放置断点,您将看到label1为nil

您是否尝试在UILabel上设置不同的
,以确保它不仅仅是定位错误?该标签是一个IBOutlet,因此我是否假设您已在interface builder中创建了它?如果是这样,请尝试删除
self.label1=[[UILabel alloc]initWithFrame:CGRectMake(20,71,200,83)]
@nspostwenidle no我通过编程添加了它,删除了IBOutlet,但现在我明白它的作用了!:)这不会导致标签不可见,因为他正在将其添加到视图中,并且视图始终保持对其子视图的强引用。当使用弱引用时,它将在这一行之后立即释放:
self.label1=[[UILabel alloc]initWithFrame:CGRectMake(20,71,200,83)]因此,当他将其添加到视图中时,它将已经是零。准确地说,这确实解决了它:O但我不明白为什么/如何?弱引用只会持续,只要有不同的东西将其保持为强引用。如果你创建一个对象,只把它放在一个弱变量中,它将无法在这一行代码中生存。但是当我用IB在故事板中创建同一个标签时,它显示@property(弱,非原子)IBUILabel*标签;这样行吗?这就是为什么我不理解它,我想它是在IB故事板中初始化的