gpt4 book ai didi

ios UIAlertView : make alert wait for my response

转载 作者:行者123 更新时间:2023-12-03 18:17:00 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Make UIAlertView blocking

在这个问题上我已经用头撞墙好几个小时了。

这是我得到的:OrderDetailsViewController 被设置为 UIAlertViewDelegate

我有一个从搜索表单接收信息的过程。它会检查该商品是否已在订单上,如果不在,则继续添加该商品。如果它看到重复项,它会弹出一个 UIAlertView 询问用户想要做什么:有 3 个选项,“合并”- 将新数量添加到旧项,“添加”重复项作为单独的行项,或“取消”丢弃新项目。我需要它来等待 UIAlertView 的回答,以便我可以继续添加欺骗或丢弃欺骗——“组合”在委托(delegate)中处理,但我仍然需要对主要过程的回答。

这是我目前所拥有的:

 - (void)returnItemAndQty:(ProductsSearchController *)productsSearchController
withItemsToAdd:(NSMutableArray *)itemsToAdd
withQty:(NSDictionary *)qtyToAdd andClose:(BOOL)close
{
if ([itemsToAdd count] == 0) {
return;
}
Items *items;
for (int index = 0; index < [itemsToAdd count]; index++) {
items = [itemsToAdd objectAtIndex:index];
qtyAddedToOrder = [NSDecimalNumber decimalNumberWithString:[qtyToAdd objectForKey:items.ItemCode]];
NSLog(@"Item Code: %@", items.ItemCode);
NSLog(@"Qty: %@", [qtyToAdd objectForKey:items.ItemCode]);
NSError *error;

//For handling duplicate items. . .
duplicateItemDisposition = -1; //Reset the dispostion for normal operation
if([self isItemOnOrder:items.ItemCode])
{
int i = [self itemIsAlreadyOnOrder:itemAlreadyOnOrder withQty:qtyAddedToOrder];

if (i == COMBINE || i == CANCEL){ //either Cancel or Combine was pressed.
items.Checked = NO;
if (![items.managedObjectContext save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
continue;
}
}
//Add the new item or duplicate if that's what you want
OrdersDetails *newOrderDetail = [NSEntityDescription insertNewObjectForEntityForName:@"OrdersDetails"
inManagedObjectContext:self.managedObjectContext];

.//more code snipped, that handles the "ADD" or non-dupe
.
.

这是它测试重复项的地方。 . .

- (BOOL)isItemOnOrder:(NSString *)itemCode
{

NSFetchRequest *request = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"OrdersDetails"
inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"Desc1" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sort, nil];
[request setSortDescriptors:sortDescriptors];
[sort release];
[sortDescriptors release];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(ItemCode=%@ AND OrderID=%@)", itemCode, orders.OrderID];
[request setPredicate:pred];

NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:& error] mutableCopy];
[request release];
if (mutableFetchResults == nil) {
itemAlreadyOnOrder = nil;
return NO;
}else if ([mutableFetchResults count] > 0){
itemAlreadyOnOrder = [mutableFetchResults objectAtIndex:0];
return YES;
}else{
itemAlreadyOnOrder = nil;
return NO;
}
}

这是它看到存在欺骗的地方以及 UIAlertview 委托(delegate)的地方。 . .

- (int) itemIsAlreadyOnOrder:(OrdersDetails *)existingOrderDetail withQty:(NSDecimalNumber *)qty 
{
if (existingOrderDetail == nil) {
return -1;
}

UIAlertView *duplicateAlert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Duplicate Item %@ found.",existingOrderDetail.ItemCode] message:@"Tap Combine to combine the items, Tap Add to add the duplicate item or Tap Cancel to discard the duplicate" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Combine", @"Add", nil];
[duplicateAlert show];

return duplicateItemDisposition;

[duplicateAlert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Cancel = 0, Combine = 1, Add = 2

if (buttonIndex == CANCEL){
duplicateItemDisposition = CANCEL;
}else if (buttonIndex == COMBINE){
duplicateItemDisposition = COMBINE;
NSDecimalNumber *existingQty = [[NSDecimalNumber alloc] initWithDecimal:[itemAlreadyOnOrder.Qty decimalValue]];
NSDecimalNumber *existingPrice = itemAlreadyOnOrder.Price;
NSDecimalNumber *newQty = [existingQty decimalNumberByAdding:qtyAddedToOrder];
itemAlreadyOnOrder.ExtPrice = [newQty decimalNumberByMultiplyingBy:existingPrice];
[existingQty release];
NSError *error;
if (![itemAlreadyOnOrder.managedObjectContext save:&error]){
NSLog(@"Error saving. %@, %@", error, [error userInfo]);
[self handleFreeGoods:itemAlreadyOnOrder];
}else if (buttonIndex == ADD){
duplicateItemDisposition = ADD;
}
}

现在我在这里读到一些关于在后台线程中使用 NSCondition 的内容,但我不知道那是什么意思。我查阅了 NSCondition,它没有启发性。

关于如何暂停执行有什么想法吗?

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