2012-01-23 13 views
1

私はUILongPressGestureRecognizerでアラートを表示しています。私が直面する問題は、アラートを1回クリックするだけで解除する必要がある間に、アラートを解除するたびに3回クリックする必要があることです。なぜこのUIAlertViewを3回却下する必要がありますか?

そして、これによる異常行動に、エントリはコアデータに重複します。..

私が使用していたコードは下の通りです: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]; 
} 
} 
関連する問題