在这个人身上撞了几个小时。
这就是我所拥有的:OrderDetailsViewController设置为UIAlertViewDelegate
我有一个从搜索表单接收信息的程序。它检查物品是否已经在订单上,如果没有,则继续添加物品。如果它看到重复,它会弹出UIAlertView询问用户想要做什么:有3个选项,“合并” - 将新数量添加到旧项目,“添加”副本作为单独的行项目,或“取消“扔掉新物品。我需要等待来自UIAlertView的答案,以便我可以继续添加欺骗或丢弃欺骗 - “委托”在委托中处理,但我仍然需要主程序的答案。
这是我到目前为止所拥有的:
- (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委托它。 。
- (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,它不是很有启发性。
关于如何暂停执行的任何想法?
答案 0 :(得分:2)
UIAlertView
无法阻止,因为它会显示在runloop的末尾。方法itemIsAlreadyOnOrder:
不返回指示应对重复条目执行的操作的值,而UIAlertView
中的委托方法需要通知您的控制器已解决相关项。跟踪相关对象(_objectToVerify
或类似的东西),并在UIAlertView
的委托方法中调用基于用户选择的方法,该方法将作用于_objectToVerify
。
答案 1 :(得分:0)
我不知道这可能会改变你当前的设计多少,但是Cocoa设计模式会有一个提醒警报不返回任何内容的方法,所以像伪代码中的if (duplicate) showAlertView
。然后在警报视图的委托中,根据用户输入调用实际处理副本处理的方法。