UIAlertController:每次尝试引用UIAlertController时出现“UICollectionViewFlowLayout未定义”警告

9
我正在使用UIAlertController获取用户输入并更新表格单元。每次尝试创建警报时,都会在控制台中收到以下警告:
2015-11-19 17:51:42.034 SimpleTableView[5488:584215] UICollectionViewFlowLayout的行为未定义,因为: 2015-11-19 17:51:42.035 SimpleTableView[5488:584215] item高度必须小于UICollectionView减去节拍上下值和内容插入上下值的高度。 2015-11-19 17:51:42.036 SimpleTableView[5488:584215] 相关的UICollectionViewFlowLayout实例是<_UIAlertControllerCollectionViewFlowLayout: 0x7fd0a057c3d0>,它附加到; layer = ; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <_UIAlertControllerCollectionViewFlowLayout: 0x7fd0a057c3d0>。 2015-11-19 17:51:42.036 SimpleTableView[5488:584215] 在调试器中设置一个符号断点来捕获UICollectionViewFlowLayoutBreakForInvalidSizes。
实现方法非常简单,正如许多博客中所提到的。
func displayAlertInfo(){
    let alertController = UIAlertController(title: "New Race", message: "Type in a new race", preferredStyle: .Alert)
    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in
        alertController.dismissViewControllerAnimated(true, completion: nil)
    }
    let addAction = UIAlertAction(title: "Add", style: .Default) { (_) in
        let textFieldInput = alertController.textFields![0] as UITextField
        let newRace = textFieldInput.text?.capitalizedString
        DataManager.sharedInstance.addRace(species: self.species, race: newRace!)
        let newIndexPath = NSIndexPath(forRow: self.races.count - 1, inSection: 0)
        self.racesTableVew.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }

    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
        textField.placeholder = "New Race"
    }

    alertController.addAction(cancelAction)
    alertController.addAction(addAction)

    self.presentViewController(alertController, animated: true, completion: nil)
}

我在导航按钮点击时调用此函数。警告正确显示,但每次尝试点击创建警报的按钮时都会收到此警告。 我做错了什么或者我跳过了什么吗? 编辑:添加截图:从导航项按钮调用警报,在tableView上。

enter image description here

3个回答

13

谢谢@Mak,大家一定要调用alertcontroller而不是UIViewController对象。 - Mohammad Zaid Pathan

2

我有一个类似的问题:在 iPhone 6 Plus 竖屏下运行正常,在横屏下出现了故障。我做了一个小改动:从

 message: nil,  

to:

 message: "this is a nice long line of crapthis is a nice long line of crapthis is a nice long line of crap",

我发现,如果一条消息长度足够长,能够至少换行两次,那么就能解决出现错误的问题。但如果消息长度较短,不能至少换行两次,仍然会导致错误的发生。

接着我找到了...

 message: "\n\n"  

在IT技术方面,单个换行符的消息发送没有问题,但是多个换行符的消息发送却失败了。如果将标题设置为nil,则需要在消息中添加更多的换行符。这似乎与警报的总高度、标题和消息的结合有关。

注意:其他没有文本字段的警报没有显示相同的问题。而且在我的情况下,setNeedsLayout()没有任何区别。

(当然,添加那么多行会使得警报在小尺寸iPhone横屏时过于高大,一旦弹出键盘就会出现同样的错误信息洪流。因此,最终我把这些行的添加条件限制在屏幕的高度和宽度大于一个固定值的情况下。)


对我没用。我的是Xcode 7.3.1。设备是iPhone 6 Plus。 - triiiiista
XCode 7.3.1 可能出了问题。我还在使用 7.3,因为我正在进行一个项目;还没有尝试过 7.3.1。 - Adam Wilt
我在使用XCode 7.3.1和iOS 9.3.2时仍然可以正常工作。现在我只有在将小屏幕(iPhone 4S&5)从纵向旋转到横向并显示警报和键盘时才会看到消息:警报(带标题但没有消息)在初始横向或纵向显示时适合,但在旋转回横向时会出现问题,标题和输入字段会向上移动到警报的顶部(如果重新旋转回纵向,则保持不变)。如果我再次旋转到横向,标题和输入字段就会再次移动到它们的正确位置。 - Adam Wilt
奇怪。我的应用程序始终处于竖屏模式,并且在我的iPhone 6 Plus上不断收到此消息。但是,我的iPhone 4S并没有显示这样的消息。 - triiiiista

1

我在我的iPhone 6 Plus屏幕上也遇到了同样的问题。请检查一下,如果您没有传递空标题,请将标题设置为空值,如下所示:

 UIAlertController *confirmAlert = [UIAlertController alertControllerWithTitle:nil message:displayMessage preferredStyle:UIAlertControllerStyleAlert];

代替,取代
  UIAlertController *confirmAlert = [UIAlertController alertControllerWithTitle:@"" message:displayMessage preferredStyle:UIAlertControllerStyleAlert];

它解决了我的问题。祝好运。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接