How do I change the text of a label with a segmented controller that has three segments> - swift

For class I have a segmented controller that has three segments.
First segment: make label say Hello
Second segment make label say goodbye
Third segment make label say you rock.
I also need to add a name entered by a text field and when a button is pushed it goes into the label either starting with Hello, Goodbye, or You Rock.The picture is showing what I have so far.
second picture shows what I need to do

You can get the title of the selected segment and insert it into the name label text.
class ViewController: UIViewController {
// MARK: Outlets
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var greetingControl: UISegmentedControl!
// MARK: Properties
var name: String? {
return nameTextField.text
}
var greeting: String? {
return greetingControl.titleForSegment(
at: greetingControl.selectedSegmentIndex
)
}
// MARK: Methods
private func updateOutlets() {
nameLabel.text = "\(greeting!), \(name!)!"
}
// MARK: Actions
#IBAction func changeNameOnTap(_ sender: UIButton) {
updateOutlets()
}
#IBAction func nameController(_ sender: UISegmentedControl) {
updateOutlets()
}
}

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?

printing percentage value into decimal swift

The Result I wantedi have three buttons under same IBAction showing different percentages %0, %10, %20.
and I have another button called calculate in another IBAction. so what I want is. when I choose %10 and press calculate I want to print out the 0.1 in the console. this is very beginner question but I m really stuck here
below is my code
import UIKit
class CalculatorViewController: UIViewController {
#IBOutlet weak var billTextField: UITextField!
#IBOutlet weak var zeroPctButton: UIButton!
#IBOutlet weak var tenPctButton: UIButton!
#IBOutlet weak var twentyPctButton: UIButton!
#IBOutlet weak var splitNumberLabel: UILabel!
#IBAction func tipChanged(_ sender: UIButton) {
zeroPctButton.isSelected = false
tenPctButton.isSelected = false
twentyPctButton.isSelected = false
sender.isSelected = true
}
#IBAction func stepperValueChanged(_ sender: Any) {
}
#IBAction func calculatePressed(_ sender: UIButton) {
}
}
The easiest way is to add the output on the percentage button tapped, e.g. in the #IBAction of the "10%" button:
print("0.1")
If you really need to output on the Calculate button tap, you can first determine which percantage button is selected:
if zeroPctButton.isSelected {
print("0.0")
} else if tenPctButton.isSelected {
// ...
If you don't want to hardcode the output, you need some data to base on. Since you don't show us any model code, we can only rely on the text on the buttons. So, the solution might look something like this (inside the Calculate button's #IBAction):
[zeroPctButton,
tenPctButton,
twentyPctButton]
.filter { $0.isSelected }
.forEach {
print("\(Double(($0.titleLabel.text ?? "").filter { ("0"..."9").contains($0) }) / 100.0)")
}
(It takes the text of the selected percentage buttons, filters out only digits from it, converts the resulting digit string to Double, and divides the result by 100.0.)

Is it possible to modify the properties of a subclass from a parent class in Swift 4?

Via a method or closure, perhaps?
I created a subclass view controller of my superclass/parent view controller and placed labels with placeholder text in that subclass view controller.
I want to set the labels' values to blank strings from the superclass/parent view controller, or, specifically, from an IBAction function that causes the subclass view controller to appear.
Here is the code, first from the parent class, then from the subclass...
'''
class ViewController: UIViewController {
#IBAction func leavingView(){
self.EntryViewController.entryDateLabel.text = ""
self.EntryViewController.entryLabel.text = ""
self.dismiss(animated: true, completion: nil)
}
'''
then from the subclass...
'''
class EntryViewController: ViewController {
#IBOutlet var entryDateLabel: UILabel!
#IBOutlet var entryLabel: UILabel!
}
'''
I have come up with 2 solutions to this problem, without having the parent view controller know about its subclass.
In the first example the parent sets properties on itself that the child listens to (via the didSet method, it then updates its view accordingly. However, this isn't ideal because the entryDate and entry string fields are useless on their own, almost redundant in the parent.
class ParentViewController: UIViewController {
var entryDate: String?
var entry: String?
#IBAction func leavingView(){
self.entryDate = ""
self.entry = ""
self.dismiss(animated: true, completion: nil)
}
}
class ChildViewController: ParentViewController {
#IBOutlet var entryDateLabel: UILabel!
#IBOutlet var entryLabel: UILabel!
override var entryDate: String? {
didSet {
guard isViewLoaded else {
return
}
entryDateLabel.text = entryDate
}
}
override var entry: String? {
didSet {
guard isViewLoaded else {
return
}
entryLabel.text = entry
}
}
}
In my opinion, the second solution is clearer and keeps implementation details more separate because you're using instructions or events to notify the child view controllers.
class ParentViewController: UIViewController {
#IBAction func leavingView(){
self.dismiss(animated: true, completion: didLeaveView)
}
func didLeaveView() { }
}
class ChildViewController: ParentViewController {
#IBOutlet var entryDateLabel: UILabel!
#IBOutlet var entryLabel: UILabel!
override func didLeaveView() {
entryDateLabel.text = ""
entryLabel.text = ""
}
}
Since your requirement is not that much clear I have created a demo for you and into that demo I have added child ContainerViewController into parent ViewController and from that parent view controller you can change UILabel text when you click on UIButton of parent ViewController and code will be for ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btnFromParentViewTapped(_ sender: Any) {
//Here get the child of your parent view controller
if let containerView = self.children[0] as? ContainerViewController {
containerView.lblContainer.text = ""
}
}
}
and ContainerViewController code will be:
class ContainerViewController: UIViewController {
#IBOutlet weak var lblContainer: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Don't need to add much here because you are accessing it from parent view.
And your result will be:
As you can see when I click on button which title says Change Container label text the label text from ContainerViewController set to empty string.
For more info check THIS demo project.

OS X Hello World Swift tutorial

So I'm following this tutorial for Swift and am not getting the desired result at the end. This is my code for the ViewController:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var nameField: NSTextField!
#IBOutlet weak var helloLabel: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
#IBAction func sayButtonClicked(_ sender: Any) {
var name = nameField.stringValue
if name.isEmpty {
name = "World"
}
let greeting = "Hello \(name)!"
helloLabel.stringValue = greeting
}
}
And the following is the result:
When I debug the program however it does show the correct variables:
What am I doing wrong here?
Turns out I didn't stretch out the the Label field enough.

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