Objective c 为什么我的AlertView需要点击3次取消按钮才能关闭?
我实现了一个UIAlertView,它显示一个tableView来进行选择并返回到基础视图控制器 警报视图弹出良好,显示所有数据,当我单击“取消”按钮时,它没有响应,然后我再次单击它,它将位置移动四分之一英寸,然后当我第三次单击它时,警报视图最终消失 什么会导致这种行为 代码如下:Objective c 为什么我的AlertView需要点击3次取消按钮才能关闭?,objective-c,tableview,uialertview,Objective C,Tableview,Uialertview,我实现了一个UIAlertView,它显示一个tableView来进行选择并返回到基础视图控制器 警报视图弹出良好,显示所有数据,当我单击“取消”按钮时,它没有响应,然后我再次单击它,它将位置移动四分之一英寸,然后当我第三次单击它时,警报视图最终消失 什么会导致这种行为 代码如下: -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { CGPoint p = [gestureRecognizer lo
-(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];
}
也许您应该使用SBTableAlertDatasource和SBTableAlertDelegate方法,而不是UITableViewDatasource和UITableviewDelegate方法
此外,手势识别器还支持长按手势识别器。这是一个连续识别器。您需要检查识别器的状态,并忽略UIgestureRecognitizerStateStart之后发生的所有事件如何设置_alertView?什么是SBTableAlert?似乎缺少很多相关代码。我包括了设置_alertView的位置。SBTableAlert如下所述:在handleLongPress中,您需要检查手势识别器的
状态
属性,因为识别器将多次调用该方法。看,等等,好吧,我可以说你很棒。那太简单了,我本可以花上几个小时!非常感谢你!我不确定我是如何接受这个答案的,但这正是问题所在。可能吧。您是否有使用此SBTableAlert的经验?除了这一点,其他一切都很完美。根据我所知,提供的示例使用UITableViewDataSource和UITableViewDelegates,因此我认为这不是错误。我也查看了示例,它没有使用这些方法,而是使用SBTableAlert数据源和委派方法。谢谢你指出这一点。您必须原谅我的天真,但我尝试的方法是编辑SBTableAlert.m文件本身,而不是使用单独的rootViewController。我将尝试重新制作,以更好地复制样品。没问题。当使用第三方类时,通常不会修改源来实现它。首先,如果创建者更新了代码、修复了bug或者其他什么,你所要做的就是下载新代码并复制到现有文件上。第二,如果您想自定义未创建的类的行为,您应该创建子类或类别方法。如果此问题回答了您的问题,请将此问题标记为已回答。谢谢