Why isn't this code in ViewDidLoad executing? - swift

I have two text fields, one button, and four labels.
When you push the button, it should update the four labels.
Label 1: name
Label 2: email
Label 3: name (same data as label 1)
Label 4: email (same data as label 2)
I was experimenting under viewDidLoad() to see if I can make another var and update my last label. No error when I run the code, but it turns out "empty" in the simulator.
Can anyone explain why?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var emailInput: UITextField!
#IBOutlet weak var nameInput: UITextField!
#IBOutlet weak var label: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
#IBOutlet weak var label4: UILabel!
#IBAction func actionButton(_ sender: Any) {
var text = nameInput.text
var email = emailInput.text
label.text = text
label2.text = email
label3.text = text
}
//where i experimented:
override func viewDidLoad() {
super.viewDidLoad()
var email2 = emailInput.text
label4.text = email2
}
}

Change
label4.text = email2
To
label4.text = "howdy"
Now you will see something in your interface.

The viewDidLoad method called in the beginning perfectly. But the label not showing any data because initially the textfield has no value. That's the label not contains any data.
Please set any value to the label initially. Like
label4.text = "Something"
Hope you can understand that the viewDidLoad is called perfectly.

Related

The picture goes up because the description is long how can I fix this?

When the picture description is long, my picture disappears, I can't see it. How can I show full size of text and full size of images?
How can I scroll the text up and down?
FavoriteDetailViewController
import UIKit
import ProgressHUD
class FavoriteDetailViewController: UIViewController {
#IBOutlet weak var shortdescriptionLabel: UILabel!
#IBOutlet weak var favoriteImageView: UIImageView!
#IBOutlet weak var favoriteTitleLabel: UILabel!
#IBOutlet weak var descriptionLabel: UILabel!
#IBOutlet weak var nameField: UITextField!
var breed: Breed!
override func viewDidLoad() {
super.viewDidLoad()
populateView()
}
private func populateView() {
favoriteImageView.kf.setImage(with: breed.image?.asUrl)
favoriteTitleLabel.text = breed.name
descriptionLabel.text = breed.description
shortdescriptionLabel.text = breed.shortDescription
}
#IBAction func favoriteButton(_ sender: UIButton) {
guard let name = nameField.text?.trimmingCharacters(in: .whitespaces),
!name.isEmpty else {
ProgressHUD.showError("Please enter your name")
return
}
print("Hello \(name)")
}
}
I made the text smaller but it's very illegible
I dropped the spacing, it looks bad too
I want to make the whole text look nice and not spoil the picture. What is the solution? Is it possible to move the label up and down?

Swift 3 OSX app textfield and pop button to the clipboard

I'm new with Swift, but what I would like to do basically is
now what the user selected for Q1 : Q 4,
if for example the user selected Yes at Q1, no to Q2 then I would like to add a string to the clipboard :
Q1 : Question one is there : Yes
Q2 : Question two is here : No
and last thing from the button "done"
I would like to added to the previous clipboard and copy what ever the text is on the first textfield and put all together into the clipboard
// Case Questions
#IBOutlet weak var qUestionOne: NSPopUpButton!
#IBOutlet weak var qUestionTwo: NSPopUpButton!
#IBOutlet weak var qUestionTree: NSPopUpButton!
#IBOutlet weak var qUestionFor: NSPopUpButton!
// CaseNote
#IBOutlet weak var cAseNoteTextField: NSTextField!
// Draft
#IBOutlet weak var dRaftNote: NSTextField!
// buttons
#IBAction func dOneButton(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}

UITextfield value for each indexpath.row

My uitableview consists of 10 cells and each cell has 2 uitextfield's.
I need to take values from each uitextfield in each cell and add it to an array
// my custom cell
class MatchTableViewCell: UITableViewCell, UITextFieldDelegate {
#IBOutlet weak var Team2Score: UITextField!
#IBOutlet weak var Team1Score: UITextField!
#IBOutlet weak var Team2: UILabel!
#IBOutlet weak var Image2: UIImageView!
#IBOutlet weak var Image1: UIImageView!
#IBOutlet weak var Team1: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Make sure in the cell initializer for tableviewcontroller that your using your custom cell name and that you've set your identifier correctly. Then just initalize a cell and say something to the effect of
cell.Team2Score.text = "100"
you will need a global array to hold the strings. So..
var wordArray: [String] = []
you will need to add a button within the cell so when you hit the button it will add (append in the input of string (letters) to the array.
You need to add a tag to the button to know which cell it was clicked in.
button.tag = indexPath.row
#IBaction button(sender: UIButton){
var team2Words = Team2Score.text
wordArray.append(team2Words)
// do the samething for Team1Score
// you can use and if statement to check if one or the other one is empty
}
This will give you a lead

SWIFT: UIImageView Photo Going Past Boundaries

Can anybody tell me why my photos go beyond the boundary of UIImageView?
The pictures end up covering the buttons below it.
My code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var Photo: UIImageView!
#IBOutlet var QuestionLabel: UILabel!
#IBOutlet var Button1: UIButton!
#IBOutlet var Button2: UIButton!
#IBOutlet var Button3: UIButton!
#IBOutlet var Button4: UIButton!
#IBOutlet var LabelEnd: UILabel!
var CorrectAnswer = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Hide()
Photo.layer.cornerRadius = self.Photo.frame.size.width / 10
Photo.clipsToBounds = true
Photo.layer.masksToBounds = true
Photo.layer.borderWidth = 1
Photo.layer.borderColor = UIColor.blackColor().CGColor
Photo.contentMode = .ScaleAspectFit
Photo.contentMode = .ScaleAspectFill
RandomQuestions()
}
I apologize for being such a novice, just learning how to code.
Are you sure it's going beyond the boundary of your image view?
Try printing out the frame of your view to double check. Doesn't look like you have constraints set up from that screenshot - maybe your image view is sized to fit.

Update labels from unwind segue

I'm trying to change the labels of a viewController after a certain action is taken from a modal view which triggers an unwind segue.
Once unwind segue happens the labels of the current view (the one which the modal was covering) should be changed.
My current attempt at doing this is resulting in a "unexpectedly found nil while unwrapping an Optional value" error. Here is the code:
class DataViewController: UIViewController {
var experiment: NSDictionary?
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var bodyLabel: UILabel!
#IBOutlet weak var tlRightLine: UIImageView!
#IBOutlet weak var tlLeftLine: UIImageView!
#IBOutlet weak var brRightLine: UIImageView!
#IBOutlet weak var brLeftLine: UIImageView!
#IBOutlet weak var bodyTest: UITextView!
#IBAction func removeExperimentSegue(unwindSegue:UIStoryboardSegue) {
removeExperiment = true
titleLabel.text = "Done"
bodyLabel.text = "Done"
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let dict: NSDictionary = experiment {
if let title = dict.objectForKey("title") as? String {
self.titleLabel!.text = title
}
if let body = dict.objectForKey("body") as? String {
self.bodyTest!.text = body
}
} else {
self.titleLabel!.text = ""
self.bodyLabel!.text = ""
}
}
}
What am I doing wrong?
I did! I ended up using a global boolean variable. I set it true on the initial load of the menu and then had it flip to false during the unwind segue.
var removeExperiment = false
class DataViewController: UIViewController {
#IBAction func removeExperimentSegue(unwindSegue:UIStoryboardSegue) {
removeExperiment = true
}
if removeExperiment == true {
doneLabel.text = "You've completed this experiment. You won't see it again unless you hit 'Reset' from the Home menu."
}
}
Hope that helps!