gpt4 book ai didi

IOS 从以编程方式制作的标签和步进器中减去

转载 作者:行者123 更新时间:2023-11-29 02:54:02 26 4
gpt4 key购买 nike

我在做一个售票的ipad应用,想减去的时候遇到了问题..

UI是这样制作的:

  for(NSString *string in ticketArray){

float y = 60*ticketCount+spaceFloat;

//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);
[stepper addTarget:self action:@selector(chanegestep:) forControlEvents:UIControlEventValueChanged];
stepper.maximumValue = 100;
stepper.minimumValue = 0;
[ticketTypeView addSubview:stepper];

//label for price pr. ticket
UILabel *pricePrTicket = [[UILabel alloc]initWithFrame:CGRectMake(620, 5, 100, 50)];
[pricePrTicket setText:@"1000.50"];
pricePrTicket.tag = kTagPriceTicket;
[ticketTypeView addSubview:pricePrTicket];

//totalPrice label
UILabel *totalTypePrice = [[UILabel alloc]initWithFrame:CGRectMake(900, 5, 100, 50)];
[totalTypePrice setText:@"0.00 Kr."];
totalTypePrice.tag = kTagTotalTypePrice;
[ticketTypeView addSubview:totalTypePrice];

ticketCount++;
}

我的 ValueChanged 方法:

 - (IBAction) chanegestep:(UIStepper *)sender {
double va = [sender value];
NSLog(@"stepper pressed");


//get the 2 labels for the price and for displaying price
UILabel *ticketprice = (UILabel *)[sender.superview viewWithTag:kTagPriceTicket];
UILabel *totalPrice = (UILabel *)[sender.superview viewWithTag:kTagTotalTypePrice];
double price = [ticketprice.text doubleValue];



//calc the total from that one ticet type
double totalDisplayed = va*price;

//add to the complete sales amount
completeSalesAmount += totalDisplayed;


NSLog(@"%.02f",completeSalesAmount);
if(ticketprice){
totalPrice.text = [[NSString alloc]initWithFormat:@"%.02fKr.",totalDisplayed];
}

//activate btnContinue
if(completeSalesAmount!=0){
[btnContinue setEnabled:YES];
}




//[ setText:[NSString stringWithFormat:@"%d", (int)va]];

我的问题:将数字相加效果很好,但是当按下步进器上的“-”时,我必须以某种方式检测到它,并从 totalSalesAmount 中减去给定票的价格..

感谢任何帮助。

问候,即将到来的 IOS 开发者 xD

编辑:

每次调用 valueChanged 时都会计算 completedSalesAmount..

假设我有 2 种票证类型,每种票证类型有 1 个选择。

ticket1 - price 10$ - amount 1, total 10$ (10*1)

completeSalesAmount - 0 += (10*1) = 10$

ticket2 - price 10$ - amount 1, total 10$ (10*1)

completeSalesAmount - 10 += (10*1) = 20$

现在在 ticket1 上按下“-”

ticket1 - price 10$ - amount 0, total 0$ (10*0)

completeSalesAmount += 0...

最佳答案

问题是您不知道按下了步进器上的哪个按钮。你可以做的是像这篇文章建议的那样将你的 UIStepper 子类化:

UIStepper. Find out whether incremented or decremented

否则你可以创建一个函数来计算每个 UIStepper 的 completeSalesAmount,你需要将你的 UIStepper 包装器放在一个数组中:

NSMutableArray *fullSteppers = [NSMutableArray arrayWithCapacity:[ticketArray count]];    

for(NSString *string in ticketArray){
...
stepper.tag = kTagStepper;
[fullSteppers addObject:ticketTypeView];
...
}

- (void)updateCompleteSalesAmount{
completeSalesAmount = 0;
for(UIView *fullStepper in fullSteppers){
UILabel *ticketprice = (UILabel *)[fullStepper viewWithTag:kTagPriceTicket];
UILabel *totalPrice = (UILabel *)[fullStepper viewWithTag:kTagTotalTypePrice];
double price = [ticketprice.text doubleValue];

UIStepper *stepper = (UIStepper *)[fullStepper viewWithTag:kTagStepper];
completeSalesAmount += stepper.value * price;
}
}

只需在您的 changestep 方法中调用更新函数:

- (IBAction) chanegestep:(UIStepper *)sender {
....
[self updateCompleteSalesAmount];
}

关于IOS 从以编程方式制作的标签和步进器中减去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24135771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com