我正在制作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 valueChanged?该步进器应该进行计数,将其乘以pricePrTicket并将其显示在totalPrice标签中。

任何帮助或提示将不胜感激:)

最佳答案

您需要为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


在添加每个标签之前,在上面几行中写下。

10-07 21:23