在 Swift 中将 TextField 添加到 UIAlertView,以保存到 TableView 控制器



编辑:感谢您的回复,是的,我需要警报而不是操作表! 我已经实现了这个新代码并且它可以工作,但是在用户可以输入乘车标题之前,它为什么会转到下一个视图?

它还会在调试控制台中抛出此消息,我应该担心吗?

2016-02-16 12:30:21.675 CartoBike[687:128666] 不鼓励在分离的视图控制器上显示视图控制器。

@IBAction func stopAction(sender: UIButton) {
    let alert = UIAlertController(title: "Ride Stopped", message: "Give a title to your ride", preferredStyle: .Alert)
    let saveAction = UIAlertAction(title: "Save", style: .Default,
        handler: { (action:UIAlertAction) -> Void in
            // Allow for text to be added and appended into the RideTableViewController
            let textField = alert.textFields!.first
            rideContent.append(textField!.text!)
    })
    let cancelAction = UIAlertAction(title: "Cancel",
        style: .Default) { (action: UIAlertAction) -> Void in
    }
    alert.addTextFieldWithConfigurationHandler {
        (textField: UITextField) -> Void in
    }
    alert.addAction(saveAction)
    alert.addAction(cancelAction)
    // Save the ride
    saveRide()
    // Automatically segue to the Ride details page
    self.performSegueWithIdentifier("ShowRideDetail", sender: nil)
    presentViewController(alert, animated: true, completion: nil)
    timer.invalidate()
    self.stopLocation()
}

概述:我正在通过我的第一个真正的应用程序工作。 基本逻辑是一个主屏幕,用于开始新的骑行或查看以前的骑行。 开始骑行将打开一个带有地图的新视图,以根据用户位置记录自行车骑行,这可以停止并保存骑行并立即切换到新视图以查看骑行地图。或者,从主屏幕,用户可以选择以前的游乐设施并在表格视图中查看其旧游乐设施的列表,然后选择一个并过渡到带有其游乐设施地图的详细视图。

问题:添加UIAlertAction时,我希望有一个保存取消功能。此外,我希望用户能够通过文本字段键入自定义标题来添加自定义标题。 来自文本字段的输入将追加到名为 rideContent 的全局变量中,该变量与在表视图中创建新单元格相关联,以按唯一标题存储多个自行车骑行。

研究:我已经复习了标题为"如何在 Swift 中为 UIAlertView 添加TextField"和"UIAlertAction编写处理程序"的问题,但似乎仍然无法辨别我做错了什么。 理想情况下,来自raywenderlich网站的应用程序的屏幕截图中的警报是我希望它的样子。我不确定我正在尝试做的事情是否可能,因为涉及如此多的视图控制器。当然,我是 swift 的新手,我确定我错过了一些明显的东西!

当前收到错误

"表达式类型不明确,没有更多上下文",请参见屏幕截图:此处

下面是 UIAlertController 代码:

// The timer pauses & the location stops updating when the the stop button is pressed
@IBAction func stopAction(sender: UIButton) {
    var inputTextField: UITextField?
    let actionSheetController = UIAlertController (title: "Ride Stopped", message: "Add a title to your ride", preferredStyle: UIAlertControllerStyle.ActionSheet)
    // Add a cancel action
    actionSheetController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    // Add a save action
    actionSheetController.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: {
        (actionSheetController) -> Void in

    //Add a text field  --- Getting an error on this line
    actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
        // you can use this text field
        inputTextField = textField
        // Append the ride title to the table view
        rideContent.append(textField!.text!)
        // Update when user saves a new ride to store the ride in the table view for permanent storage
        NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")

        // Save the ride
        self.saveRide()
        // Automatically segue to the Ride details page
        self.performSegueWithIdentifier("ShowRideDetail", sender: nil)
        }}))
        //present actionSheetController
        presentViewController(actionSheetController, animated: true, completion: nil)
        timer.invalidate()
        self.stopLocation()
    }

感谢您的堆栈溢出为您提供的任何帮助!

您正在使用 .操作表,而您显示的教程使用的是 .警报

操作表可以有按钮,但不能有文本字段。

"警报可以同时具有按钮和文本字段,而操作表仅支持按钮。">

NSHipster

按下添加按钮并使用您的表格视图数组,并"将警报视图文本firld添加到表格视图数组,然后重新加载表格视图"。

@IBAction func btnAdd(_ sender: Any) {
    let alertController = UIAlertController(title: "Add Category", message: "", preferredStyle: .alert)
    let saveAction = UIAlertAction(title: "Add", style: .default, handler: { alert -> Void in
        let firstTextField = alertController.textFields![0] as UITextField
        self.categories.add(firstTextField.text!)
        self.tblCatetgory.reloadData()
    })
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {
        (action : UIAlertAction!) -> Void in })
    alertController.addTextField { (textField : UITextField!) -> Void in
        textField.placeholder = "Enter Category!!!"
    }
    alertController.addAction(saveAction)
    alertController.addAction(cancelAction)
    self.present(alertController, animated: true, completion: nil)
}

}

最新更新