2012-09-13 64 views
0

我试图添加一个UILabel programaticly(我做了它与IB和它的作品)。但我无法找出它为什么没有显示出来..我已经添加了一个按钮程序化,工作得很好。我希望按钮用随机引号更新标签。怎么了?UILabel不显示

的.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 
+0

您是否尝试过在UILabel上设置不同的'frame'以确保它不仅仅是被错误定位? – bdesham

+0

该标签是一个IBOutlet,所以我假设你已经在界面构建器中创建了它?如果是这样,请尝试删除'self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(20,71,200,83)]'。 –

+0

@NSPostWhenIdle没有,我编程添加它,删除了IBOutlet,但现在我明白它的作用了! :) – tobbe

回答

2

你的标签得到释放,利用你的休息@property (strong, nonatomic) IBOutlet UILabel *label1;

如果您在分配label1后在任意行上放置了一个断点,您将看到label1为零

+0

这不会导致标签不可见,因为他将它添加到视图中,并且视图始终保持对其子视图的强引用。 – DrummerB

+1

当使用弱引用时,它会在这一行之后被immidiatly释放:'self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(20,71,200,83)];'所以在添加它时它已经为零到视图。 – Bastian

+0

这样做确实解决了它:但我不明白为什么/如何? – tobbe

0

中的loadView的原因,你的观点不被初始化。它是通过XIB/StoryBoard连接的吗?

如果不是,

只是初始化您认为在您的方法第一线添加标签来

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

然后写的我们的代码

+0

它在IB storyBoard中初始化 – tobbe