Я новичок в создании приложений, но учусь. Я пытаюсь извлечь значение из предупреждения и сохранить его на этикетке. Это создать простое расписание на неделю. У меня есть две трудности: одна возвращает значение из функции, чтобы отобразить его на моей метке, которая находится за кнопкой, нажатой на главной доске.
Я пытаюсь вернуть свое значение сюда, чтобы присвоить его метке 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
}