Change textcolor for UITextField - xcode

I am trying to change the textColor.Though I enter the numbers correctly into the textfields,it is working only for "else" condition but not for 'if'. Here is my code.
Can anyone fix this issue. Thanks
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var questionOne: UILabel!
#IBOutlet weak var fieldOne: UITextField! // textFields should be set as labels
#IBOutlet weak var questionTwo: UILabel!
#IBOutlet weak var fieldTwo: UITextField!
#IBOutlet weak var questionThree: UILabel!
#IBOutlet weak var fieldThree: UITextField!
#IBOutlet weak var questionFour: UILabel!
#IBOutlet weak var fieldFour: UITextField!
#IBAction func submitButton(sender: UIButton) {
fieldOneColorChange()
fieldTwoColorChange()
fieldThreeColorChange()
fieldFourColorChange()
}
#IBAction func resetButton(sender: UIButton) {
}
// Text color changes
func fieldOneColorChange() {
if fieldOne == 1 {
questionOne.textColor = UIColor.redColor()
} else {
questionOne.textColor = UIColor.blueColor()
}
}
func fieldTwoColorChange() {
if fieldTwo == 2 {
questionTwo.textColor = UIColor.brownColor()
} else {
questionTwo.textColor = UIColor.redColor()
}
}
func fieldThreeColorChange(){
if fieldThree == 3 {
questionThree.textColor = UIColor.blueColor()
} else {
questionThree.textColor = UIColor.purpleColor()
}
}
func fieldFourColorChange(){
if fieldFour == 4 {
questionFour.textColor = UIColor.blueColor()
} else {
questionFour.textColor = UIColor.redColor()
}
}

Change your if statement like this
if fieldOne.text=="1"
so on for every if statement respectively

Related

Passing a value from inputField to NSTextField

I'd like to pass string value from one NSTextField to another NSTextField pressing a button. I used for this for-in loop. I need to pass a value from inputField to visibleText1, then to visibleText2 and then to visibleText3. But it doesn't work.
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
#IBOutlet weak var inputField: NSTextField!
#IBOutlet weak var visibleText1: NSTextField!
#IBOutlet weak var visibleText2: NSTextField!
#IBOutlet weak var visibleText3: NSTextField!
func applicationDidFinishLaunching(aNotification: NSNotification) { }
func applicationWillTerminate(aNotification: NSNotification) { }
#IBAction func applyButton(sender: AnyObject) {
for u in (visibleText1.stringValue...visibleText3.stringValue) {
visibleText.stringValue[u] = inputField.stringValue
inputField.stringValue = ""
}
}
}
Xcode gives me an error:
// Type 'ClosedInterval<String>' does not conform to protocol 'SequenceType'
How how to do it right?
No you can't do that because you can't create a range of string values of different text fields.
You could make an array of the three fields and enumerate that:
#IBAction func applyButton(sender: AnyObject) {
for field in [visibleText1, visibleText2, visibleText3] {
field.stringValue = inputField.stringValue
}
inputField.stringValue = ""
}
or with the forEach function
#IBAction func applyButton(sender: AnyObject) {
[visibleText1, visibleText2, visibleText3].forEach {
$0.stringValue = inputField.stringValue
}
inputField.stringValue = ""
}
Resetting the inputField in the repeat loop would always apply an empty string after the first iteration.
There are several things wrong with this, but I will start with what will work:
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
#IBOutlet weak var inputField: NSTextField!
#IBOutlet weak var visibleText1: NSTextField!
#IBOutlet weak var visibleText2: NSTextField!
#IBOutlet weak var visibleText3: NSTextField!
func applicationDidFinishLaunching(aNotification: NSNotification) { }
func applicationWillTerminate(aNotification: NSNotification) { }
#IBAction func applyButton(sender: AnyObject) {
for u in [visibleText1, visibleText2, visibleText3] {
u.stringValue = inputField.stringValue
}
}
}
So what's wrong with the original?
1) Your (visibleText1.stringValue...visibleText3.stringValue) is of type String ... String, which is not what you intended. You need to have an array of NSTextFields.
2) visibleText.stringValue[u] is not even a thing. There is no variable visibleString, and even if it was an NSTextField - which I think is what you want it to be, it's .stringValue is a String, and not an array.
3) What are you doing setting inputField.stringValue = "" inside the for loop? If your construct worked, only the first field would be set.
4) Not an error, but why are you doing all of this inside NSApplicationDelegate, rather than a viewController?

