I'm making an app where users put numbers into 4 different textfields and it does something with them on the backend. Right now if they want to change those numbers they have to go back and delete them all and retype them or close the app and reopen it to have all the text fields blank again.
I want to add a button to the bottom of the app that says something like Clear Text and it will clear all 4 textfields.
I've tried looking it up but couldn't find anything like this, Only to make a clear text button appear when editing that text field. I'm looking to clear 4 text fields when a Button is clicked.
This should work:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBOutlet var field1: UITextField!
#IBOutlet var field2: UITextField!
#IBOutlet var field3: UITextField!
#IBOutlet var field4: UITextField!
#IBAction func buttonThatClears(_ sender: UIButton) {
field1.text = ""
field2.text = ""
field3.text = ""
field4.text = ""
field1.becomeFirstResponder()
}
}
Likely will look something like this:
Select the Attributes inspector for the textField.
In the first section, you have Clear button
Below, select Clear when editing begins.
Related
I'm creating an app and I need to login to it and I don't want to use 2 Text Field for insert Email and Password. What I would wish to achieve is: click on the button "Log in", then it will open the classic full screen keyboard that is usually appearing when you are doing click on UITextField in tvOS.
Can someone please write an example of code on how to invoke the keyboard clicking on a UIButton instead of UITextField? Thanks a lot!
While your UI is questionable, once your first text field finishes editing, you could start the other one.
Set the first text field UITextFieldDelegate to the view controller, and then:
class ViewController: UIViewController , UITextFieldDelegate{
#IBOutlet weak var tf2: UITextField!
#IBOutlet weak var tf1: UITextField!
#IBAction func click() {
tf1.becomeFirstResponder()
}
func textFieldDidEndEditing(_ textField: UITextField) {
if (textField == tf1){
tf2.becomeFirstResponder()
}
}
}
If you want a UITextField to open up the keyboard without the user having to tap on it, you can use yourTextField.becomeFirstResponder().
I don't think you can present the keyboard without any associated input view
I'm following official iOS Apps tutorial to make a basic Single View Application in Xcode.
Literally all we have done so far is:
Added a label to the UI and set initial text to 'Meal name:'
Added a textbox to the UI
Added a button to the UI
Then we've added some very simple code to the View Controller declaring the label as an outlet and a button action which, when invoked, should change the label's text to Default Text.
My ViewController code is now identical to the tutorial code namely:
class ViewController: UIViewController {
//MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//MARK: Actions
#IBAction func setDefaultLabelText(_ sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
The problem is that when I press the button in simulator, I get an error message saying an implicitly unwrapped optional value is nil. (app launches fine, it's just when pressing the button)
As I understand it this means something is blank that can't be, but the only optionals I have are:
The textbox, which isn't blank because before I press the button I write 'biscuits' or something in it
The label text, which isn't blank because it's set to 'Meal name:' by default
I really can't work out what supposedly has a nil value that is triggering this error?
As I understand it this means something is blank that can't be
No , This means you need to make sure outlet
mealNameLabel.text = "Default Text" // here mealNameLabel is nil
is connected to the label in IB
I want to refer to a label based on the title of the button pressed.
I have a plenty of buttons, that I connected together in a single action. I want to be able to refer to only one label, that corresponds to the name of button pressed.
The Label names are: aLabel, bLabel, cLabel ...
The buttons pressed are titled "a", "b", "c"...
I ame creating the string with name of the label i want to refer to, but I can't use it as label name to change this particular label values.
I want to use this string to refer to the corresponding label, to change it title, color, and so on.
I tried, casting, looking for a function that changes strings into UILabels. I was also thinking about an array with pointers to the Labels, but I didn't succed even with establishing a pointer to a single Label...
//My code
#IBOutlet weak var a: UIButton!
#IBOutlet weak var b: UIButton!
#IBOutlet weak var c: UIButton!
#IBOutlet weak var aLabel: UILabel!
#IBOutlet weak var bLabel: UILabel!
#IBOutlet weak var cLabel: UILabel!
var currentLabel : String
#IBAction func FieldDisplay(_ sender: UIButton) {
currentLabel = sender.currentTitle! + "Label"
currentLabel.text = "OK"
}
//what I tried
(UILabel)currentLabel.text = "OK"
currentLabel = currentLabel.to.UIlabel
When interface objects are associated in multiple pairs like this — a series of button–label pairs, as you have it — there are two approaches commonly used. One is to assign each pair a tag. For example, in the storyboard, the first button would have tag 1, and the first label would have tag 101. Then the second button would have tag 2, and the second label would have tag 102. And so on.
So now in your IBAction function you look at the tag of the sender, add 100 to it, and call viewWithTag on your view to find the corresponding label.
The other possibility is to use outlet collections. Instead of three button outlets you have one array-of-button outlet; so too for the labels. Now in your IBAction function you get the firstIndex of the button in its array; that, if you've set this up correctly, is the index of the corresponding label in its array.
Remove all outlets and create 2 collections
#IBOutlet var allBts: [UIButton]!
#IBOutlet var allLbls: [UILabel]!
then hook all labels / btns to each , and set a tag for each group ( lbl1 && btn1 tag = 0 , lbl2 && btn2 tag = 1 and so on)
#IBAction func FieldDisplay(_ sender: UIButton) {
allLbls[sender.tag].text = sender.currentTitle! + "Label"
}
You can use value(forKey:) method
#IBAction func buttonTapped(_ sender: UIButton) {
if let currentLabel = value(forKey: sender.currentTitle! + "Label") as? UILabel {
currentLabel.text = "OK"
}
}
https://developer.apple.com/documentation/objectivec/nsobject/1412591-value
I have an IB NSPopUpButton with a list of items and a number of NSTextFields.
The items from the popup are used to populate the text fields (ingredients) until a recipe is complete.
I would like to reset the popup title after each item is used but cannot find the code and syntax to do this.
I'd also like to be able to click on any of the text fields after selecting an ingredient to drop it there. Drag and drop from the popup would be ideal but I cannot find a simple way to do either so am currently using another button adjacent to each text field to initiate the drop.
This is neither elegant nor ideal.
Hopefully someone can propose better solutions.
My code currently looks like this
var ItemLabel: String = ""
#IBAction func Ingredients(_ sender: NSPopUpButton){
ItemLabel = sender.titleOfSelectedItem ?? "Nil"
}
#IBOutlet weak var Ingredient1: NSTextField!
#IBAction func AddIngredient1(_ sender: NSButton){ // button next to text field
Ingredient1Label.stringValue = ItemLabel
// Here I need to reset the popup title
}
#IBOutlet var green1: UIImageView!
#IBOutlet var red1: UIImageView!
#IBOutlet var blue1: UIImageView!
#IBAction func red(sender: UIButton) {
green1.hidden = true
red1.hidden = false
blue1.hidden = true
}
I want to add and remove objects when the button is pressed.
At the moment they hide and are visible, but i want to remove them completely form the scene when the button is pressed.
And i want the one that is not hidden to appear.
i tried add child and subview but it didn't work the code is in GameViewController.
If you are showing/hiding frequently, changing hidden property is ok, however if you want to remove completely you need to remove the particular image view from its super view.
ex - green1.removeFromSuperView() and when you need it, you have to add as subview and while adding provide its frame.