My Vital UITextField code isn't working - ios

#IBOutlet var prevKg: UITextField!
#IBOutlet var prevGm: UITextField!
#IBOutlet var nowKg: UITextField!
#IBOutlet var nowGm: UITextField!
#IBOutlet var resultLabelMetric: UILabel!
#IBAction func findDifMetric(sender: AnyObject) {
var enteredPrevKg = prevKg.text.toInt()
var enteredPrevGm = prevGm.text.toInt()
var enteredNowGm = nowGm.text.toInt()
var enteredNowKg = nowKg.text.toInt()
I recently cracked open a project that I haven't touched for ages, only to find out some of its vital code isn't working! In this app I have 4 text fields in which you type in numbers, and then press a button to find answers. Xcode tells me to insert an '!' or '?' after ".text" but then more errors come up . Can anyone help me debug/figure out my problem?

Related

Looking to accept user input, and calculate a running total - Swift 5/Xcode

I am a beginner working on an app that will function like a golf scorecard. My first issue has come while attempting to accept user input in a (prototype) series of 9 textFields so the user can type in their scores, and then a textView at the end that is not editable that will show the total for the 9 holes. I am trying to get this to be a running total that updates constantly.
I tried passing the inputs from each textField into an array, and returning the sum of the array to a the textView, but still had issues with the data type from the textField being a string, where as I will only be dealing with integers.
My clunky first pass is as follows -
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var holeOneScore: UITextField!
#IBOutlet weak var holeTwoScore: UITextField!
#IBOutlet weak var holeThreeScore: UITextField!
#IBOutlet weak var holeFourScore: UITextField!
#IBOutlet weak var holeFiveScore: UITextField!
#IBOutlet weak var holeSixScore: UITextField!
#IBOutlet weak var holeSevenScore: UITextField!
#IBOutlet weak var holeEightScore: UITextField!
#IBOutlet weak var holeNineScore: UITextField!
#IBOutlet weak var totalForFrontNine: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//MARK: Calculate Scores
#IBAction func calculate(_ sender: Any) {
let hole1:Int = Int(holeOneScore.text!)!
let hole2:Int = Int(holeTwoScore.text!)!
let hole3:Int = Int(holeThreeScore.text!)!
let hole4:Int = Int(holeFourScore.text!)!
let hole5:Int = Int(holeFiveScore.text!)!
let hole6:Int = Int(holeSixScore.text!)!
let hole7:Int = Int(holeSevenScore.text!)!
let hole8:Int = Int(holeEightScore.text!)!
let hole9:Int = Int(holeNineScore.text!)!
let totalArray = [hole1, hole2, hole3, hole4, hole5, hole6, hole7, hole8, hole9]
let totalScore = totalArray.reduce(0, +)
totalForFrontNine.text = String(totalScore)
print(totalForFrontNine!)
}
It worked, but barely. Any thoughts to modify this or a complete refresh is fine! I am not tied to anything as i am using this project to just teach me the basics.
Thanks in advance, cheers - glassGarrett
Like #jawadali mentioned, use IBOutletCollection. Here is a tutorial about how to use it.
As for the other issue,
...the data type from the textField being a string, where as I will only be dealing with integers.
A quick solution is to set the keyboardType of your textfield to UIKeyboardTypeNumberPad, or .numberPad in Swift.
textField.keyboardType = .numberPad
Or in storyboard,

Store user input and show into UITextView

I'm currently teaching myself in Swift and Xcode.
At the moment I'm trying to get some user input over a UITextField and I want to store them in an Array. The array should go through a for-loop to show the stored data in a UITextView.
At the start I tried it with one user input. It worked.
#IBOutlet weak var InputPlayerName: UITextField!
#IBOutlet weak var SubmitPlayer: UIButton!
#IBOutlet weak var ShowPlayerName: UITextView!
#IBOutlet weak var ShowSize: UITextView!
var players = [String]()
#IBAction func SubmittedPlayer(_ sender: Any) {
players.append(ShowPlayerName.text);
let playerssize = players.count;
ShowSize.text = "\(playerssize)";
for player in players {
if(playerssize == 1) {
ShowPlayerName.text = "Player: \n \(player)";
} else {
ShowPlayerName.text += "\(player)";
}
}
I don't get any errors, it just doesn't show me the results. But the arraysize grows if I press the button.
How can I show the Data line per line in the View?

Put multiple UILabels into an array

