I would like to have a user input numbers and then have a calculation as the result. I have succeeded with this but I would like the user to be able to input doubles if they want too and also have the result be a double. This is my issue. I have an input TextField box and a Label result. So two Outlets. So the code I have pasted below works but only for whole numbers (ints) Hope this makes sense. Any ideas? Keep in mind I am a newbie to Swift Programming. cheers
using Xcode:
#IBOutlet weak var userInput: NSTextField!
#IBOutlet weak var result: NSTextField!
var value = 100
#IBAction func pushButtonforResult(_ sender: Any) {
result.integerValue = value / userInput.integerValue
}
Related
I apologize if this has been asked before but I couldn't find what I was looking for online over the last few hours. I'm still functional a noob with swift.
I am trying to store the stringValue of a TextField when I click a NSButton. If I click anywhere and then click on the NSButton the code works perfect but if I don't click the stringValue is still reporting the previous value.
#IBOutlet weak var NameText: NSTextField!
#IBOutlet weak var SaveChangesAccountButton: NSButton!
var selectedAccountItemNumber = NSInteger()
#IBAction func SaveAccountChanges(_ sender: Any)
{
let AccountName = NameText.stringValue
AccountingData.instance.book.account[selectedAccountItemNumber].name = AccountName
}
You have to call validateEditing() on the text field.
And please conform to the naming convention that variable and function names start with a lowercase letter and don't use NSInteger in Swift.
#IBOutlet weak var nameText: NSTextField!
#IBOutlet weak var saveChangesAccountButton: NSButton!
var selectedAccountItemNumber = 0
#IBAction func saveAccountChanges(_ sender: Any)
{
nameText.validateEditing()
let accountName = nameText.stringValue
AccountingData.instance.book.account[selectedAccountItemNumber].name = accountName
}
This answer shows how to enter a string into an NSScrollView in Swift 4 (when developing Mac apps in Xcode 10):
#IBOutlet weak var myScrollView: NSScrollView!
myScrollView.documentView!.insertText("Hello world")
But how do you read this text (get the text string back into a variable)?
So similar to what stringValue does for NSTextField:
#IBOutlet weak var myTextField: NSTextField!
var myTextString = myTextField.stringValue
I'm struggling to find straightforward documentation. Apple's own documentation seems to be shallow at this point, but they are indicating that this is the right component to use:
let myTextView: NSTextView = myScrollView.documentView! as! NSTextView
let myText:String = myTextView.string
This question already has answers here:
Converting String to Int with Swift
(31 answers)
Closed 5 years ago.
I'm having some difficulty getting one of my view controllers to recognize a text input. I'm getting the "Cannot convert value of type 'String' to expected argument type 'Int'" error. I've seen some questions on stack overflow regarding the String/Int conversion but none that seemed to fix this specific situation.
The user is expected to input a number into the text field to set the value of pointsNeededText. The parameters of class Goal, however, state that the variable is an Int. How can I have the user type in a number into the text field and have it recognized as an Int? I set the keyboard type for that specific text field to be the number pad. Is this even the best way to do this? Should it be set up as something other than a text field? I'm a rookie at this, so any and all help would be greatly appreciated! Thank you!
import UIKit
class AddGoalsTableViewController: UITableViewController {
var goal:Goal?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SaveGoal" {
goal = Goal(goalText: nameOfRewardText.text!, pointsToCompleteGoal: pointsNeededText.text!, pointsEarnedTowardsGoal: goalProgressText.text!)
}
}
#IBOutlet var goalTableTitleText : UILabel!
#IBOutlet weak var goalProgressText: UILabel!
#IBOutlet weak var nameOfRewardText: UITextField!
#IBOutlet weak var pointsNeededText: UITextField!
#IBOutlet weak var repeatSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
If the initializer for the Goal class needs the values as Int, you need to explicitly cast it from String to Int for it to work.
let integerValue = Int(stringValue)
Note that this generates an optional value, and thus, has to be unwrapped if your Goal init doesn't accept optionals.
I have 2 textfields. If they both has a float value bigger than 100, when you click on my button it should allow you to go to another page.
So far so good, however in my code the text field can't have either int or float or doubles...
What can I do?
As Lukas says you need to convert it to a string. If you are capturing the value in the textfield on button click, you need to convert it, like so:
if let doubleValue = Double(textField.text!) {
}
I think based on what you have said, you need to do something like this:
class ViewController: UIViewController {
#IBOutlet weak var box1: UITextField!
#IBOutlet weak var box2: UITextField!
#IBOutlet weak var Check: UIButton!
#IBOutlet weak var Page1: UILabel!
#IBAction func didPressCheckButton(sender: UIButton) {
if let stringValue = box1.text {
if let doubleValue = Double(stringValue) {
if doubleValue > 100 {
print("Navigate to next page")
}
}
}
}
}
You will need to modify the check so that you check if both text boxes have values over 100, but this is a starting point.
I have Swift 2 and I can't access my textfield's text, what should I do?
#IBOutlet weak var CoolField: UITextField!
let texts = Int(CoolField.text?)
There is always the error message that Instance member CoolField cannot be used with type ViewController
You cannot define the following as class variable let texts = Int(CoolField.text?) , it doesn't work like that in Swift :
Keep this defined as it is : #IBOutlet weak var CoolField: UITextField!
Define under it as class variable : var texts : Int!
Then use it in viewDidLoad for example like : texts = Int(CoolField.text!)
Thats should work fine, Good luck !