ios - 通过变量使 UIAlertView 触发

标签 ios objective-c uialertview

一些二维码扫描器应用程序会在完成扫描后通过警报显示结果(URL 或其他东西),所以我想做同样的事情并通过警报显示我的视频处理结果(整数)。我的视频处理函数是一个委托(delegate)方法。我阅读了一些 UIAlertView 示例,但需要按钮来触发警报。在我的例子中,需要在计算变量 result 后显示警报。但是,如果我在 processImage 函数中添加警报:

- (void)processImage:(cv::Mat&)image {
    int result;
    videoProcessing() {
    ...      
    result = 10
    }

    if (result == 10) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The code is" message:@"10" 
        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}    

我的应用程序将因错误而终止:

2014-04-17 11:11:02.189 DotReader[3813:1803] *** Assertion failure in -[UIKeyboardTaskQueue performTask:], /SourceCache/UIKit/UIKit-2935.137/Keyboard/UIKeyboardTaskQueue.m:388
2014-04-17 11:11:02.190 DotReader[3813:1803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue performTask:] may only be called from the main thread.'
*** First throw call stack:
(0x183ab6950 0x18ffbc1fc 0x183ab6810 0x1845eedb4 0x186aa8fc0 0x186aa8eec 0x186aa8b50 0x186aa6588 0x186aa565c 0x186f811d0 0x186f81698 0x186b00c7c 0x186affa04 0x186f83010 0x10007ad0c 0x10013c374 0x1827b8434 0x190594014 0x190593fd4 0x19059a4a8 0x1905964c0 0x19059b0f4 0x19059b4fc 0x1907296bc 0x19072954c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

谁能告诉我如何正确添加UIAlertView

最佳答案

所有的 UI 事件都应该从主线程调用

if (result == 10) {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The code is" message:@"10"
                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    });

}

关于ios - 通过变量使 UIAlertView 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124136/

相关文章:

ios - Ionic 3. 如何使用 cli 定位特定版本的 iOS 模拟器?

ios - 无法从 IPv6 连接到 IPv4 错误域=NSURLErrorDomain 代码=-1003“找不到具有指定主机名的服务器

ios - UITableViewCell 内的 UITextField : becomeFirstResponder in didSelectRowAtIndexPath

ios - 检测设备是否为 iPhone X

objective-c - 设置 NSSetUncaughtExceptionHandler 时出错 ..

iphone - 如何仅在月份、日期和年份中更改我的日期选择器显示

objective-c - 使用openssl编译报错

ios - 如何使用 iOS 中的客观 C 单元测试处理整个应用程序中随时出现的警报?

ios - 如何将 `accessibilityLabel` 添加到 `UIAlertView` 按钮?

iphone - UIAlertView 替换