How can I get the contents of the text box below and display the retrieved text on the label when I press the button? - swift

enter image description here
Please tell me what kind of code to write

I suppose you've already created the reference for the textbox as TextView. Now you need to create a new IBOutlet for the label. Once you have the reference to the label you can get the text value from the textview and show it in the label.
#IBAction func display(_sender) {
self.labelView.text = self.TextView.text
//if you want to clear the textview
self.TextView.text = ""
}

Create outlet for your label
#IBOutlet weak var yourlabel: UILabel!
On Button Action right code to set the value to label.
#IBAction func display(_sender) {
yourlabel.text = self.TextView.text
}

Related

Setting the text of a UILabel with a constant

I am a total newby to xCode and Swift so please excuse me if this question is silly.
I am trying to update 2 UILabels and a UIImageView by using the sender.currentTitle of the button pressed.
If a user presses the 'Honduras' button then I want to populate the 2 UILabel with it's corresponding information and the UIImageView with its corresponding image file.
I have the first UILabel working and the UIImageView working but cannot find a way to populate the 2nd UILabel with its corresponding text (which is stored in a constant).
UILabel 1 is called #IBOutlet weak var countryLabel: UILabel!
UILabel 2 is called #IBOutlet weak var peopleLabel: UILabel!
UImageView is called #IBOutlet weak var peopleUIImageView: UIImageView!
The information/file name I want to pass into these 3 are called:
"Honduras"
let peopleHondurasText = "The Lenca"
"peopleHonduras.png"
I have the UILabel 1 working like this:
countryLabel.text = sender.currentTitle
I have the image working by calling this func:
func findCountryImages() {
peopleUIImageView.image = UIImage(named: "people\(countryLabel.text! .filter { !" ".contains($0) }).png")
}
and I am trying to call the constant "peopleHondurasText" like I did the UIImage but it isn't working.
Here is my very amateur code:
peopleLabel.text = "people\(countryLabel.text! .filter { !" ".contains($0) })Text"
Where this:
"people\(countryLabel.text! .filter { !" ".contains($0) })Text"
Should be equal to the constant named:
peopleHondurasText
I want to be able to do this so if a user clicks on any country button it will populate those 2 UILabels and UIImageViews with that countries text and images.
I know I must be doing something fundamentally incorrect or maybe going about this complete in the wrong but if anyone could let me know either how to pass the constant name into the UILable
or tell me that you cannot do this and I should try something different, that would be greatly appreciated.
How come you don't just do:
peopleLabel.text = peopleHondurasText

I can't change the text of a UILabel in Swift

I am trying to change the text of a UILabel in my code, but the text won't change.
I tried to use the well-known command for changing the text, "NameOfLabel.text = 'Hello", but that did not work. So I tried to put it in a start function so you would click a UIButton and it would change the text, didn't work either.
#IBOutlet var nameOfRobot: UILabel!
#IBAction func startButton(_ sender: Any){
let nameNumber = Int.random(in: 1...3)
if nameNumber == 1 {
self.nameOfRobot.text = "Ben"
}
if nameNumber == 2 {
self.nameOfRobot.text = "Oliver"
}
if nameNumber == 3 {
self.nameOfRobot.text = "Colton"
}
}
I want it to choose a number between 1 and three and have it change the UILabel to that name. When I start the app though, it works, but it doesn't change the text of the label.
Looks like you forgot to connect the action to the button press:
You can tell this is done correctly by looking at the full circle indicator in the editor:
EDIT: Setting the correct class to the viewController in the storyboard:

How to get text input from UITextField?

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!)

What is the syntax for editing a label's constraints/position in swift?

I want to set up a conditional that will change the location of a particular label in the view in swift. I figure I will put the change of the label's position in the viewDidLoad(), but if I put in this modification to move the label's constraints based on the conditional, will it (upon being reloaded) have the label in the position/constraint set manually on the storyboard? Or would I need to code that position back in?
if condition x{
//move label position
} else{
//keep the position set in storyboard, even upon reload
}
Is this possible?
///I have created the button & label on storyboard. And changing the position of label when button is clicked. I have added the constraint to the label on storyboard & created the outlet of top constraint.
var flag = true
#IBOutlet weak var UiLblConstraintTop: NSLayoutConstraint!
#IBAction func ChangePositionOfLabel(sender: UIButton) {
if flag{
self.UiLblConstraintTop.constant = 300
}
else{
self.UiLblConstraintTop.constant = 50
}
flag = !flag
self.view.layoutIfNeeded()
}

Image change dependent of UISlider value

I have some images that need to be displayed dependent on the value of a slider. It is not working - where am I going wrong?
#IBOutlet weak var image1: UISlider! [this is connected to the slider NOT the image]
image1.setImage(UIImage(named:"image1.png"),forState:UIControlState.Selected)
#IBAction func sliderValue(sender: UIButton) {
let slider value = Int(sender.value)
if sliderValue == 1 {
image1.setImage
}
}
Maybe you want to do something like that ? You check the value of your slider each time its value change, then you set an image to your imageView.
#IBOutlet weak var imageView: UIImageView!
#IBAction func sliderValueChanged(sender: UISlider) {
var sliderValue = Int(sender.value)
if sliderValue == 1 {
self.imageView.image = UIImage(named: "foo")
}
}
To connect your Slider in your storyboard to your code :
In the right panel of your storyboard, select your ViewController, go to the third tab. In Custom Class > Class field, enter class you are using. It should autocomplete.
Click on "SHOW THE ASSISTANT EDITOR" (two circles icon) to split your window between your storyboard and your code.
Press Ctrl + drag the element (here your slider) to the code.
Choose Outlet or Action. For your imageView, choose outlet, because you will be able to interact with it programmatically, but the user won't do any action with it. I'll let you search Google if you need more details on IBOutlet and IBAction
Credits : Images from this site