- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个 iPad 应用程序。我正在创建一个 UIAlertController 并添加一个文本字段。它崩溃了。它仅在我添加文本字段时崩溃。
let alert = UIAlertController(title: "Enter Name", message:nil, preferredStyle: UIAlertControllerStyle.Alert);
alert.addTextFieldWithConfigurationHandler { (textfield:UITextField!) -> Void in
textfield.placeholder = "Sexy time";
}
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction!) -> Void in
//Some action here
}));
self.presentViewController(alert, animated: true, completion: nil);
我遇到了一个有趣的崩溃,告诉我约束被搞砸了。此代码在 < 8.3 中运行良好,没有警告。即使在一个除了这段代码什么都没有的干净项目上,它也会崩溃——该项目需要是 iPad 上的 splitview 项目。
这是完整的堆栈跟踪和奇怪的约束警告,它仅在尝试将文本字段添加到 alertController 后出现。
2015-04-10 15:25:07.155 Observation[18235:281813] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fb66cf9dfc0 UITableView:0x7fb66b855000.left == UIView:0x7fb66fae68e0.left>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-04-10 15:25:07.155 Observation[18235:281813] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fb66cf9e010 UITableView:0x7fb66b855000.right == UIView:0x7fb66fae68e0.right>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-04-10 15:25:07.155 Observation[18235:281813] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fb66fb37f90 UITableView:0x7fb66b855000.top == UIView:0x7fb66fae68e0.top>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-04-10 15:25:07.156 Observation[18235:281813] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fb66fb80580 UITableView:0x7fb66b855000.bottom == UIView:0x7fb66fae68e0.bottom>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-04-10 15:25:13.589 Observation[18235:281813] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x7fb66cf9dfc0 UITableView:0x7fb66b855000.left == UIView:0x7fb66fae68e0.left>
Container hierarchy:
<UIView: 0x7fb66fa86e00; frame = (0 0; 0 0); layer = <CALayer: 0x7fb66fadf8e0>>
| <UIView: 0x7fb66af3e080; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x7fb66fae32c0>>
| | <_UIAlertControllerShadowedScrollView: 0x7fb66fa68c80; frame = (0 0; 0 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fb66fa38a80>; layer = <CALayer: 0x7fb66fa97560>; contentOffset: {0, 0}; contentSize: {0, 0}>
| | | <UIView: 0x7fb66fa87350; frame = (0 0; 0 0); layer = <CALayer: 0x7fb66fadf810>>
| | | | <UILabel: 0x7fb66fa88740; frame = (0 0; 0 0); text = 'Enter Name'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fb66fa94ed0>>
| | | | <UILabel: 0x7fb66fa73710; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fb66cc0ee10>>
| | | | <UIView: 0x7fb66fae68e0; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x7fb66fa90160>>
| | <UILabel: 0x7fb66fa3ad40; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fb66fa73680>>
| | <UICollectionView: 0x7fb66c130200; frame = (0 0; 0 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fb66faebab0>; layer = <CALayer: 0x7fb66fa3acf0>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <_UIAlertControllerCollectionViewFlowLayout: 0x7fb66fae0b30>
View not found in container hierarchy: <UITableView: 0x7fb66b855000; frame = (0 20; 768 1004); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x7fb66cf79f30>; layer = <CALayer: 0x7fb66cf600a0>; contentOffset: {0, 0}; contentSize: {768, 25}>
That view's superview: NO SUPERVIEW
2015-04-10 15:25:13.594 Observation[18235:281813] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x7fb66cf9dfc0 UITableView:0x7fb66b855000.left == UIView:0x7fb66fae68e0.left> view:<UIView: 0x7fb66fa86e00; frame = (0 0; 0 0); layer = <CALayer: 0x7fb66fadf8e0>>'
*** First throw call stack:
(
0 CoreFoundation 0x0000000102940c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010221dbb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000102940b9d +[NSException raise:format:] + 205
3 Foundation 0x0000000101daf479 -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 187
4 UIKit 0x00000001039bca34 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 474
5 Foundation 0x0000000101dbd1be -[NSISEngine withBehaviors:performModifications:] + 155
6 UIKit 0x00000001039bc83a __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
7 UIKit 0x00000001039bc64d -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
8 UIKit 0x00000001039bc933 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 217
9 Foundation 0x0000000101dbd1be -[NSISEngine withBehaviors:performModifications:] + 155
10 UIKit 0x00000001039bc83a __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
11 UIKit 0x00000001039bc64d -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
12 UIKit 0x00000001039bc933 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 217
13 Foundation 0x0000000101dbd1be -[NSISEngine withBehaviors:performModifications:] + 155
14 UIKit 0x00000001039bc83a __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
15 UIKit 0x00000001039bc64d -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
16 UIKit 0x00000001033b5717 __40-[UIView(Hierarchy) layoutBelowIfNeeded]_block_invoke + 39
17 Foundation 0x0000000101dbd1be -[NSISEngine withBehaviors:performModifications:] + 155
18 UIKit 0x00000001033b5556 -[UIView(Hierarchy) layoutBelowIfNeeded] + 320
19 UIKit 0x000000010374a394 -[_UIAlertControllerAnimatedTransitioning animateTransition:] + 470
20 UIKit 0x000000010344fa4e __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 1867
21 UIKit 0x000000010336562c _applyBlockToCFArrayCopiedToStack + 314
22 UIKit 0x00000001033654a6 _afterCACommitHandler + 533
23 CoreFoundation 0x0000000102873ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
24 CoreFoundation 0x0000000102873c00 __CFRunLoopDoObservers + 368
25 CoreFoundation 0x0000000102869a33 __CFRunLoopRun + 1123
26 CoreFoundation 0x0000000102869366 CFRunLoopRunSpecific + 470
27 GraphicsServices 0x0000000106dd6a3e GSEventRunModal + 161
28 UIKit 0x0000000103341900 UIApplicationMain + 1282
29 Observation 0x0000000101612927 main + 135
30 libdyld.dylib 0x0000000104f60145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
最佳答案
编辑:此修复会根据您是使用 Xcode 6 还是 Xcode 7 进行构建而发生变化,因此我添加了两个版本的 Xcode 的相关信息。
我今天遇到了这个问题,它的意思是它无法将文本字段添加到 View Controller 的 View 中,或者它无法将自动布局约束添加到其父 View 中。这似乎是因为它还没有创建要将其添加到的 super View ,所以它会出现 panic 和崩溃。
我发现的简单修复是在告诉警报 Controller 显示后添加文本字段。这对我来说是固定的,但我不确定它是否会影响任何东西,比如在出现警报时弹出的文本字段。
使用 Xcode 6 构建
let alertController = UIAlertController(title: "Enter Name", message:nil, preferredStyle: .Alert)
let okAction = UIAlertAction(title: "Ok", style: .Default) { (_) -> Void in
// Some action here
}
alertController.addAction(okAction)
presentViewController(alertController, animated: true, completion: nil)
// Add any text fields after presenting the alert controller to fix crash in iOS 8.3
alertController.addTextFieldWithConfigurationHandler { (textfield) -> Void in
textfield.placeholder = "Name"
}
附言作为代码示例的旁注,请记住使用 Swift,您不需要在每一行的末尾使用 ;
,尽管这样做并不重要。 😉
使用 Xcode 7 构建
当您使用 Xcode 7 构建应用程序时,Apple 似乎已经解决了这个问题。使用上面显示的方法将不再在 iOS 9 中显示文本字段(即使它在 iOS 8 中仍能正确显示)。
下面是一段代码,它本来应该是这样的,当在 Xcode 7 中构建时,它可以在 iOS 8 和 iOS 9 中正确运行。
let alertController = UIAlertController(title: "Enter Name", message:nil, preferredStyle: .Alert)
alertController.addTextFieldWithConfigurationHandler { (textfield) -> Void in
textfield.placeholder = "Name"
}
let okAction = UIAlertAction(title: "Ok", style: .Default) { (_) -> Void in
// Some action here
}
alertController.addAction(okAction)
presentViewController(alertController, animated: true, completion: nil)
使用 Xcode 7 GM (7A218) 在 Swift 2.0 和 Obj-C 中测试
关于尝试添加文本字段时 iOS 8.3 UIAlertController 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29571502/
我们如何从第一个警报打开第二个 UIAlert? 第一段代码工作正常,显示了警报。但是我希望能够调用第二个警报 View ,如果选择了第一个警报中的一个选项,就会出现该 View 。 在下面的示例中,
我有一个 Parse Sign Up 和一个 UIAlertController,我希望 UIAlertController 显示一个指示器,但在注册出现错误时被另一个 UIAlertControll
如何在 UIAlertController 外部点击时关闭 UIAlertController? 我可以添加 UIAlertActionStyleCancel 样式的 UIAlertAction 来关
这是我的代码:请检查是否出了什么问题?请给我 UIActionsheet (UIAlertController) 的代码,其中包含 swift 中的数组 let alertA = UIAlertCon
我正在使用 UIAlertController 要求用户输入一个字符串,这个字符串不能为空。为此,我为位于 UIAlertController 上的 UITextField 上的每个编辑事件添加了处理
我想介绍一个 UIAlertController与 UIAlertControllerStyleAlert风格,同时解雇 UIAlertController与 UIAlertControllerSty
我正在使用 UIAlertController 获取用户输入并更新表格单元格。每次当我尝试创建警报时,我都会在控制台中收到以下警告 2015-11-19 17:51:42.034 SimpleTabl
我正在尝试将 UIAlertController 中的推送通知(PN)中的消息从 AppDelegate 呈现到当前 ViewController。如果我发送 1 个 PN,没有问题,并且会显示警报!
我正在尝试从 UIAlertController 文本字段中获取文本。有人可以告诉我我做错了什么,因为它不起作用。我得到一个零返回。 - (IBAction)btnMakeRec {
我正在将我的应用程序移植到 iOS 8.0 并注意到 UIAlertView 已被弃用。 所以我改变了使用 UIAlertController 的方法。这在大多数情况下都有效。 除了,当我的应用程序打
我想根据代码更改警报操作按钮的顺序,我尝试了所有可能性,但不允许我更改顺序。 根据 HIG,取消在右侧 https://developer.apple.com/library/ios/document
在 iOS 8 及更低版本中显示 UIActionSheet当键盘出现时,将在键盘上显示操作表。在 iOS 9 中,情况不再如此。 在我的应用程序中,我们有一个聊天功能,并希望通过键盘显示一个 Act
嗨,我想知道是否可以使用UIAlert播放声音?根据以下文章,这似乎是可能的,但我正在努力将Obj-C转换为Swift。任何帮助表示赞赏! IOS alert message with sound 最
我需要一个系统来隐藏所有UIAlertController当她进入后台时在我的应用程序中。目前我使用 BaseViewController我所有的类(class)UIViewControllers注册
我正在调用带有2个文本字段的UIAlertController(在Obj-C中)。我已经定义了需要在警报中使用的全局UITextField,因为我需要利用UITextFieldDelegate。 我的
我在 View 中添加了一个 UIAlterController。 IT 触发很好,看起来很好,并且有它的 Action ,但是点击取消 Action 什么都不做,它不运行任何代码,即使我添加一个 b
这个问题在这里已经有了答案: How to add textField in UIAlertController? (4 个回答) 4年前关闭。 我可以使用在 UIAlertController 中添
我正在开发一个带有深色主题的 iOS 应用程序,我想使用 UIAlertController .但是,白色警报看起来不合适。即使在黑暗主题的应用程序上使用黑暗主题是否违反人机界面指南 UIAlertC
我正在做一个使用 UIAlertView 的项目,现在的问题是我必须替换所有 UIAlertView's与 UIAlertController's代码中有大约 1250 个。我打算使用现有的 Util
我正在尝试使用操作表样式创建 UIAlertController,但我想将背景颜色更改为灰色。我已经能够找到一种方法来更改 UIAlertController 的背景颜色,但不能更改取消按钮。单独的取
我是一名优秀的程序员,十分优秀!