ios - 努力将多个文本字段添加到 alertview

标签 ios swift uialertview

我正在尝试创建一个提醒 View ,在点击广告按钮后将一个或多个项目添加到我的表格 View 中。在按下添加按钮时,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)
}

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

最佳答案

您不应该尝试操纵警报 Controller 的 View 层次结构。相反,使用方法 addTextField(configurationHandler:) 将文本字段添加到您的警报 Controller 。我建议查看 Xcode 中的 UIAlertController 类引用。它有据可查。

编辑:

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

关于ios - 努力将多个文本字段添加到 alertview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41202585/

相关文章:

swift - 带有自定义 TextView 的自定义 View

ios - 如何使用 SwiftUI 按钮打开本地 PDF 文件?

ios - 如何获取 UIAlertView 消息的字体大小

iOS - iOS 8 中的 UIAlertView/UIAlertController 方向问题

ios - 旋转 iPad 时自定义警报 View 会导致崩溃

iphone - 我们可以在 iPhone 设备上自动安装任何 .deb/.ipa 文件吗?

ios - 突出显示动态创建的 UIButtons?

ios - React Native Undefined 不是对象(评估 'this.camera.capture' )

ios - 子类中的 Swift #selector

swift - 合并:从带有选择器的通知中心 addObserver 到通知发布者