gpt4 book ai didi

ios - 快速连续多次创建 UIAlertView

转载 作者:行者123 更新时间:2023-11-28 18:35:34 26 4
gpt4 key购买 nike

在我的应用程序中,我打算让两个(或更多)设备共享和合并 NSDictionaries。我的 Multipeer Connectivity 工作完美无缺,我只是在每次传输时合并两个词典时遇到了问题。我现在有两个循环遍历两个字典的“for”循环。如果已经存在键/值对,系统会提示用户是否要覆盖已存在的对象或保留它。他们可以选择保留当前对象、覆盖当前对象、保留所有冲突对象或覆盖所有冲突对象。我到目前为止的代码:

-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{

pathChooser = 1;

NSLog(@"DATA RECEIVED: %d bytes!", data.length);

dataReceived = data;

receivedDataDict = [[NSMutableDictionary alloc] init];

receivedDataDict = [NSKeyedUnarchiver unarchiveObjectWithData:dataReceived];

for (key1 in receivedDataDict) {

NSLog(@"%@", key1);

if ([dataDict objectForKey:key1] == nil) {

NSLog(@"Writing new folder");

[dataDict setObject:[[NSMutableDictionary alloc] init] forKey:key1];

}

for (key2 in [receivedDataDict objectForKey:key1]) {

if ([[dataDict objectForKey:key1] objectForKey:key2] == nil) {

NSLog(@"Writing a new file");

[[dataDict objectForKey:key1] setObject:[[receivedDataDict objectForKey:key1] objectForKey:key2] forKey:key2];

}

else{

if (pathChooser == 1) {

NSLog(@"MADE IT TO -ALREADY EXISTS-");

UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle: [[NSString alloc] initWithFormat:@"Match %@ already exists in %@!", key2, key1]

message: @"Overwrite?"

delegate: self

cancelButtonTitle:@"Keep"

otherButtonTitles:@"Overwrite", @"Keep All", @"Overwrite All",nil];

[alert1 show];

}

else if (pathChooser == 3){

NSLog(@"Path 3");

[[dataDict objectForKey:key1] setObject:[[receivedDataDict objectForKey:key1] objectForKey:key2] forKey:key2];

}

}

}

}

[dataDict writeToFile:path atomically:YES];

NSLog(@"%@", dataDict);

receivedDataDict = nil;

}



// Buttons for UIAlertView...

-(void)alertView:(UIAlertView *)alert1 clickedButtonAtIndex:(NSInteger)buttonIndex{

//Keep Current

if (buttonIndex == 0) {

pathChooser = 1;

}



//Overwrite Current

else if (buttonIndex == 1) {

[[dataDict objectForKey:key1] setObject:[[receivedDataDict objectForKey:key1] objectForKey:key2] forKey:key2];

}



//Keep All

else if (buttonIndex == 2) {

pathChooser = 2;

}



//Overwrite All

else if (buttonIndex == 3) {

pathChooser = 3;

}



}

当两个 NSDictionaries 之间存在多个相似的键/值对时,我会遇到这个问题,这会快速连续多次创建 UIAlertView,这显然不是一件好事。有什么办法可以延迟每次创建 UIAlertView 吗?还有比这更明显的解决方案吗?我试过使用代码块,但代码块似乎没有读取“key1”和“key2”值,因为它们在 UIAlertView 中显示为“(null)”。我也在 [alert1 show] 之后的行中尝试了这种方法:

 while ((!alert1.hidden) && (alert1.superview != nil))

{

[[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];

}

但是直到第二次创建 UIAlertView 之后它似乎才起作用。我得到的错误是:

断言失败 -[UIKeyboardTaskQueue performTask:],/SourceCache/UIKit/UIKit-2903.23/Keyboard/UIKeyboardTaskQueue.m:388

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UIKeyboardTaskQueue performTask:] 只能从主线程调用。”

欢迎任何建议,我会尽力而为。我找不到很多其他人遇到与我相同的问题。

最佳答案

您始终可以在创建 UIAlertView 时执行“dispatch_async”。

所以,它最终看起来像:

__block UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:[[NSString alloc] initWithFormat:@"Match %@ already exists in %@!", key2, key1]
message:@"Overwrite?"
delegate:weakSelf
cancelButtonTitle:@"Keep"
otherButtonTitles:@"Overwrite", @"Keep All", @"Overwrite All",nil];
dispatch_async(dispatch_get_main_queue(), ^(void){

[alert1 show];
});

关于ios - 快速连续多次创建 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19796447/

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