Add button doesn't add text to UITextView in swift - swift

I'm testing the my add button in the view controller file. It should go to validateName function in my model file and which returns true and then enables my addButton. But when I click on "Add" it doesn't change the text of the UITextView.
In viewcontroller.swift:
import UIKit
class ViewController: UIViewController,UITextFieldDelegate{
#IBOutlet weak var nameInputText: UITextField!
#IBOutlet weak var usernameInputText: UITextField!
#IBOutlet weak var passwordInputText: UITextField!
#IBOutlet weak var phoneInputText: UITextField!
#IBOutlet weak var emailInputText: UITextField!
#IBOutlet weak var addButton: UIButton!
#IBOutlet weak var textviewText: UITextView!
let model = Model()
#IBAction func addButton(_ sender: UIButton) {
textviewText.text = "hi"
}
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String)
-> Bool
{
addButton.isEnabled = false
let text: String = nameInputText.text!
var test_name: Bool
var name_array = [String]()
test_name = model.validateName(nametext: text)
if test_name == true{
addButton.isEnabled = true
name_array.append(text)
return true
}
return (true)
}
override func viewDidLoad() {
super.viewDidLoad()
usernameInputText.delegate = self
nameInputText.delegate = self
passwordInputText.isSecureTextEntry = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool{
usernameInputText.resignFirstResponder()
nameInputText.resignFirstResponder()
return (true)
}
}
//In model.swift:
import Foundation
class Model {
func validateName(nametext: String) -> Bool{
return true
}
}

Related

On pressed touch Hide/Show UIImage, and count number down from 10 to 0

On every pressed touch I want UILabel to count number down one by one from 10 to 0.
Also when pressed I want to show UIImageView: "MyRocketWith", and when release the pressed touch I want to show UIImageView: "MyRocket".
class ViewController: UIViewController {
#IBOutlet weak var MyRocket: UIImageView!
#IBOutlet weak var MyRocketWith: UIImageView!
#IBOutlet weak var Countdown: UILabel!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch: UITouch? = touches.first
if touch?.view != self.MyRocket {
self.MyRocketWith.isHidden = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
The below code shows and hides the UIImageView on click of UILabel
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var MyRocketWith: UIImageView!
#IBOutlet weak var CountDownLabel: UILabel!
#IBOutlet weak var MyRocket: UIImageView!
var count = 10
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
CountDownLabel.isUserInteractionEnabled = true
CountDownLabel.addGestureRecognizer(tap)
MyRocket.isHidden = false
MyRocketWith.isHidden = true
MyRocketWith.tag=0
}
#IBAction func tapFunction(sender: UITapGestureRecognizer) {
print("tap working")
count = count-1
CountDownLabel.text = String(count)
if MyRocketWith.tag == 0
{
MyRocket.isHidden = true
MyRocketWith.isHidden = false
MyRocketWith.tag=1
}
else
{
MyRocket.isHidden = false
MyRocketWith.isHidden = true
MyRocketWith.tag=0
}
}
}

Unable to get the value back from a function to assign it to a label

I'm new to building an application but learning. I am trying to take value from the alert and save it to the label. This is to create a simple timetable to for the Week. I have two difficulties one is getting value back from the function to display it to my label which is placed behind the Button pressed on the Main.Sotryboard.
I am trying to get my value back here to assign to the Label MonB
#IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
//Trying to get my text input back here.
I am trying to get the value from the function below:
func FoodItemAdded(Food: String)
{
//return self.
//Trying to return the value from here.
}
I also looked around but don't know how to save these label values so When i close the app it still remains saved as persistent.
This is the complete Code.
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
}
Write your function similar to this
func FoodItemAdded(Food: String)->String
{
//setting the parameter value to a variable
let string = Food
//returning new string type variable
return string
}
You can set the return value to a label like this,
self.mylabel.text = FoodItemAdded("Burger")
To store data, you can use core data/ SQLITE.
Coredata: https://youtu.be/tP4OGvIRUC4
Sqlite: https://youtu.be/c4wLS9py1rU
This is how I solved the problem:
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)
}

