Question

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 hooked up with the addButton1: method, it crashes? Thanks in advance for the help!

Was it helpful?

Solution

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)];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top