tvOS 应用程序 - UIAlertController 与 UIAlertAction 崩溃
tvOS app - UIAlertController crashes with UIAlertAction
我有一个单视图 tvOS 应用程序。它有一个包含 24 个 collectionviewcell 的集合视图。选择 collectionviewcell 时,我会触发警报。我正在使用 Apple 参考文档(粘贴在下面)中的默认代码。
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction]; // This is what I comment out
[self presentViewController:alert animated:YES completion:nil];
当我没有将 defaultAction 添加到警报时(注释掉 [alert addAction:defaultAction];),警报会正确显示但无法关闭。当我将 defaultAction 添加到警报时,警报会显示 'OK' 按钮,但一旦它出现在屏幕上就会收到可怕的 SIGABRT 错误
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[_UIAlertControllerActionView image]: unrecognized selector
sent to instance 0x7fe571ddee10'
此处的堆栈跟踪:
2015-11-10 19:42:02.927 AppABC[1701:97632] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIAlertControllerActionView image]: unrecognized selector sent to instance 0x7fefd3ddaf10'
*** First throw call stack:
(
0 CoreFoundation 0x000000010de0a0b5 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010d884deb objc_exception_throw + 48
2 CoreFoundation 0x000000010de126dd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010dd6005a ___forwarding___ + 970
4 CoreFoundation 0x000000010dd5fc08 _CF_forwarding_prep_0 + 120
5 IsItGood 0x000000010d381170 __82-[ViewController collectionView:didUpdateFocusInContext:withAnimationCoordinator:]_block_invoke78 + 48
6 UIKit 0x000000010e260df4 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 582
7 UIKit 0x000000010e2612ec +[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:] + 63
8 IsItGood 0x000000010d380ca8 -[ViewController collectionView:didUpdateFocusInContext:withAnimationCoordinator:] + 536
9 UIKit 0x000000010ea24e09 -[UICollectionView _didUpdateFocusInContext:withAnimationCoordinator:] + 1181
10 UIKit 0x000000010eb15a5e _UIFocusEnvironmentDidUpdateFocus + 628
11 UIKit 0x000000010e4fe102 __36-[UIScreen _updateFocusWithContext:]_block_invoke + 88
12 UIKit 0x000000010e4fe161 __36-[UIScreen _updateFocusWithContext:]_block_invoke + 183
13 UIKit 0x000000010e4fdb9f -[UIScreen _updateFocusWithContext:] + 1912
14 UIKit 0x000000010e4feca9 -[UIScreen updateFocusIfNeeded] + 597
15 UIKit 0x000000010e1c1e4a _runAfterCACommitDeferredBlocks + 317
16 UIKit 0x000000010e1d53d9 _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
17 UIKit 0x000000010e1e1312 _afterCACommitHandler + 90
18 CoreFoundation 0x000000010dd35ab7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
19 CoreFoundation 0x000000010dd35a27 __CFRunLoopDoObservers + 391
20 CoreFoundation 0x000000010dd2b67b __CFRunLoopRun + 1147
21 CoreFoundation 0x000000010dd2af78 CFRunLoopRunSpecific + 488
22 GraphicsServices 0x0000000111582ad2 GSEventRunModal + 161
23 UIKit 0x000000010e1b608d UIApplicationMain + 171
24 AppABC 0x000000010d3826af main + 111
25 libdyld.dylib 0x000000011050d9e9 start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
有什么想法吗??
我尝试了您的代码并创建了一个新的单视图应用程序,具有一个按钮和一个按钮操作。按钮操作包含您的代码片段:
- (IBAction)action:(id)sender {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction]; // This is what I comment out
[self presentViewController:alert animated:YES completion:nil];
}
它工作正常。该错误必须在解除 UIAlertViewController 后发生(这是在触摸 OK 后自动完成的)。这部分代码丢失了。在代码的所有相关部分添加断点并检查它会继续跟踪此错误的位置。
根据您的堆栈跟踪,您的视图控制器是这里的问题:您试图在未实现该方法的对象上调用 image
方法,这就是导致此崩溃的原因。
看看您对 -[ViewController collectionView:didUpdateFocusInContext:withAnimationCoordinator:]
的实现,看看您在什么地方调用 image
:看起来您假设该对象属于一种类型,而实际上它是另一种类型。
我写了三个不同的例子
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func alertDone(sender: AnyObject) {
let title = "Download Complete"
let message = "Your game download completed"
let buttonTitle = "OK"
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let doneAction = UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.Default) { _ in
print("Done was pressed")
}
alert.addAction(doneAction)
presentViewController(alert, animated: true, completion: nil)
}
@IBAction func alertError(sender: AnyObject) {
let title = "Error Complete"
let message = "Please try again later."
let okButton = "OK"
let retryButton = "Retry Download"
let alertError = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let okAction = UIAlertAction(title: okButton, style: .Default, handler: nil)
let retryAction = UIAlertAction(title: retryButton, style: .Cancel) { _ in
print("Retry was pressed")
}
alertError.addAction(okAction)
alertError.addAction(retryAction)
presentViewController(alertError, animated: true, completion: nil)
}
@IBAction func alertConfirm(sender: AnyObject) {
let title = "Are you sure"
let message = "Please try again later."
let cancelButton = "Cancel"
let deleteButton = "Delete"
let alertError = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: cancelButton, style: .Default, handler: nil)
let deleteAction = UIAlertAction(title: deleteButton, style: .Cancel) { _ in
print("Retry was pressed")
}
alertError.addAction(cancelAction)
alertError.addAction(deleteAction)
presentViewController(alertError, animated: true, completion: nil)
}
}
我有一个单视图 tvOS 应用程序。它有一个包含 24 个 collectionviewcell 的集合视图。选择 collectionviewcell 时,我会触发警报。我正在使用 Apple 参考文档(粘贴在下面)中的默认代码。
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction]; // This is what I comment out
[self presentViewController:alert animated:YES completion:nil];
当我没有将 defaultAction 添加到警报时(注释掉 [alert addAction:defaultAction];),警报会正确显示但无法关闭。当我将 defaultAction 添加到警报时,警报会显示 'OK' 按钮,但一旦它出现在屏幕上就会收到可怕的 SIGABRT 错误
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[_UIAlertControllerActionView image]: unrecognized selector
sent to instance 0x7fe571ddee10'
此处的堆栈跟踪:
2015-11-10 19:42:02.927 AppABC[1701:97632] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIAlertControllerActionView image]: unrecognized selector sent to instance 0x7fefd3ddaf10'
*** First throw call stack:
(
0 CoreFoundation 0x000000010de0a0b5 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010d884deb objc_exception_throw + 48
2 CoreFoundation 0x000000010de126dd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010dd6005a ___forwarding___ + 970
4 CoreFoundation 0x000000010dd5fc08 _CF_forwarding_prep_0 + 120
5 IsItGood 0x000000010d381170 __82-[ViewController collectionView:didUpdateFocusInContext:withAnimationCoordinator:]_block_invoke78 + 48
6 UIKit 0x000000010e260df4 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 582
7 UIKit 0x000000010e2612ec +[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:] + 63
8 IsItGood 0x000000010d380ca8 -[ViewController collectionView:didUpdateFocusInContext:withAnimationCoordinator:] + 536
9 UIKit 0x000000010ea24e09 -[UICollectionView _didUpdateFocusInContext:withAnimationCoordinator:] + 1181
10 UIKit 0x000000010eb15a5e _UIFocusEnvironmentDidUpdateFocus + 628
11 UIKit 0x000000010e4fe102 __36-[UIScreen _updateFocusWithContext:]_block_invoke + 88
12 UIKit 0x000000010e4fe161 __36-[UIScreen _updateFocusWithContext:]_block_invoke + 183
13 UIKit 0x000000010e4fdb9f -[UIScreen _updateFocusWithContext:] + 1912
14 UIKit 0x000000010e4feca9 -[UIScreen updateFocusIfNeeded] + 597
15 UIKit 0x000000010e1c1e4a _runAfterCACommitDeferredBlocks + 317
16 UIKit 0x000000010e1d53d9 _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
17 UIKit 0x000000010e1e1312 _afterCACommitHandler + 90
18 CoreFoundation 0x000000010dd35ab7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
19 CoreFoundation 0x000000010dd35a27 __CFRunLoopDoObservers + 391
20 CoreFoundation 0x000000010dd2b67b __CFRunLoopRun + 1147
21 CoreFoundation 0x000000010dd2af78 CFRunLoopRunSpecific + 488
22 GraphicsServices 0x0000000111582ad2 GSEventRunModal + 161
23 UIKit 0x000000010e1b608d UIApplicationMain + 171
24 AppABC 0x000000010d3826af main + 111
25 libdyld.dylib 0x000000011050d9e9 start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
有什么想法吗??
我尝试了您的代码并创建了一个新的单视图应用程序,具有一个按钮和一个按钮操作。按钮操作包含您的代码片段:
- (IBAction)action:(id)sender {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction]; // This is what I comment out
[self presentViewController:alert animated:YES completion:nil];
}
它工作正常。该错误必须在解除 UIAlertViewController 后发生(这是在触摸 OK 后自动完成的)。这部分代码丢失了。在代码的所有相关部分添加断点并检查它会继续跟踪此错误的位置。
根据您的堆栈跟踪,您的视图控制器是这里的问题:您试图在未实现该方法的对象上调用 image
方法,这就是导致此崩溃的原因。
看看您对 -[ViewController collectionView:didUpdateFocusInContext:withAnimationCoordinator:]
的实现,看看您在什么地方调用 image
:看起来您假设该对象属于一种类型,而实际上它是另一种类型。
我写了三个不同的例子
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func alertDone(sender: AnyObject) {
let title = "Download Complete"
let message = "Your game download completed"
let buttonTitle = "OK"
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let doneAction = UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.Default) { _ in
print("Done was pressed")
}
alert.addAction(doneAction)
presentViewController(alert, animated: true, completion: nil)
}
@IBAction func alertError(sender: AnyObject) {
let title = "Error Complete"
let message = "Please try again later."
let okButton = "OK"
let retryButton = "Retry Download"
let alertError = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let okAction = UIAlertAction(title: okButton, style: .Default, handler: nil)
let retryAction = UIAlertAction(title: retryButton, style: .Cancel) { _ in
print("Retry was pressed")
}
alertError.addAction(okAction)
alertError.addAction(retryAction)
presentViewController(alertError, animated: true, completion: nil)
}
@IBAction func alertConfirm(sender: AnyObject) {
let title = "Are you sure"
let message = "Please try again later."
let cancelButton = "Cancel"
let deleteButton = "Delete"
let alertError = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: cancelButton, style: .Default, handler: nil)
let deleteAction = UIAlertAction(title: deleteButton, style: .Cancel) { _ in
print("Retry was pressed")
}
alertError.addAction(cancelAction)
alertError.addAction(deleteAction)
presentViewController(alertError, animated: true, completion: nil)
}
}