我正在制作一款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];现在.。如何为我的IBAction valueChanged添加UIStepper?步进应该取计数,乘以pricePrTicket并显示在totalPrice标签中。
任何帮助或暗示都将不胜感激:)
发布于 2014-06-04 09:44:56
您需要将唯一的tag分配给您的ticketTypeView的所有子视图(每个子视图都应该是唯一的),然后按照@thedjnivek回答。当您调用- (void) stepperChanged:(UIStepper*)theStepper方法时,获得这样的totalPrice标签对象,
UILabel *ticketprice = (UILabel *)[theStepper.superview viewWithTag:kTagPriceTicket];检查标签对象是否为零,
if(ticketprice) {
ticketprice.text = theStepper.value * pricePrTicket;
}在创建ticketTypeView和其他标签的for循环中。
标签标记对于标签应该是唯一的,对于单个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在添加每个标签之前,先写上一行。
发布于 2014-06-04 09:36:49
您必须像这样使用addTarget:action:设置目标:
[stepper addTarget:self action:@selector(stepperChanged:) forControlEvents:UIControlEventValueChanged];
- (void) stepperChanged:(UIStepper*)theStepper{
//This method would be called on UIControlEventsValueChanged
}我希望这能帮助你;)
https://stackoverflow.com/questions/24033862
复制相似问题