开发者

UILabel's text will not change

Hey guys i am creating a UILabel and a UIButton programatically inside another button, so when this button is pressed, it creates a UILabel and a UIButton and adds is as a subview on a seperate page. I want a button to be able to add 1 to the labels value. Here is the code for the button that adds the label to the second view:

-(IBAction)outlet1:(id)sender{
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[addButton addTarget:self action:@selector(addButton1:)    
forControlEvents:UIControlEventTouchUpInside];
[addButton setTitle:@"+" forState:UIControlStateNormal];
addButton.frame = CGRectMake(80.0, 210.0, 35, 40.0);
[cart.view addSubview:addButton];

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(137, 150, 25, 35)];
label.backgroundColor = [UIColor clearColor];
label.text = @"1";
[cart.view addSubview:label];
}

and this is the button i want to be able to alter the label:

- (void)addButton1:(id)sender {
[black.label setText:[NSString stringWithFormat:@"%d",[black.label.text intValue] +1]];    
}

black is an ivar that is associated with the first viewController and cart is assoctiated with the second viewController!My problem is when i press the button that is hooke开发者_运维知识库d up with the addButton1: method, it crashes? Thanks in advance for the help!


You need to init your UILabel in viewDidLoad and create an ivar to it, then you can access it throughout your @implementation.

@synthesize label;

- (void) viewDidLoad
{
    label = [[UILabel alloc] initWithFrame:CGRectMake(137, 150, 25, 35)];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