What's the correct syntax of passing an IBAction from a UICollectionViewCell to its host UIViewController?

Scenario: processing a button action originating from a UICollectionViewCell in its hosting UIViewController.
The Cell:
Here's my code:
protocol CellProtocol {
func doSomething()
}
...
class ItemCollectionCell:UICollectionViewCell {
var delegate:CellProtocol?
var itemData:ItemData?
#IBOutlet weak var descLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var imageView: UIImageView!
#IBAction func addToCartAction() {
delegate?.doSomething()
print("Add to Cart")
}
}
class ShopViewController:UIViewController, CellProtocol {
let kCellID = "ItemCell"
#IBOutlet weak var featuredItemLabel: UILabel!
#IBOutlet weak var featuredItemPriceLabel: UILabel!
#IBOutlet weak var featuredItemImageView: UIImageView!
#IBOutlet weak var collectionView: UICollectionView!
override var prefersStatusBarHidden: Bool {
return true
}
func doSomething() {
print("Howdy Batman!")
}
// -----------------------------------------------------------------------------------
// MARK: - Action Methods
#IBAction func addToCartAction(_ sender: UIButton) {
}
}
This isn't working.
I know it has something to do with delegation.
I merely sent 'self' to my custom UICollectionViewCell class during the cell-loading phase.

Optional() in my text view

For one of my static labels on my main story board, it prints out "Optional("United States"). However, I would like it to print out "United States". So my question is, how do I get rid of the "Optional" part? I've already tried doing:
if let p = placemarks!.first{
self.addressLabel.text = "\(p.country)"
}
I think the exclamation mark is supposed to "unwrap" some value right? However, even if I do p = placemarks!.first, it prints out "Optional("United States").
Below is the rest of my code just in case you would like some context:
//
// ViewController.swift
// Map Demo Rob 2
//
// Created by Jae Hyun Kim on 8/17/15.
// Copyright © 2015 Jae Hyun Kim. All rights reserved.
//
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var latitudeLabel: UILabel!
#IBOutlet weak var longitudeLabel: UILabel!
#IBOutlet weak var courseLabel: UILabel!
#IBOutlet weak var speedLabel: UILabel!
#IBOutlet weak var altitudeLabel: UILabel!
#IBOutlet weak var addressLabel: UILabel!
var manager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
// 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 locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
let userLocation: CLLocation = locations[0]
self.latitudeLabel.text = "\(userLocation.coordinate.latitude)"
self.longitudeLabel.text = "\(userLocation.coordinate.longitude)"
self.courseLabel.text = "\(userLocation.course)"
self.speedLabel.text = "\(userLocation.speed)"
self.altitudeLabel.text = "\(userLocation.altitude)"
CLGeocoder().reverseGeocodeLocation(userLocation, completionHandler: {(placemarks, error) -> Void in
print(userLocation)
if error != nil {
print(error)
return
}
else {
if let p = placemarks?.first{
self.addressLabel.text = "\(p.country)"
}
}
})
}
}
In
if let p = placemarks!.first{
self.addressLabel.text = "\(p.country)"
}
p.country is an Optional<String>. You need to unwrap that aswell in order to output only it's content (if it exists).
if let country = placemarks?.first?.country {
self.addressLabel.text = country
}

Images are not showing up when using a button