error message with IBAction

import UIKit
import Firebase
class ViewController: UIViewController {
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBOutlet weak var emailkeyboard: UITextField!
#IBOutlet weak var passwordkeyboard: UITextField!
#IBOutlet weak var myaccountButton: UIButton!
#IBOutlet weak var welcomeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Built in method
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
#IBAction func buttonPressed(sender: UIButton) {
self.emailkeyboard.resignFirstResponder()
self.passwordkeyboard.resignFirstResponder()
}
#IBAction func myaccountbutton(sender: AnyObject) {
FIRAuth.auth()?.signInWithEmail(self.emailField.text!, password: self.passwordField.text!, completion: { (user,error) in
}
#IBAction func createaccountButton(sender: AnyObject) {
FIRAuth.auth()?.createUserWithEmail(emailField.text!, password: passwordField.text!) { (user, error) in
if error == nil {
print("User Created")
if (FIRAuth.auth()?.currentUser) != nil
{
self.myaccountButton.alpha = 1.0
}
else
{
self.myaccountButton.alpha = 0.0
self.welcomeLabel.text = ""
}
}
}
}
}
}
I cant figure out where to put the ) I asked before but i am still stuck. Sorry guys for asking again.
I am not sure if theres an error in the code above or with the IBAction itself so I have posted all of the code maybe that will help find the problem.
The signInWithEmail() function call is missing its ending parentheses, as well as an actual closure for the closure argument.

How do I dismiss keyboard with a touch? touchesBegan not workig

According to everything I've read here, I should override touchesBegan() to dismiss the keyboard (in my case DatePicker). Unfortunately touching the screen does not dismiss the DatePicker. Touching other UI elements like the UISteppers dismisses the keyboard just fine, and it is using the same function of CloseKeyboard()
class RecordWorkoutTableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var dateTextField: UITextField!
#IBOutlet weak var weightLabel: UILabel!
#IBOutlet weak var setOneLabel: UILabel!
#IBOutlet weak var setTwoLabel: UILabel!
#IBOutlet weak var weightStepper: UIStepper!
#IBOutlet weak var setOneStepper: UIStepper!
#IBOutlet weak var setTwoStepper: UIStepper!
var newDate: NSDate? {
didSet {
dateTextField.text = NSDateToPrettyString(newDate!)
}
}
var newWeight: Int? {
didSet {
weightLabel.text = "\(newWeight!) lbs"
}
}
var newSetOne: Int? {
didSet {
setOneLabel.text = "Set 1: \(newSetOne!) reps"
}
}
var newSetTwo: Int? {
didSet {
setTwoLabel.text = "Set 2: \(newSetTwo!) reps"
}
}
var workout: Workout?
// MARK: UITextFieldDelegate
func textFieldDidBeginEditing(textField: UITextField) {
let datePicker = UIDatePicker()
textField.inputView = datePicker
datePicker.addTarget(self, action: #selector(RecordWorkoutTableViewController.datePickerChanged(_:)), forControlEvents: .ValueChanged)
}
func datePickerChanged(sender: UIDatePicker) {
newDate = sender.date
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
// disable editing of date text. datepicker input only
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
return false
}
// MARK: Helper Functions
func closeKeyboard() {
self.view.endEditing(true)
}
// MARK: Touch Events
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
closeKeyboard()
}
override func viewDidLoad() {
super.viewDidLoad()
dateTextField.delegate = self
newDate = NSDate()
if let lastWorkout = workout {
newWeight = lastWorkout.sets[0].weight
newSetOne = lastWorkout.sets[0].repCount
newSetTwo = lastWorkout.sets[1].repCount
} else {
newWeight = 0
newSetOne = 9
newSetTwo = 8
}
weightStepper.stepValue = 5
weightStepper.maximumValue = 995
weightStepper.value = Double(newWeight!)
setOneStepper.stepValue = 1
setOneStepper.maximumValue = 20
setOneStepper.value = Double(newSetOne!)
setTwoStepper.stepValue = 1
setTwoStepper.maximumValue = 20
setTwoStepper.value = Double(newSetTwo!)
}
#IBAction func weightStepperChanged(sender: UIStepper) {
newWeight = Int(sender.value)
closeKeyboard()
}
#IBAction func setOneStepperChanged(sender: UIStepper) {
newSetOne = Int(sender.value)
closeKeyboard()
}
#IBAction func setTwoStepperChanged(sender: UIStepper) {
newSetTwo = Int(sender.value)
closeKeyboard()
}
}
You said in the comments that you have another class in your app named Set. Since this class is in the same module as your table view controller, Swift is prioritizing it over it's built in class.
You can fix this by either renaming your Set class, or explicitly specifying the Swift module in the function declaration:
override func touchesBegan(touches: Swift.Set<UITouch>, withEvent event: UIEvent?) {
closeKeyboard()
}

