为什么我要三次解雇这个UIAlertView?

时间:2012-01-23 11:57:50

标签: iphone objective-c ios

我正在UILongPressGestureRecognizer上显示警告,我面临的问题是,每次我必须点击三次才能解除警报,而单击按钮时警报应该被解除。

由于这种异常行为,条目会在核心数据中重复出现..

我使用的代码如下:

在cellForRowAtIndexPath中:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // NSString * cellValue;
    if (tableView == listTable) {
       cellValue = [listVehicles objectAtIndex:indexPath.row];
    } else { // handle search results table view
        cellValue = [filteredListItems objectAtIndex:indexPath.row];
    }

    static NSString *CellIdentifier = @"vlCell";

    VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSLog(@"Cell Created");

        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil];

        for (id currentObject in nibObjects) {
            if ([currentObject isKindOfClass:[VehicleListCell class]]) {
                cell = (VehicleListCell *)currentObject;
            }
        }

        UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
        pressRecongnizer.minimumPressDuration = 0.5f;
        [cell addGestureRecognizer:pressRecongnizer];
        [pressRecongnizer release];
    }

    cell.textLabel.font = [UIFont systemFontOfSize:10];

    [[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]];
    [[cell direction] setImage:[UIImage imageNamed:@"south.png"]];

    cell.licPlate.text = cellValue;

    NSLog(@"cellvalue for cellforRow: %@", cell.licPlate.text);

    return cell;
}

In(void)tableCellPressed:(UILongPressGestureRecognizer *)识别方法

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer {
    VehicleListCell* cell = (VehicleListCell *)[recognizer view];
    cellValueForLongPress = cell.licPlate.text;

    NSLog(@"cell value: %@", cellValueForLongPress);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;

    [alert addButtonWithTitle:@"Add to Favourites"];
    [alert addButtonWithTitle:@"Take to Map"];

    [alert show];
}

在警报视图方法中:

-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {

    NSString *title = [alert buttonTitleAtIndex:buttonIndex];

    NSManagedObjectContext *context = [app managedObjectContext];
    Favouritesdata * favourites = [NSEntityDescription insertNewObjectForEntityForName:@"Favouritesdata" inManagedObjectContext:context];

    if([title isEqualToString:@"Add to Favourites"])
    {
        NSLog(@"Added to favourites.");

        NSLog(@"cellValueForLongPress: %@", cellValueForLongPress);


        if (cellValueForLongPress <= 0) {
            NSLog(@"There is no value to save");
        }
        else {
            favourites.licenseplate = cellValueForLongPress;
        }

        [alert dismissWithClickedButtonIndex:0 animated:YES];
    }
    else if([title isEqualToString:@"Take to Map"])
    {
        NSLog(@"Go to MapView");
    }

    NSError *error;

    if (![context save:&error]) {
        NSLog(@"Error Occured");} 
}

可能是什么问题?

2 个答案:

答案 0 :(得分:3)

问题是你的长按手势识别器会触发每个状态,开始,结束等。如果事件的状态不是'开始',那么你应该返回而不执行后续代码。

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer { 
       if (recognizer.state != UIGestureRecognizerStateBegan) {
            return;
        }

       // ... rest of code
}

答案 1 :(得分:0)

它会多次触发以指示手势的不同状态(开始,更改,结束等)。因此,在处理程序方法中,检查手势识别器的state属性,以避免在手势的每个状态下执行操作。

在你的情况下:

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer 
{
VehicleListCell* cell = (VehicleListCell *)[recognizer view];
cellValueForLongPress = cell.licPlate.text;

NSLog(@"cell value: %@", cellValueForLongPress);


if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;

[alert addButtonWithTitle:@"Add to Favourites"];
[alert addButtonWithTitle:@"Take to Map"];

[alert show];
}
}