【问题标题】:Struggling to add multiple text fields to alertview努力将多个文本字段添加到警报视图
【发布时间】:2016-12-17 20:07:21
【问题描述】:

我正在尝试创建一个警报视图,在点击广告按钮后将一个或多个项目添加到我的表格视图中。在按下添加按钮时,alertview 应该出现三个文本字段,用于项目名称价格和数量,当按下确定按钮时,它将添加项目,无论数量是多少倍。当我点击按钮时,虽然我收到以下错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'

函数如下:

func createAlertView() -> UIAlertController {

    let view = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(250), height: CGFloat(100)))
    let itemName = UITextField(frame: CGRect(x: CGFloat(10), y: CGFloat(0), width: CGFloat(252), height: CGFloat(25)))
    itemName.borderStyle = .RoundedRect
    itemName.placeholder = "Item Name"
    itemName.keyboardAppearance = .Alert
    itemName.delegate = self
    view.addSubview(itemName)

    let itemPrice = UITextField(frame: CGRect(x: CGFloat(10), y: CGFloat(30), width: CGFloat(252), height: CGFloat(25)))
    itemPrice.placeholder = "Item Price"
    itemPrice.borderStyle = .RoundedRect
    itemPrice.keyboardAppearance = .Alert
    itemPrice.delegate = self
    view.addSubview(itemPrice)

    let itemQuantity = UITextField(frame: CGRect(x: CGFloat(10), y: CGFloat(60), width: CGFloat(252), height: CGFloat(25)))
    itemQuantity.placeholder = "Item Quantity"
    itemQuantity.borderStyle = .RoundedRect
    itemQuantity.keyboardAppearance = .Alert
    itemQuantity.delegate = self
    view.addSubview(itemQuantity)

    let alertController = UIAlertController(title: "Add Item", message: nil, preferredStyle: .Alert)
    alertController.view.addSubview(view)

    return alertController
}

func createAlertViewItem(alertController: UIAlertController, itemName: String, itemStringPrice: String) {

    let managedContext = self.bill.managedObjectContext
    let entity = NSEntityDescription.entityForName("Item", inManagedObjectContext: managedContext!)
    let newItem = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)

    if let itemName = alertController.textFields?.first?.text {

        newItem.setValue(itemName, forKey: "name")

    }
    if let itemPriceString = alertController.textFields?[1].text {

        let itemPriceNumber = Int(itemPriceString)

        newItem.setValue(itemPriceNumber, forKey: "price")
    }

    do {
        try managedContext!.save()
    }
    catch let error as NSError {
        print("Core Data save failed: \(error)")
    }

    let currentItems = self.bill.mutableSetValueForKey("items")
    currentItems.addObject(newItem)

    self.tableView.reloadData()
}

@IBAction func addNewItem(sender: UIButton) {

    let alertController = createAlertView()

    let itemName = alertController.textFields![0].text
    let itemPrice = alertController.textFields?[1].text
    let itemQuantity: Int!

    if alertController.textFields![2].text == "" {
        itemQuantity = 0
    } else {
        itemQuantity = Int((alertController.textFields?[2].text)!)!
    }

    let okAction = UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
        if itemQuantity == 0 || itemQuantity == 1 {
            print(2)
            self.createAlertViewItem(alertController, itemName: itemName!, itemStringPrice: itemPrice!)
        } else {
            for _ in 0...itemQuantity {
                print(3)
                self.createAlertViewItem(alertController, itemName: itemName!, itemStringPrice: itemPrice!)
            }
        }
    })

    alertController.addAction(okAction)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
    alertController.addAction(cancelAction)

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

我认为在将视图添加到警报视图时我遗漏了一些东西,但我看不到什么,因为我认为错误表明它找不到文本字段。不过我不能确定。不过,任何帮助都会很棒。谢谢!

【问题讨论】:

  • 另外 - 您也可以在情节提要中创建自定义视图。将其添加到您希望它显示的视图控制器中。使其不可见或将其放置在屏幕外。然后在你想显示它的时候用一些动画呈现它。这个过程可以让您完全控制外观。
  • 感谢您的帮助。如果我找不到 alertviewcontroller 的解决方案,我会尝试。

标签: ios swift uialertview


【解决方案1】:

您不应该尝试操纵警报控制器的视图层次结构。相反,使用addTextField(configurationHandler:) 方法将文本字段添加到警报控制器。我建议看一下 Xcode 中的 UIAlertController Class 参考。这是有据可查的。

编辑:

另外,我建议不要将UIAlertController 称为“alertview”。在以前的 iOS 版本中,我们使用了一个名为 UIAlertView 的类,但现在已弃用。 “alertview”一词会让人们认为您使用的是旧的已弃用的类。

【讨论】:

  • 但是当我向 UIAlertController 添加第三个文本字段时,我无法访问它,这就是为什么我不得不通过创建自定义视图来添加它们的原因。还是我错过了什么?
【解决方案2】:

你见过其他SO Questions吗?如果是这样,请尝试推断。错误消息似乎表明您的方法调用之间存在一个 nil 对象,因此请确保您在这些方法之间使用共享引用。在添加 UITextfield 之前和之后检查对象值以验证控制器的状态。添加您的 alertController 作为对 UIViewController 类的引用,并在您进行所需更改时检查其状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 2014-06-10
    • 2023-01-07
    • 2020-12-08
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多