Swift: Button Enabling Not Working?

I have a button that is disabled in my view controller. I have IBActions for when two text fields are edited. I am trying to enable the button when two text fields both contain integers. I have tried to do this, but whenever I run the ios simulator, the button stays disabled even when I put integers into each text field. Why is it staying disabled? I am new to swift, so please help me out. Here is the code for my entire project:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var calculatorButton: UIButton!
#IBOutlet weak var inspirationLabel: UILabel!
#IBOutlet weak var beginningLabel: UILabel!
#IBOutlet weak var calculatorContainer: UIView!
#IBOutlet weak var answer1Label: UILabel!
#IBOutlet weak var doneButton: UIButton!
#IBOutlet weak var yourWeightTextField: UITextField!
#IBOutlet weak var calorieNumberTextField: UITextField!
#IBOutlet weak var menuExampleButton: UIButton!
#IBOutlet weak var aboutButton: UIButton!
#IBOutlet weak var calculateButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
yourWeightTextField.delegate = self
calorieNumberTextField.delegate = self
calculateButton.enabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func calculatorButtonTapped(sender: AnyObject) {
calculatorContainer.hidden = false
inspirationLabel.hidden = true
beginningLabel.hidden = true
menuExampleButton.hidden = true
aboutButton.hidden = true
}
#IBAction func yourWeightEditingDidEnd(sender: AnyObject) {
yourWeightTextField.resignFirstResponder()
}
#IBAction func calorieNumberEditingDidEnd(sender: AnyObject) {
calorieNumberTextField.resignFirstResponder()
}
var yourWeightFilled = false
var calorieNumberFilled = false
func yourWeightTextFieldValueValidInt(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// Find out what the text field will be after adding the current edit
let text = (yourWeightTextField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
if let intVal = text.toInt() {
self.yourWeightFilled = true
} else {
self.yourWeightFilled = false
}
return true
}
func calorieNumberTextFieldValueValidInt(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// Find out what the text field will be after adding the current edit
let text = (calorieNumberTextField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
if let intVal = text.toInt() {
self.calorieNumberFilled = true
} else {
self.calorieNumberFilled = false
}
return true
}
#IBAction func yourWeightTextFieldEdited(sender: AnyObject) {
if self.yourWeightFilled && self.calorieNumberFilled {
self.calculateButton.enabled = true
}
}
#IBAction func calorieNumberTextFieldEdited(sender: AnyObject) {
if self.yourWeightFilled && self.calorieNumberFilled {
self.calculateButton.enabled = true
}
}
}
Your delegate methods are a bit mixed up -- they have to be named exactly what the caller expects, or they won't be found, so yourWeightTextFieldValueValidInt() and calorieNumberTextFieldValueValidInt() aren't being called at all. Instead you'll need to handle the edits to both text fields in a single method:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// Find out what the text field will be after adding the current edit
let text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
if textField == yourWeightTextField {
yourWeightFilled = text.toInt() != nil
} else if textField == calorieNumberTextField {
calorieNumberFilled = text.toInt() != nil
}
return true
}