我正在制作iphone应用,但我遇到了一个问题..
我正在创建包含标签和UIStepper的子视图..
它们是由for循环制作的:
//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];
现在.. 如何为我的UIStepper添加IBAction值更改?步进器应该计数,将它乘以pricePrTicket并将其显示在totalPrice标签中。
非常感谢任何帮助或提示:)
答案 0 :(得分:1)
您必须使用addTarget:action:
设置目标:
[stepper addTarget:self action:@selector(stepperChanged:) forControlEvents:UIControlEventValueChanged];
- (void) stepperChanged:(UIStepper*)theStepper{
//This method would be called on UIControlEventsValueChanged
}
我希望能帮到你;)
答案 1 :(得分:1)
您需要为tag
的所有子视图分配唯一的ticketTypeView
(每个都应该是唯一的),然后按照@thedjnivek回答。当您收到调用- (void) stepperChanged:(UIStepper*)theStepper
方法时,请获取totalPrice
这样的标签对象,
UILabel *ticketprice = (UILabel *)[theStepper.superview viewWithTag:kTagPriceTicket];
检查标签对象是否为零,
if(ticketprice) {
ticketprice.text = theStepper.value * pricePrTicket;
}
在for循环中,您要创建ticketTypeView
和其他标签。
您的标签标签对于标签应该是唯一的,对于单个ticketTypeView
视图应该是相同的。
像这样创建标签(你可以为标签提供任何整数),
#define kTagTicketType 110
#define kTagPriceTicket 111
#define kTagTotalTypePrice 112
...
...
...
[ticketType setTag:kTagTicketType]; //NOTE this
[pricePrTicket setTag:kTagPriceTicket]; //NOTE this
[totalTypePrice setTag:kTagTotalTypePrice]; //NOTE this
在添加每个标签之前写上面的行。