2012-02-23 37 views
0

我實現了一個UIAlertView,它顯示一個tableView來做出選擇並返回到底層視圖控制器。爲什麼我的AlertView取消3次點擊取消按鈕關閉?

警報視圖彈出正常,顯示所有數據,當我點擊取消按鈕它沒有響應,然後我再次點擊它,它移動位置四分之一英寸,然後當我點擊它的第三個時間,alertView終於消失。

什麼會導致這種類型的行爲?

這是代碼:

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 
CGPoint p = [gestureRecognizer locationInView:self.tableView]; 

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; 
if (indexPath == nil){ 
    NSLog(@"long press on table view but not on a row"); 
} 
else { 
    NSLog(@"long press on table view at row %d", indexPath.row); 
    selectedQuote = [self.quotes objectAtIndex:indexPath.row]; 

    NSLog(@"   subject title = %@", selectedQuote.title); 

    // NOW PULL UP THE ADD QUOTE MAP CONTROLLER SO THIS QUOTE CAN BE ADDED TO ANOTHER CATEGORY 

    if(aqmController == nil) 
     aqmController = [[AddQuoteMapController alloc] initWithNibName:@"AddQuoteMapController" bundle:nil]; 

    aqmController.selectedQuote = self.selectedQuote; 

    //POP UP SBTableAlert 

    SBTableAlert *alert; 
    alert = [[SBTableAlert alloc] initWithTitle:@"Categorize this Quote" cancelButtonTitle:@"Cancel" messageFormat:@""]; 

    [alert setType:SBTableAlertTypeMultipleSelct]; 
    [alert.view addButtonWithTitle:@"OK"]; 
    [alert.view setTag:0]; 
    [alert setStyle:SBTableAlertStyleApple]; 

    [alert setDelegate:self]; 
    [alert setDataSource:self]; 

    [alert show]; 

} 

} 

#pragma mark - SBTableAlertDataSource 

- (UITableViewCell *)tableAlert:(SBTableAlert *)tableAlert cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell; 

cell = [[[SBTableAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 

Category *cat = [categories objectAtIndex:indexPath.section]; 

Subject *sub = [cat.subjects objectAtIndex:indexPath.row]; 

cell.textLabel.text =sub.title; 
cell.detailTextLabel.text = sub.category_title; 

return cell; 

} 

- (NSInteger)tableAlert:(SBTableAlert *)tableAlert numberOfRowsInSection:(NSInteger)section { 

Category *cat = [categories objectAtIndex:section]; 
return cat.subjects.count; 

} 

- (NSInteger)numberOfSectionsInTableAlert:(SBTableAlert *)tableAlert { 

QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate]; 
self.categories = [appDelegate categories]; 

return self.categories.count; 

} 

- (NSString *)tableAlert:(SBTableAlert *)tableAlert titleForHeaderInSection:(NSInteger)section { 

Category *cat = [categories objectAtIndex:section]; 
NSString *title = [cat category_title]; 

return title; 
} 

#pragma mark - SBTableAlertDelegate 

- (void)tableAlert:(SBTableAlert *)tableAlert didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

Category *cat = [categories objectAtIndex:indexPath.section]; 

Subject *sub = [cat.subjects objectAtIndex:indexPath.row]; 

selectedSubject = sub; 
NSLog(@"selectedSubject = %@", selectedSubject.title); 

//INSERT INTO QUOTEMAP TABLE 

// GET SUBJECT_ID 
NSString *stringOfSubjectId = [NSString stringWithFormat:@"%d", selectedSubject.subject_id]; 

NSLog(@"mySubjectId= %@", stringOfSubjectId); 

// GET THE NEXT QUOTE_MAP_ID 

QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate]; 
QuoteMap *qm = [[QuoteMap alloc] init]; 

NSInteger newQuoteMapId = [appDelegate getNextQuoteMapId]; 
NSLog(@"quote_map_id= %d subId = %@ quoteId = %@", newQuoteMapId, stringOfSubjectId, selectedQuote.quote_id); 

// INSERT INTO QUOTE_MAP TABLE 
NSString *stringOfId = [NSString stringWithFormat:@"%d", newQuoteMapId]; 

qm.quote_map_id = stringOfId; 
qm.subject_id = stringOfSubjectId; 
qm.quote_id  = selectedQuote.quote_id; 
//qm.isDirty  = YES; 

[qm insertQuoteMap:qm]; 

//Add it to the array. 
[qmv.quoteMaps addObject:qm]; 
[qmv.tableView reloadData]; 

if (tableAlert.type == SBTableAlertTypeMultipleSelct) { 
    UITableViewCell *cell = [tableAlert.tableView cellForRowAtIndexPath:indexPath]; 
    if (cell.accessoryType == UITableViewCellAccessoryNone) 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    else 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 

    [tableAlert.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

//release 
[qm autorelease]; 


} 

- (void)tableAlert:(SBTableAlert *)tableAlert didDismissWithButtonIndex:(NSInteger)buttonIndex  { 
NSLog(@"Dismissed: %i", buttonIndex); 

[tableAlert release]; 
} 

- (void)dealloc{ 

[qmv release]; 
[subjects release]; 
[categories release]; 
[selectedSubject release]; 

} 
+0

_alertView如何設置?什麼是SBTableAlert?看起來像很多相關的代碼缺失。 – picciano 2012-02-23 23:18:51

+0

我包含_alertView正在設置的位置。這裏描述了SBTableAlert:[SBTableAlert](https://github.com/blommegard/SBTableAlert/blob/master/README.markdown) – jroyce 2012-02-23 23:34:44

+1

在handleLongPress中,您需要檢查手勢識別器的'state'屬性,因爲方法將會是由識別器多次調用。見http://stackoverflow.com/questions/8971237/uialertview-issue-iphone,http://stackoverflow.com/questions/3243812/uilongpressgesturerecognizer-issue,http://stackoverflow.com/questions/7688329/uilongpressgesturerecognizer-on -uitableviewcell-double-call,等等。 – Anna 2012-02-24 01:44:01

回答

2

也許你應該使用SBTableAlertDatasource和SBTableAlertDelegate方法,而不是UITableViewDatasource和的UITableViewDelegate方法。

另外手勢是UILongPressGestureRecognizer。這是一個持續的認識者。您需要檢查識別器的狀態,並忽略所有發生在UIGestureRecognizerStateBegan後的事件。

+0

可能。您有使用此SBTableAlert的經驗嗎?除此之外,其他一切都很完美。提供的示例使用UITableViewDataSource和UITableViewDelegates從我可以告訴,所以我不認爲這是錯誤。 – jroyce 2012-02-23 23:44:33

+0

我也查看了示例,它不使用這些方法,它使用SBTableAlert數據源和委託方法。 https://github.com/blommegard/SBTableAlert/blob/master/SBTableAlertDemo/SBTableAlertDemo/RootViewController.m – dbrajkovic 2012-02-23 23:47:44

+0

感謝您指出這一點。你不得不原諒我的天真,但我試圖這樣做的方式是編輯SBTableAlert.m文件本身,而不是使用單獨的rootViewController。我將嘗試重做它以更好地複製樣本。 – jroyce 2012-02-24 00:05:38

相關問題