2012-01-23 43 views
1

我在UILongPressGestureRecognizer上显示一个提醒,我面临的问题是,每次我必须点击三次才能解除警报,而只需单击一下按钮即可解除警报。为什么我必须解雇这个UIAlertView三次?

而且由于这种不正常的行为,入口得到核心数据复制..

我使用的代码是为下:

在的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; 
} 

在(空白)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");} 
} 

可能是什么问题?

回答

3

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

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

     // ... rest of code 
} 
+0

你能帮助我的代码片段,请 –

+0

与例如更新... –

+0

日Thnx @Simon李:)其解决 –

0

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

你的情况:

- (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]; 
} 
}