无法从函数取回值以将其分配给标签
Unable to get the value back from a function to assign it to a label
我是构建应用程序的新手,但正在学习。我正在尝试从警报中获取价值并将其保存到标签中。这是为一周创建一个简单的时间表。我有两个困难,一个是从函数中获取值以将其显示到我的标签上,该标签位于 Main.Sotryboard.
上按下的按钮后面
我正在尝试取回我的值以分配给标签 MonB
@IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
//试图让我的文本输入回到这里。
我正在尝试从以下函数中获取值:
func FoodItemAdded(Food: String)
{
//return self.
//Trying to return the value from here.
}
我也环顾四周,但不知道如何保存这些标签值,所以当我关闭应用程序时,它仍然保存为持久性。
这是完整的代码。
import UIKit
class MenuViewController: UIViewController {
//Define all the Output Labels
@IBOutlet weak var MonB: UILabel!
@IBOutlet weak var MonL: UILabel!
@IBOutlet weak var MonD: UILabel!
@IBOutlet weak var TueB: UILabel!
@IBOutlet weak var TueL: UILabel!
@IBOutlet weak var TueD: UILabel!
@IBOutlet weak var WedB: UILabel!
@IBOutlet weak var WedL: UILabel!
@IBOutlet weak var WedD: UILabel!
@IBOutlet weak var ThuB: UILabel!
@IBOutlet weak var ThuL: UILabel!
@IBOutlet weak var ThuD: UILabel!
@IBOutlet weak var FriB: UILabel!
@IBOutlet weak var FriL: UILabel!
@IBOutlet weak var FriD: UILabel!
@IBOutlet weak var SatB: UILabel!
@IBOutlet weak var SatL: UILabel!
@IBOutlet weak var SatD: UILabel!
@IBOutlet weak var SunB: UILabel!
@IBOutlet weak var SunL: UILabel!
@IBOutlet weak var SunD: UILabel!
//Define Buttons to Enable/Disable
@IBOutlet weak var MondB: UIButton!
@IBOutlet weak var MondL: UIButton!
@IBOutlet weak var MondD: UIButton!
@IBOutlet weak var TuesB: UIButton!
@IBOutlet weak var TuesL: UIButton!
@IBOutlet weak var TuesD: UIButton!
@IBOutlet weak var WedsB: UIButton!
@IBOutlet weak var WedsL: UIButton!
@IBOutlet weak var WedsD: UIButton!
@IBOutlet weak var ThurB: UIButton!
@IBOutlet weak var ThurL: UIButton!
@IBOutlet weak var ThurD: UIButton!
@IBOutlet weak var FridB: UIButton!
@IBOutlet weak var FridL: UIButton!
@IBOutlet weak var FridD: UIButton!
@IBOutlet weak var SatuB: UIButton!
@IBOutlet weak var SatuL: UIButton!
@IBOutlet weak var SatuD: UIButton!
@IBOutlet weak var SundB: UIButton!
@IBOutlet weak var SundL: UIButton!
@IBOutlet weak var SundD: UIButton!
//Define the Edit button to enable Tap Buttons
@IBOutlet var editButton: UIBarButtonItem!
//Define all the Output Buttons
@IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func MoL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func MoD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuD(_ sender: UIButton) {
ButtonTapped(sender)
}
//VIEWDIDLOAD Parameters
override func viewDidLoad() {
super.viewDidLoad()
//Hide the Navigation Bar on the screen
self.navigationController?.isNavigationBarHidden = false
//Define the Title for the page
self.title = "~~~Weekly Menu~~~"
//Enable the buttons to edit
self.navigationItem.rightBarButtonItem = self.editButton
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Disable UIButton by default
self.MondB.isEnabled = false
self.MondL.isEnabled = false
self.MondD.isEnabled = false
self.TuesB.isEnabled = false
self.TuesL.isEnabled = false
self.TuesD.isEnabled = false
self.WedsB.isEnabled = false
self.WedsL.isEnabled = false
self.WedsD.isEnabled = false
self.ThurB.isEnabled = false
self.ThurL.isEnabled = false
self.ThurD.isEnabled = false
self.FridB.isEnabled = false
self.FridL.isEnabled = false
self.FridD.isEnabled = false
self.SatuB.isEnabled = false
self.SatuL.isEnabled = false
self.SatuD.isEnabled = false
self.SundB.isEnabled = false
self.SundL.isEnabled = false
self.SundD.isEnabled = false
}
@IBAction func editEnable(_ sender: Any) {
//self.MonB.isHidden = false
// Alert stating Changes can be made
let alert = UIAlertController(title: "Message for Cook",
message: "Time to change the Menu
for Next Week",
preferredStyle:
UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style:
UIAlertAction.Style.default))
present(alert, animated: true, completion: {
//Enable Button capabilities
self.MondB.isEnabled = true
self.MondL.isEnabled = true
self.MondD.isEnabled = true
self.TuesB.isEnabled = true
self.TuesL.isEnabled = true
self.TuesD.isEnabled = true
self.WedsB.isEnabled = true
self.WedsL.isEnabled = true
self.WedsD.isEnabled = true
self.ThurB.isEnabled = true
self.ThurL.isEnabled = true
self.ThurD.isEnabled = true
self.FridB.isEnabled = true
self.FridL.isEnabled = true
self.FridD.isEnabled = true
self.SatuB.isEnabled = true
self.SatuL.isEnabled = true
self.SatuD.isEnabled = true
self.SundB.isEnabled = true
self.SundL.isEnabled = true
self.SundD.isEnabled = true
})
}
@objc func ButtonTapped(_ sender: UIButton)
{
// Create an alert
let alert = UIAlertController(
title: "What to cook?",
message: "Add The Name Of The Dish:",
preferredStyle: .alert)
// Add a text field to the alert for the new item's title
alert.addTextField(configurationHandler: nil)
// Add a "cancel" button to the alert. This one doesn't need a handler
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
// Add a "OK" button to the alert. The handler calls addNewToDoItem()
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
if let Dish = alert.textFields?[0].text
{
self.FoodItemAdded(Food: Dish)
}
}))
// Present the alert to the user
self.present(alert, animated: true, completion: nil)
}
func FoodItemAdded(Food: String)
{
//return self.
//Trying to return the value from here.
}
//Saving the data
}
编写类似于此的函数
func FoodItemAdded(Food: String)->String
{
//setting the parameter value to a variable
let string = Food
//returning new string type variable
return string
}
您可以将 return 值设置为这样的标签,
self.mylabel.text = FoodItemAdded("Burger")
要存储数据,可以使用核心数据/SQLITE。
核心数据:https://youtu.be/tP4OGvIRUC4
数据库:https://youtu.be/c4wLS9py1rU
我是这样解决问题的:
import UIKit
import CoreData
@available(iOS 10.0, *)
class MenuViewController: UIViewController {
//Define all the Output Labels
@IBOutlet weak var MonB: UILabel!
@IBOutlet weak var MonL: UILabel!
@IBOutlet weak var MonD: UILabel!
@IBOutlet weak var TueB: UILabel!
@IBOutlet weak var TueL: UILabel!
@IBOutlet weak var TueD: UILabel!
@IBOutlet weak var WedB: UILabel!
@IBOutlet weak var WedL: UILabel!
@IBOutlet weak var WedD: UILabel!
@IBOutlet weak var ThuB: UILabel!
@IBOutlet weak var ThuL: UILabel!
@IBOutlet weak var ThuD: UILabel!
@IBOutlet weak var FriB: UILabel!
@IBOutlet weak var FriL: UILabel!
@IBOutlet weak var FriD: UILabel!
@IBOutlet weak var SatB: UILabel!
@IBOutlet weak var SatL: UILabel!
@IBOutlet weak var SatD: UILabel!
@IBOutlet weak var SunB: UILabel!
@IBOutlet weak var SunL: UILabel!
@IBOutlet weak var SunD: UILabel!
//Define Buttons to Enable/Disable
@IBOutlet weak var MondB: UIButton!
@IBOutlet weak var MondL: UIButton!
@IBOutlet weak var MondD: UIButton!
@IBOutlet weak var TuesB: UIButton!
@IBOutlet weak var TuesL: UIButton!
@IBOutlet weak var TuesD: UIButton!
@IBOutlet weak var WedsB: UIButton!
@IBOutlet weak var WedsL: UIButton!
@IBOutlet weak var WedsD: UIButton!
@IBOutlet weak var ThurB: UIButton!
@IBOutlet weak var ThurL: UIButton!
@IBOutlet weak var ThurD: UIButton!
@IBOutlet weak var FridB: UIButton!
@IBOutlet weak var FridL: UIButton!
@IBOutlet weak var FridD: UIButton!
@IBOutlet weak var SatuB: UIButton!
@IBOutlet weak var SatuL: UIButton!
@IBOutlet weak var SatuD: UIButton!
@IBOutlet weak var SundB: UIButton!
@IBOutlet weak var SundL: UIButton!
@IBOutlet weak var SundD: UIButton!
//Define the Edit button to enable Tap Buttons
@IBOutlet var editButton: UIBarButtonItem!
//Define all the Output Buttons
@IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func MoL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func MoD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuD(_ sender: UIButton) {
ButtonTapped(sender)
}
//VIEWDIDLOAD Parameters
override func viewDidLoad() {
super.viewDidLoad()
//Hide the Navigation Bar on the screen
self.navigationController?.isNavigationBarHidden = false
//Define the Title for the page
self.title = "~~~Weekly Menu~~~"
//Enable the buttons to edit
self.navigationItem.rightBarButtonItem = self.editButton
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Disable UIButton by default
self.MondB.isEnabled = false
self.MondL.isEnabled = false
self.MondD.isEnabled = false
self.TuesB.isEnabled = false
self.TuesL.isEnabled = false
self.TuesD.isEnabled = false
self.WedsB.isEnabled = false
self.WedsL.isEnabled = false
self.WedsD.isEnabled = false
self.ThurB.isEnabled = false
self.ThurL.isEnabled = false
self.ThurD.isEnabled = false
self.FridB.isEnabled = false
self.FridL.isEnabled = false
self.FridD.isEnabled = false
self.SatuB.isEnabled = false
self.SatuL.isEnabled = false
self.SatuD.isEnabled = false
self.SundB.isEnabled = false
self.SundL.isEnabled = false
self.SundD.isEnabled = false
}
@IBAction func editEnable(_ sender: Any) {
// Alert stating Changes can be made
let alert = UIAlertController(title: "Message for Cook",
message: "Time to change the Menu for Next Week",
preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default))
present(alert, animated: true, completion: {
//Enable Button capabilities
self.MondB.isEnabled = true
self.MondL.isEnabled = true
self.MondD.isEnabled = true
self.TuesB.isEnabled = true
self.TuesL.isEnabled = true
self.TuesD.isEnabled = true
self.WedsB.isEnabled = true
self.WedsL.isEnabled = true
self.WedsD.isEnabled = true
self.ThurB.isEnabled = true
self.ThurL.isEnabled = true
self.ThurD.isEnabled = true
self.FridB.isEnabled = true
self.FridL.isEnabled = true
self.FridD.isEnabled = true
self.SatuB.isEnabled = true
self.SatuL.isEnabled = true
self.SatuD.isEnabled = true
self.SundB.isEnabled = true
self.SundL.isEnabled = true
self.SundD.isEnabled = true
})
}
@objc func ButtonTapped(_ sender: UIButton)
{
// Create an alert
let alert = UIAlertController(
title: "What to cook?",
message: "Add The Name Of The Dish:",
preferredStyle: .alert)
// Add a text field to the alert for the new item's title
alert.addTextField(configurationHandler: nil)
// Add a "cancel" button to the alert. This one doesn't need a handler
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
// Add a "OK" button to the alert. The handler calls addNewToDoItem()
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
if let Dish = alert.textFields?[0].text {
if sender == self.MondB {
self.MonB.text = Dish
}
else if sender == self.MondL
{
self.MonL.text = Dish
}
else if sender == self.MondD
{
self.MonD.text = Dish
}
else if sender == self.TuesB
{
self.TueB.text = Dish
}
else if sender == self.TuesL
{
self.TueL.text = Dish
}
else if sender == self.TuesD
{
self.TueD.text = Dish
}
else if sender == self.WedsB
{
self.WedB.text = Dish
}
else if sender == self.WedsL
{
self.WedL.text = Dish
}
else if sender == self.WedsD
{
self.WedD.text = Dish
}
else if sender == self.ThurB
{
self.ThuB.text = Dish
}
else if sender == self.ThurL
{
self.ThuL.text = Dish
}
else if sender == self.ThurD
{
self.ThuD.text = Dish
}
else if sender == self.FridB
{
self.FriB.text = Dish
}
else if sender == self.FridL
{
self.FriL.text = Dish
}
else if sender == self.FridD
{
self.FriD.text = Dish
}
else if sender == self.SatuB
{
self.SatB.text = Dish
}
else if sender == self.SatuL
{
self.SatL.text = Dish
}
else if sender == self.SatuD
{
self.SatD.text = Dish
}
else if sender == self.SundB
{
self.SunB.text = Dish
}
else if sender == self.SundL
{
self.SunL.text = Dish
}
else if sender == self.SundD
{
self.SunD.text = Dish
}
}}))
// Present the alert to the user
self.present(alert, animated: true, completion: nil)
}
我是构建应用程序的新手,但正在学习。我正在尝试从警报中获取价值并将其保存到标签中。这是为一周创建一个简单的时间表。我有两个困难,一个是从函数中获取值以将其显示到我的标签上,该标签位于 Main.Sotryboard.
上按下的按钮后面我正在尝试取回我的值以分配给标签 MonB
@IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
//试图让我的文本输入回到这里。
我正在尝试从以下函数中获取值:
func FoodItemAdded(Food: String)
{
//return self.
//Trying to return the value from here.
}
我也环顾四周,但不知道如何保存这些标签值,所以当我关闭应用程序时,它仍然保存为持久性。
这是完整的代码。
import UIKit
class MenuViewController: UIViewController {
//Define all the Output Labels
@IBOutlet weak var MonB: UILabel!
@IBOutlet weak var MonL: UILabel!
@IBOutlet weak var MonD: UILabel!
@IBOutlet weak var TueB: UILabel!
@IBOutlet weak var TueL: UILabel!
@IBOutlet weak var TueD: UILabel!
@IBOutlet weak var WedB: UILabel!
@IBOutlet weak var WedL: UILabel!
@IBOutlet weak var WedD: UILabel!
@IBOutlet weak var ThuB: UILabel!
@IBOutlet weak var ThuL: UILabel!
@IBOutlet weak var ThuD: UILabel!
@IBOutlet weak var FriB: UILabel!
@IBOutlet weak var FriL: UILabel!
@IBOutlet weak var FriD: UILabel!
@IBOutlet weak var SatB: UILabel!
@IBOutlet weak var SatL: UILabel!
@IBOutlet weak var SatD: UILabel!
@IBOutlet weak var SunB: UILabel!
@IBOutlet weak var SunL: UILabel!
@IBOutlet weak var SunD: UILabel!
//Define Buttons to Enable/Disable
@IBOutlet weak var MondB: UIButton!
@IBOutlet weak var MondL: UIButton!
@IBOutlet weak var MondD: UIButton!
@IBOutlet weak var TuesB: UIButton!
@IBOutlet weak var TuesL: UIButton!
@IBOutlet weak var TuesD: UIButton!
@IBOutlet weak var WedsB: UIButton!
@IBOutlet weak var WedsL: UIButton!
@IBOutlet weak var WedsD: UIButton!
@IBOutlet weak var ThurB: UIButton!
@IBOutlet weak var ThurL: UIButton!
@IBOutlet weak var ThurD: UIButton!
@IBOutlet weak var FridB: UIButton!
@IBOutlet weak var FridL: UIButton!
@IBOutlet weak var FridD: UIButton!
@IBOutlet weak var SatuB: UIButton!
@IBOutlet weak var SatuL: UIButton!
@IBOutlet weak var SatuD: UIButton!
@IBOutlet weak var SundB: UIButton!
@IBOutlet weak var SundL: UIButton!
@IBOutlet weak var SundD: UIButton!
//Define the Edit button to enable Tap Buttons
@IBOutlet var editButton: UIBarButtonItem!
//Define all the Output Buttons
@IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func MoL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func MoD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuD(_ sender: UIButton) {
ButtonTapped(sender)
}
//VIEWDIDLOAD Parameters
override func viewDidLoad() {
super.viewDidLoad()
//Hide the Navigation Bar on the screen
self.navigationController?.isNavigationBarHidden = false
//Define the Title for the page
self.title = "~~~Weekly Menu~~~"
//Enable the buttons to edit
self.navigationItem.rightBarButtonItem = self.editButton
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Disable UIButton by default
self.MondB.isEnabled = false
self.MondL.isEnabled = false
self.MondD.isEnabled = false
self.TuesB.isEnabled = false
self.TuesL.isEnabled = false
self.TuesD.isEnabled = false
self.WedsB.isEnabled = false
self.WedsL.isEnabled = false
self.WedsD.isEnabled = false
self.ThurB.isEnabled = false
self.ThurL.isEnabled = false
self.ThurD.isEnabled = false
self.FridB.isEnabled = false
self.FridL.isEnabled = false
self.FridD.isEnabled = false
self.SatuB.isEnabled = false
self.SatuL.isEnabled = false
self.SatuD.isEnabled = false
self.SundB.isEnabled = false
self.SundL.isEnabled = false
self.SundD.isEnabled = false
}
@IBAction func editEnable(_ sender: Any) {
//self.MonB.isHidden = false
// Alert stating Changes can be made
let alert = UIAlertController(title: "Message for Cook",
message: "Time to change the Menu
for Next Week",
preferredStyle:
UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style:
UIAlertAction.Style.default))
present(alert, animated: true, completion: {
//Enable Button capabilities
self.MondB.isEnabled = true
self.MondL.isEnabled = true
self.MondD.isEnabled = true
self.TuesB.isEnabled = true
self.TuesL.isEnabled = true
self.TuesD.isEnabled = true
self.WedsB.isEnabled = true
self.WedsL.isEnabled = true
self.WedsD.isEnabled = true
self.ThurB.isEnabled = true
self.ThurL.isEnabled = true
self.ThurD.isEnabled = true
self.FridB.isEnabled = true
self.FridL.isEnabled = true
self.FridD.isEnabled = true
self.SatuB.isEnabled = true
self.SatuL.isEnabled = true
self.SatuD.isEnabled = true
self.SundB.isEnabled = true
self.SundL.isEnabled = true
self.SundD.isEnabled = true
})
}
@objc func ButtonTapped(_ sender: UIButton)
{
// Create an alert
let alert = UIAlertController(
title: "What to cook?",
message: "Add The Name Of The Dish:",
preferredStyle: .alert)
// Add a text field to the alert for the new item's title
alert.addTextField(configurationHandler: nil)
// Add a "cancel" button to the alert. This one doesn't need a handler
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
// Add a "OK" button to the alert. The handler calls addNewToDoItem()
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
if let Dish = alert.textFields?[0].text
{
self.FoodItemAdded(Food: Dish)
}
}))
// Present the alert to the user
self.present(alert, animated: true, completion: nil)
}
func FoodItemAdded(Food: String)
{
//return self.
//Trying to return the value from here.
}
//Saving the data
}
编写类似于此的函数
func FoodItemAdded(Food: String)->String
{
//setting the parameter value to a variable
let string = Food
//returning new string type variable
return string
}
您可以将 return 值设置为这样的标签,
self.mylabel.text = FoodItemAdded("Burger")
要存储数据,可以使用核心数据/SQLITE。 核心数据:https://youtu.be/tP4OGvIRUC4 数据库:https://youtu.be/c4wLS9py1rU
我是这样解决问题的:
import UIKit
import CoreData
@available(iOS 10.0, *)
class MenuViewController: UIViewController {
//Define all the Output Labels
@IBOutlet weak var MonB: UILabel!
@IBOutlet weak var MonL: UILabel!
@IBOutlet weak var MonD: UILabel!
@IBOutlet weak var TueB: UILabel!
@IBOutlet weak var TueL: UILabel!
@IBOutlet weak var TueD: UILabel!
@IBOutlet weak var WedB: UILabel!
@IBOutlet weak var WedL: UILabel!
@IBOutlet weak var WedD: UILabel!
@IBOutlet weak var ThuB: UILabel!
@IBOutlet weak var ThuL: UILabel!
@IBOutlet weak var ThuD: UILabel!
@IBOutlet weak var FriB: UILabel!
@IBOutlet weak var FriL: UILabel!
@IBOutlet weak var FriD: UILabel!
@IBOutlet weak var SatB: UILabel!
@IBOutlet weak var SatL: UILabel!
@IBOutlet weak var SatD: UILabel!
@IBOutlet weak var SunB: UILabel!
@IBOutlet weak var SunL: UILabel!
@IBOutlet weak var SunD: UILabel!
//Define Buttons to Enable/Disable
@IBOutlet weak var MondB: UIButton!
@IBOutlet weak var MondL: UIButton!
@IBOutlet weak var MondD: UIButton!
@IBOutlet weak var TuesB: UIButton!
@IBOutlet weak var TuesL: UIButton!
@IBOutlet weak var TuesD: UIButton!
@IBOutlet weak var WedsB: UIButton!
@IBOutlet weak var WedsL: UIButton!
@IBOutlet weak var WedsD: UIButton!
@IBOutlet weak var ThurB: UIButton!
@IBOutlet weak var ThurL: UIButton!
@IBOutlet weak var ThurD: UIButton!
@IBOutlet weak var FridB: UIButton!
@IBOutlet weak var FridL: UIButton!
@IBOutlet weak var FridD: UIButton!
@IBOutlet weak var SatuB: UIButton!
@IBOutlet weak var SatuL: UIButton!
@IBOutlet weak var SatuD: UIButton!
@IBOutlet weak var SundB: UIButton!
@IBOutlet weak var SundL: UIButton!
@IBOutlet weak var SundD: UIButton!
//Define the Edit button to enable Tap Buttons
@IBOutlet var editButton: UIBarButtonItem!
//Define all the Output Buttons
@IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func MoL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func MoD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func TuD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func WeD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func ThD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func FrD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SaD(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuB(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuL(_ sender: UIButton) {
ButtonTapped(sender)
}
@IBAction func SuD(_ sender: UIButton) {
ButtonTapped(sender)
}
//VIEWDIDLOAD Parameters
override func viewDidLoad() {
super.viewDidLoad()
//Hide the Navigation Bar on the screen
self.navigationController?.isNavigationBarHidden = false
//Define the Title for the page
self.title = "~~~Weekly Menu~~~"
//Enable the buttons to edit
self.navigationItem.rightBarButtonItem = self.editButton
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Disable UIButton by default
self.MondB.isEnabled = false
self.MondL.isEnabled = false
self.MondD.isEnabled = false
self.TuesB.isEnabled = false
self.TuesL.isEnabled = false
self.TuesD.isEnabled = false
self.WedsB.isEnabled = false
self.WedsL.isEnabled = false
self.WedsD.isEnabled = false
self.ThurB.isEnabled = false
self.ThurL.isEnabled = false
self.ThurD.isEnabled = false
self.FridB.isEnabled = false
self.FridL.isEnabled = false
self.FridD.isEnabled = false
self.SatuB.isEnabled = false
self.SatuL.isEnabled = false
self.SatuD.isEnabled = false
self.SundB.isEnabled = false
self.SundL.isEnabled = false
self.SundD.isEnabled = false
}
@IBAction func editEnable(_ sender: Any) {
// Alert stating Changes can be made
let alert = UIAlertController(title: "Message for Cook",
message: "Time to change the Menu for Next Week",
preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default))
present(alert, animated: true, completion: {
//Enable Button capabilities
self.MondB.isEnabled = true
self.MondL.isEnabled = true
self.MondD.isEnabled = true
self.TuesB.isEnabled = true
self.TuesL.isEnabled = true
self.TuesD.isEnabled = true
self.WedsB.isEnabled = true
self.WedsL.isEnabled = true
self.WedsD.isEnabled = true
self.ThurB.isEnabled = true
self.ThurL.isEnabled = true
self.ThurD.isEnabled = true
self.FridB.isEnabled = true
self.FridL.isEnabled = true
self.FridD.isEnabled = true
self.SatuB.isEnabled = true
self.SatuL.isEnabled = true
self.SatuD.isEnabled = true
self.SundB.isEnabled = true
self.SundL.isEnabled = true
self.SundD.isEnabled = true
})
}
@objc func ButtonTapped(_ sender: UIButton)
{
// Create an alert
let alert = UIAlertController(
title: "What to cook?",
message: "Add The Name Of The Dish:",
preferredStyle: .alert)
// Add a text field to the alert for the new item's title
alert.addTextField(configurationHandler: nil)
// Add a "cancel" button to the alert. This one doesn't need a handler
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
// Add a "OK" button to the alert. The handler calls addNewToDoItem()
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
if let Dish = alert.textFields?[0].text {
if sender == self.MondB {
self.MonB.text = Dish
}
else if sender == self.MondL
{
self.MonL.text = Dish
}
else if sender == self.MondD
{
self.MonD.text = Dish
}
else if sender == self.TuesB
{
self.TueB.text = Dish
}
else if sender == self.TuesL
{
self.TueL.text = Dish
}
else if sender == self.TuesD
{
self.TueD.text = Dish
}
else if sender == self.WedsB
{
self.WedB.text = Dish
}
else if sender == self.WedsL
{
self.WedL.text = Dish
}
else if sender == self.WedsD
{
self.WedD.text = Dish
}
else if sender == self.ThurB
{
self.ThuB.text = Dish
}
else if sender == self.ThurL
{
self.ThuL.text = Dish
}
else if sender == self.ThurD
{
self.ThuD.text = Dish
}
else if sender == self.FridB
{
self.FriB.text = Dish
}
else if sender == self.FridL
{
self.FriL.text = Dish
}
else if sender == self.FridD
{
self.FriD.text = Dish
}
else if sender == self.SatuB
{
self.SatB.text = Dish
}
else if sender == self.SatuL
{
self.SatL.text = Dish
}
else if sender == self.SatuD
{
self.SatD.text = Dish
}
else if sender == self.SundB
{
self.SunB.text = Dish
}
else if sender == self.SundL
{
self.SunL.text = Dish
}
else if sender == self.SundD
{
self.SunD.text = Dish
}
}}))
// Present the alert to the user
self.present(alert, animated: true, completion: nil)
}