gpt4 book ai didi

iphone - 为什么我必须关闭这个 UIAlertView 三次?

转载 作者:行者123 更新时间:2023-12-01 19:23:38 27 4
gpt4 key购买 nike

我在 UILongPressGestureRecognizer 上显示警报,我面临的问题是每次我必须单击它三次才能解除警报,而单击按钮应该解除警报。

由于这种异常行为,核心数据中的条目会重复。

我正在使用的代码如下:

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

在 (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer 方法中
- (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];
}

在警报 View 方法中:
-(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");}
}

可能是什么问题?

最佳答案

问题是您的长按手势识别器会为每个状态、开始、结束等触发。如果事件的状态不是“开始”,那么您应该返回而不执行后续代码。

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

// ... rest of code
}

关于iphone - 为什么我必须关闭这个 UIAlertView 三次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971237/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com