When I click run app, the app doesn't place images when I click on the designated buttons. (I am relatively new to Swift and Xcode so bear with me)
I am going to remove the UIImageViews and use buttons instead. I'll let you know if I need more help. Thank you.
class ViewController: UIViewController {
#IBOutlet var TicTacImg1: UIImageView!
#IBOutlet var TicTacImg2: UIImageView!
#IBOutlet var TicTacImg3: UIImageView!
#IBOutlet var TicTacImg4: UIImageView!
#IBOutlet var TicTacImg5: UIImageView!
#IBOutlet var TicTacImg6: UIImageView!
#IBOutlet var TicTacImg7: UIImageView!
#IBOutlet var TicTacImg8: UIImageView!
#IBOutlet var TicTacImg9: UIImageView!
#IBOutlet var TicTacButton1: UIButton!
#IBOutlet var TicTacButton2: UIButton!
#IBOutlet var TicTacButton3: UIButton!
#IBOutlet var TicTacButton4: UIButton!
#IBOutlet var TicTacButton5: UIButton!
#IBOutlet var TicTacButton6: UIButton!
#IBOutlet var TicTacButton7: UIButton!
#IBOutlet var TicTacButton8: UIButton!
#IBOutlet var TicTacButton9: UIButton!
#IBOutlet var ResetButton: UIButton!
#IBOutlet var UserMessage: UILabel!
var plays = Dictionary <Int, Int> ()
#IBAction func UIButtonClicked(sender:UIButton) {
UserMessage.hidden = true
if plays[sender.tag] == nil && !aiDeciding && !done {
setImageForSpot(sender.tag, player:1)
}
}
func setImageForSpot (spot:Int, player:Int) {
var playerMark = player == 1 ? "x" : "o"
plays[spot] = player
switch spot {
case 1:
TicTacImg1.image = UIImage(named: playerMark)
case 2:
TicTacImg2.image = UIImage(named: playerMark)
case 3:
TicTacImg3.image = UIImage(named: playerMark)
case 4:
TicTacImg4.image = UIImage(named: playerMark)
case 5:
TicTacImg5.image = UIImage(named: playerMark)
case 6:
TicTacImg6.image = UIImage(named: playerMark)
case 7:
TicTacImg7.image = UIImage(named: playerMark)
case 8:
TicTacImg8.image = UIImage(named: playerMark)
case 9:
TicTacImg9.image = UIImage(named: playerMark)
default:
TicTacImg5.image = UIImage(named: playerMark)
}
}
Your UIButtonClicked function needs to look like this...
#IBAction func UIButtonClicked(sender:UIButton) {
ticTacLabel.hidden = true
if plays[sender.tag] == nil && !aiDeciding && !done {
setImageForSpot(sender.tag, player:1)
}
Hope this helps!

Hitting Return to save Text Input

I have a text field on my xcode project, how can I do to save the user input just by hitting return on the keyboard, instead of using a button to save it?
ViewController with the new code:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textFieldInput: UITextField!
#IBOutlet weak var iphoneSaveCharName: UIButton!
#IBOutlet weak var charOne: UIButton!
#IBOutlet weak var charTwo: UIButton!
#IBOutlet weak var charThree: UIButton!
#IBOutlet weak var charFour: 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 iphoneSaveTextInput(sender: UIButton) -> Void{
//
// let textData = textFieldInput.text
// textFieldInput.hidden = true
// iphoneSaveCharName.hidden = true
// }
var savedText: String!
func textFieldShouldReturn(textFieldInput: UITextField) -> Bool {
savedText = textFieldInput.text
println(savedText)
textFieldInput.resignFirstResponder()
return false
}
#IBAction func editText(sender: AnyObject) {
if sender is UILongPressGestureRecognizer &&
sender.state == UIGestureRecognizerState.Began {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
let button = sender.view as UIButton
if button.tag == 1{
charOne.setTitle(savedText, forState: .Normal)
} else if button.tag == 2{
charTwo.setTitle(savedText, forState: .Normal)
} else if button.tag == 3{
charThree.setTitle(savedText, forState: .Normal)
} else if button.tag == 4{
charFour.setTitle(savedText, forState: .Normal)
}
}
}
}
The editText function is a long press gesture recognizer that checks what button is being long pressed.
Set the view controller as a text field delegate and add the appropriate delegate method.
class viewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
var savedText: String!
func textFieldShouldReturn(textField: UITextField) -> Bool {
savedText = textField.text
textField.resignFirstResponder()
return false
}
}

Resources