I'm making an iphone app and I have met a problem..
I'm making sub views which contains labels and a UIStepper..
They are made by a for loop like so:
//subView to contain one ticket
UIView *ticketTypeView = [[UIView alloc]initWithFrame:CGRectMake(10, y, 1000, 60)];
if(ticketCount%2){
ticketTypeView.backgroundColor = [UIColor lightGrayColor];
}
[self.view addSubview:ticketTypeView];
//label for ticket type name
UILabel *ticketType = [[UILabel alloc]initWithFrame:CGRectMake(10, 3, 500, 50)];
[ticketType setText:string];
[ticketType setFont:[UIFont fontWithName:@"Helvetica neue" size:20.0]];
[ticketTypeView addSubview:ticketType];
//UIStepper for ticket amount
UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(500, 16, 0, 0)];
stepper.transform = CGAffineTransformMakeScale(1.2, 1.2);
[ticketTypeView addSubview:stepper];
//label for price pr. ticket
UILabel *pricePrTicket = [[UILabel alloc]initWithFrame:CGRectMake(620, 5, 100, 50)];
[pricePrTicket setText:@"1000.00 Kr."];
[ticketTypeView addSubview:pricePrTicket];
//totalPrice label
UILabel *totalTypePrice = [[UILabel alloc]initWithFrame:CGRectMake(900, 5, 100, 50)];
[totalTypePrice setText:@"0.00 Kr."];
[ticketTypeView addSubview:totalTypePrice];
Now.. How do I add a IBAction valueChanged for my UIStepper? the stepper is supposed to take the count, multiply it by the pricePrTicket and display it in the totalPrice label..
Any help or hint will be much appreciated :)
You'll need to assign unique tag
to all your subviews of ticketTypeView
(each should be unique) then follow @thedjnivek answer. When you get call - (void) stepperChanged:(UIStepper*)theStepper
method, get totalPrice
label object like this,
UILabel *ticketprice = (UILabel *)[theStepper.superview viewWithTag:kTagPriceTicket];
check if label object is not nil,
if(ticketprice) {
ticketprice.text = theStepper.value * pricePrTicket;
}
In your for loop where you're creating ticketTypeView
and other labels.
Your label tag should be unique for labels and the same for individual ticketTypeView
views.
Create tags like this (you can give any integer for tags),
#define kTagTicketType 110
#define kTagPriceTicket 111
#define kTagTotalTypePrice 112
...
...
...
[ticketType setTag:kTagTicketType]; //NOTE this
[pricePrTicket setTag:kTagPriceTicket]; //NOTE this
[totalTypePrice setTag:kTagTotalTypePrice]; //NOTE this
Write above lines before adding each of the label.