What is wrong with this it is meant to change text in label to "hi" after you click the button.
ViewController.swift:
//
// ViewController.swift
// HelloSwift
//
// Created by Abdel Rahman Osman on 10/13/14.
// Copyright (c) 2014 Abdel Rahman Osman. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var TextGoesHere: UILabel!
#IBAction func PrintText(sender: AnyObject) {
var Text = TextGoesHere.text
Text = "Hi"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Please help me fix this code as I am new to app development.
What you are doing is creating a copy of the text in the label, discard it and reassign a new value, and then discard it again when the variable goes out of scope.
The correct way is by assigning the text directly to the text property:
#IBAction func PrintText(sender: AnyObject) {
TextGoesHere.text = "Hi"
}
Related
I made a simple code to clarify my problem. When I run my app on my iPhone it works properly, but when I press home button - either on simulator or my real iPhone - and press the app icon again, the app runs, but the app's button "TestButtonAction don't work. Same problem when I press power/sleep button and turn it on again.
same problem either when I duple click home button and terminate my app by sliding it's window away, and rerun it again the button don't work or print "Right Image" in the Console
Here is my code, Thanks.
//
// ViewController.swift
// TestingHomeButton
//
// Created by Samuel Oncy on 4/25/18.
// Copyright © 2018 Samuel Oncy. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var TestButtonOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func TestButtonAction(_ sender: UIButton) {
if(TestButtonOutlet.imageView?.image == UIImage(named:"NatureView")){
print("Right Image")
}
}
}
Simply Make UIImage global Variable, If you want to compare in If.
Other ways can also be to solve same problem - Tag is right one.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var TestButtonOutlet: UIButton!
var image = UIImage (named: "NatureView")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func TestButtonAction(_ sender: UIButton) {
// print(TestButtonOutlet.imageView?.image == UIImage(named:"NatureView"))
if (TestButtonOutlet.currentImage == image) {
print(TestButtonOutlet.currentImage)
}
}
}
To check the current image of button, use the currentImage property as follows:-
#IBAction func TestButtonAction(_ sender: UIButton) {
if sender.currentImage.isEqual(UIImage(named: "NatureView")) {
print("Right Image")
}
}
I'm having a difficulty understanding what output.text means. I think my understanding is that output is an instance of the class UIViewController and text is a variable defined in this class. Is this correct? Also, in the function, consoleOut(), why should one do output.text = output.text + text instead of output.text = text?
//
// ViewController.swift
// Guessing Game
//
// Created by Jae Hyun Kim on 8/6/15.
// Copyright (c) 2015 Jae Hyun Kim. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var inputField: UITextField!
#IBOutlet weak var output: UITextView!
var guesses : UInt = 0;
var number : UInt = 0;
var gameover = false;
let MAX_GUESSES = 8;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func consoleOut(text : String) {
output.text = output.text + text;
}
#IBAction func guess(sender: UIButton) {
}
}
output is a UITextView that is defined in the view (the file that shows what screen will appear to the user). So you are correct in saying that text is a variable of the UITextView class.
Additionally, output.text = output.text + text will append text to the existing text in output.text. This is unlike output.text = text which will overwrite whatever was already in output.text with the new text.
I'm trying to create a dictionary-like app using a dictionary with multiple key-value pairs.
The idea is when the user types and existing key in the textField and clicks the button the value of that key is displayed on the label. I could only get to display all the entries (including keys and values) when button is clicked (irrespective of the content of the textField), but that's not what I'm trying to achieve.
Here's my code:
//
// ViewController.swift
// Dictionary
//
// Created by Ralph on 7/20/15.
// Copyright (c) 2015 Ralph. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
let words = [
“doe” : “a deer, a female deer”,
“ray" : “a drop of golden sun”,
“me” : “a name I call myself”
]
#IBOutlet weak var textField: UITextField!
#IBAction func searchButton(sender: AnyObject) {
displayMeaning.text = words.description
}
#IBOutlet weak var displayMeaning: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You need to access the text in the textfield like so
displayMeaning.text = words[textField.text]
In your #IBAction you should try this:
#IBAction func searchButton(sender: UIButton) {
displayMeaning.text = words.objectForKey(textField.text)
}
I am just making a basic app that I can turn on and off switches to tell me if I have homework in a certain class throughout the day. The problem I am having is the reset button that changes all of the switches to off. How can I change a switch state in swift with a button? This is what I had so far:
//
// ViewController.swift
// HomeworkManager
//
// Created by Nate Parker on 9/2/14.
// Copyright (c) 2014 Nathan Parker. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func resetClicked(sender: AnyObject) {
}
#IBOutlet var spanish: UISwitch
#IBOutlet var algebra: UISwitch
#IBOutlet var amerCult: UISwitch
#IBOutlet var bio: UISwitch
#IBOutlet var col: UISwitch
}
It should just be a matter of using the setOn() method of the switches:
#IBAction func resetClicked(sender: AnyObject) {
spanish.setOn(false, animated: true);
algebra.setOn(false, animated: true);
amerCult.setOn(false, animated: true);
bio.setOn(false, animated: true);
col.setOn(false, animated: true);
}
I can't seem to write the if statement in the end to make the outs go up when the strikes hit 3?
can someone lease help and make it so in swift the outs will go up when someone hits 3 strikes
//
// ViewController.swift
// helloWordDemo
//
// Created by Developer on 6/8/14.
// Copyright (c) 2014 AECApps. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet var labelDispaly : UILabel = nil
// dispaly Strikes
var counter = 1
#IBAction func buttonPressed(sender : AnyObject) {
labelDispaly.text = "Strikes \(counter++)"
}
//button to add strikes
#IBOutlet var OutsDispaly : UILabel = nil
var outsCounter = 1
//outs dispaly
#IBAction func outsButtonPressed(sender : AnyObject) {
OutsDispaly.text = "Outs \(outsCounter++)"
}
//button to add outs
if counter = 3 {
outsCounter ++
}
}
Use a property observer:
var counter = 1 {
didSet {
if counter == 3 {
self.outsCounter++
}
}
}
Whenever counter gets changed, didSet will be called.
(Also note that the equality operator is ==. = is for assignment.)