In my app I currently have 9 labels on my storyboard, each showing a different value. (The values are stored in an array). As far as I know, each label has to be connected from the storyboard to the viewcontroller file separately, which makes my code look like this:
#IBOutlet weak var xValue: UILabel!
#IBOutlet weak var yValue: UILabel!
#IBOutlet weak var zValue: UILabel!
#IBOutlet weak var maxXValue: UILabel!
#IBOutlet weak var maxYValue: UILabel!
#IBOutlet weak var maxZValue: UILabel!
#IBOutlet weak var minXValue: UILabel!
#IBOutlet weak var minYValue: UILabel!
#IBOutlet weak var minZValue: UILabel!
And to set the values, I need to manually do:
xValue.text = arr[0]
yValue.text = arr[1]
...
minYValue = arr[7]
minZValue = arr[8]
Is there a way to connect multiple labels from the storyboard into an array so that I can simply do something like:
for i in 0...8 {
labelArray[i] = arr[i]
}
As rmaddy mentioned in a comment you can use an outlet collection:
#IBOutlet private var labels: [UILabel]!
Then in your storyboard labels will show up under Outlet Collections when right-clicking your ViewController, and you can link multiple labels:
You can put all the UILabel into an array, like this:
let labelArray = [xValue, yValue, zValue]
for i in 0..<labelArray.count-1{
labelArray[i] = arr[i]
}

Passing data between view controllers inside page view controllers in Swift

Sorry if this particular problem has been asked about, I followed the answers on other threads but none of them seemed to work, but I just started learning Swift so all of this is pretty new to me.
So, I have a text field in two View Controllers and I want the third View Control to display a result based on the input from the other two controllers when I press a button.
I followed this tutorial and placed the text fields, label and button like I said before.
I placed my code (which you can see below) inside ViewControl.swift.
The problem is that when I attempt to run it I get a "Thread 1 :EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" error in the last two lines.
class ViewController: UIViewController {
var a: String = ""
var b: String = ""
#IBOutlet weak var aTextField: UITextField!
#IBOutlet weak var bTextField: UITextField!
#IBOutlet weak var calculateButton: UIButton!
#IBOutlet weak var resultLabel: UILabel!
#IBAction func calculateButtonPressed(_ sender: UIButton) {
let a = aTextField.text!;
let b = bTextField.text!;
I think that the error is from the data not passing between the views (because before I had everything in the same view and it worked fine), but since I only have one ViewController.swift file I couldn't figure out how to use a Segue.
Do not declare same variables multiple times. Remove let before a & b . You have already declared a & b globally and then tried to redeclare it inside IBAction
class ViewController: UIViewController {
var a: String = ""
var b: String = ""
#IBOutlet weak var aTextField: UITextField!
#IBOutlet weak var bTextField: UITextField!
#IBOutlet weak var calculateButton: UIButton!
#IBOutlet weak var resultLabel: UILabel!
#IBAction func calculateButtonPressed(_ sender: UIButton) {
a = aTextField.text!;
b = bTextField.text!;
Make sure your control outlets are setted properly.
In your two variables a & b are re-declared.Just update your code like below
#IBAction func calculateButtonPressed(_ sender: UIButton) {
self.a = aTextField.text!
self.b = bTextField.text!
}

Give buttons same behavior (twins)

I would like my UIButtons to interact with each other - (With the corresponding button)
First set of buttons;
#IBOutlet var P1button1: UIButton!
#IBOutlet var P1button2: UIButton!
#IBOutlet var P1button3: UIButton!
#IBOutlet var P1button4: UIButton!
#IBOutlet var P1button5: UIButton!
#IBOutlet var P1button6: UIButton!
#IBOutlet var P1button7: UIButton!
#IBOutlet var P1button8: UIButton!
#IBOutlet var P1button9: UIButton!
Second set of buttons;
#IBOutlet var P2button1: UIButton!
#IBOutlet var P2button2: UIButton!
#IBOutlet var P2button3: UIButton!
#IBOutlet var P2button4: UIButton!
#IBOutlet var P2button5: UIButton!
#IBOutlet var P2button6: UIButton!
#IBOutlet var P2button7: UIButton!
#IBOutlet var P2button8: UIButton!
#IBOutlet var P2button9: UIButton!
In my code I apply an Image to one random P1button from an Array;
var buttons = [P1button1, P1button2, P1button3, P1button4, P1button5, P1button6, P1button7, P1button8, P1button9]
buttons.shuffleInPlace()
buttons[0].setImage(UIImage(named: "Green"), forState: .Normal)
Now, I want to know if there is a way to make the P2buttons "co-operate" with the P1buttons. Meaning that if an Image is applied to P1button1 then P2button1 is set to the same image! (and so forth for all buttons)
Example;
P2button1.setImage = UIImage(P1button1)
This obviously doesn't work but hopefully gives you a clue about what I'm trying to achieve.
One possible approach would be by subclassing UIButton. Perhaps something like this...
class TwinButton {
weak var twin: TwinButton?
}
Now, in your viewDidLoad method, link up all of the twins.
p1button1.twin = p2button1
p2button1.twin = p1button1
Now, I'd write a set of methods that have some "with twin" semantics...
extension TwinButton {
setWithTwin(title: String)
setWithTwin(backgroundImage: UIImage)
}
and those methods simply set the value on self and twin.

Resources