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
}
Related
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.
I’m trying to make Xcode print "Nice!" when you type in "Hi". I've used a IBOutlet, but I don’t know how to use the user input in my code. Also BTW I'm using Storyboard and not SwiftUI. It also gives me an error when I try to compare the datatype UIViewController and a String. Here is my view controller code(with the default App Delegate and Scene Delegate code):
import UIKit
class ViewController: UIViewController {
#IBOutlet var yeet: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func fuel(_ yeet:UIViewController) -> Int {
if yeet == ("hi") {
print("Nice!")
}
}
}
your textfield show be setup as
#IBOutlet weak var textFeildName: UITextField!
you will need to change a couple things inside of your file to prevent a crash. I'd delete the textfield and drag it into the assistant view and give it a new name.
but before you press "connect" press the "outlet" tab and change it to "Action" and then a new selector should come up select "Editing Did End" and go to the top and press "Did End On Exit"
after that is done would want to reference the variable of the text field:
example:
#IBAction func TextFieldName(_ sender: Any) {
if(self.TextFeildName.text!.contains("hi")){
print("Nice!")
}
}
On top of all this, you do not compare strings with == that's only if you compare 2 separate strings for example stringOne == stringTwo if you are comparing or asking if a string contains anything you'd want to use the developing language specific string container IE: .contains
Also, please do not include "Xcode" as a tag with your question, as that should be reserved for Xcode related problems. not Swift or objective-c coding issues.
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 have a NSPopUpButton with a built up NSMenu.
However, when an NSMenuItem is selected, it keeps going back to the first item.
In this example, I'd expect "Maged" to be selected.
Any ideas?
Related question/answer: https://stackoverflow.com/a/53387962/157656
Duplicate:
It's been suggested that this is a duplicate of the question
How do I change/modify the displayed title of an NSPopUpButton
"I would like an NSPopUpButton to display a different title than the title of the menu item that is selected."
However, my question was about getting the NSPopUpButton to show the selected item.
In the end, I changed how I did it.
I am using a NSButton to show the menu and NSTextField to display the results.
If anyone is interested in the details, here they are.
Build the menu up and use .representedObject to store whatever you need to access at the other end. I used a struct with the name and code the in it.
You need to assign the NSMenu to the NSButton.menu
Then have a click, something like this.
#IBAction func changeVoiceClicked(_ sender: NSButton)
{
if let event = NSApplication.shared.currentEvent {
NSMenu.popUpContextMenu(sender.menu!, with: event, for: sender)
}
}
Your NSMenuItem should have an action on it, which using a selector points to a function.
Something like this:
#objc func voiceChanged(sender: NSMenuItem)
{
// cope will nil
var voice : VoiceDetail = VoiceDetail();
if (sender.representedObject != nil) {
voice = sender.representedObject as! VoiceDetail;
}
// Do what you need to on menu select.
// update text field.
}
How to get text input from UITextField ?
I tried this:
#IBAction func input(_ sender: UITextField) {
label.text = textfield.text
}
But the label doesn't get the textfield input.
Which Code should I use?
Open Main.storyboard. This is where you can see a preview of your App including your TextField.
Open the Assistant Editor. You can do so by clicking the two circles in the top right corner. Now you should have a split screen View of Main.storyboard and ViewController.swift.
Press the ctrl key and click the textfield. Now drag it in view controller.swift right below the "{" after the view controller statement at the top.
Release the key and the mouse. Now you have to select "Outlet" in the appearing and use a name
Now You can get the text anywhere in your program by calling the name you selected in step 4 and .text . E.g. name.text
What you did is create an Action. So your action function gets called whenever someone e.g. clicks the textfield. If you want to get the text of it then just use sender.text inside the action function.
I hope this helps.
Try to add #IBOutlet instead of #IBAction
For ex:
#IBOutlet weak var yourTextField: UITextField!
and then you can print it out:
print(yourTextField!)