2012-01-23 3 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

통해 UR의 내 많은 저장된 ... –

+0

thnx @ 사이먼 리 : 그 해결 –

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]; 
} 
} 
관련 문제