gpt4 book ai didi

ios - 在保存到 TableView Controller 的 Swift 中将 TextField 添加到 UIAlertView

转载 作者:搜寻专家 更新时间:2023-11-01 05:37:20 26 4
gpt4 key购买 nike

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

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

2016-02-16 12:30:21.675 CartoBike[687:128666] Presenting view controllers on detached view controllers is discouraged .

@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()
}

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

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

研究:我已经查看了标题为“How to add a TextField to UIAlertView in Swift”和“Writing handler for UIAlertAction”的问题,但仍然不能似乎看出我做错了什么。理想情况下,此屏幕截图中的警报 app来自 raywenderlich 网站的是我希望它看起来像的样子。我不确定我正在尝试做的事情是否可行,因为涉及到如此多的 View Controller 。当然,我是 swift 的新手,我确定我遗漏了一些明显的东西!

当前得到一个错误

"Type of expression is ambiguous without more context", see screenshot: here

这是 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()

}

感谢 stack overflow 提供的任何帮助!

最佳答案

您使用的是 .ActionSheet,而您显示的教程使用的是 .Alert

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

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

NSHipster

关于ios - 在保存到 TableView Controller 的 Swift 中将 TextField 添加到 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35420679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com