ProgressBar only shows the progress when the loop ends.
How do in this example for the bar to be updated in real time?
#IBAction func btnStart(sender: AnyObject) {
for var xx:Float = 0 ; xx<=1.0; xx=xx+0.00001 {
progressView.setProgress(xx, animated: false)
}
}
#IBOutlet weak var progressLabel: UILabel!
#IBOutlet weak var progressView: UIProgressView!
UI only updates in the main loop and at the end of the current scope,
so try this
#IBAction func btnStart(sender: AnyObject) {
NSThread.detachNewThreadSelector("process", toTarget: self, withObject: nil)
}
func process() {
for var xx:Float = 0 ; xx<=1.0; xx=xx+0.00001 {
dispatch_async(dispatch_get_main_queue(), {
progressView.setProgress(xx, animated: false)
})
}
}
#IBOutlet weak var progressLabel: UILabel!
#IBOutlet weak var progressView: UIProgressView!
Related
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var triviaLabel: UILabel!
#IBOutlet weak var trueButton: UIButton!
#IBOutlet weak var falseButton: UIButton!
#IBOutlet weak var progressBar: UIProgressView!
var quiz = [Question(text: "Blah blah", answer: "True"),
Question(text: "Ha ha", answer: "True"),Question(text: "Bruh bruh", answer: "False")]
var questionNumber = 0
override func viewDidLoad() {
super.viewDidLoad()
updateQuestion()
}
#IBAction func answerButtonPressed(_ sender: UIButton) {
let userAnswer = sender.currentTitle
let actualAnswer = quiz[questionNumber].answer
if userAnswer == actualAnswer {
} else {
}
if questionNumber < quiz.count-1 {
questionNumber += 1
}else {
questionNumber = 0
}
updateQuestion()
}
func updateQuestion() {
triviaLabel.text = quiz[questionNumber].text // Get the error here
updateQuestion()
}
}
The build succeeds and the app launches but there comes a blank screen and the error occurs. I tried mapping the label again with the storyboard. I speculate that there is some problem with the label declaration or connection.
It takes data in my #Published variable in CarDetailViewModel, but I could not pass the data inside this variable to the variable in the ViewController.
ViewModel:
class CarDetailViewModel: ObservableObject {
#Published var carDetail: Car = Car(brand: "", features: CarFeatures(model: "", km: 0, year: 0, price: 0, image: "", gearType: "", fuelType: "", carVersion: ""))
private var anyCancellable = Set<AnyCancellable>()
var carSubject = PassthroughSubject<Car, Never>()
func prepareCarDetail() {
carSubject
.sink {[weak self] car in
self?.carDetail = car
print("car: \(self?.carDetail)") // it is working.
}
.store(in: &anyCancellable)
}
}
ViewController:
When the assignmentDataToUI function runs, the label on the screen is blank.
class CarDetailViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var brandLabel: UILabel!
#IBOutlet weak var modelLabel: UILabel!
#IBOutlet weak var gearTypeLabel: UILabel!
#IBOutlet weak var fuelTypeLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
var carDetailViewModel = CarDetailViewModel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
carDetailViewModel.prepareCarDetail()
assignmentDataToUI()
}
func assignmentDataToUI() {
brandLabel.text = carDetailViewModel.carDetail.brand
}
}
I listened to carDetail in ViewModel with sink.
carDetailViewModel.$carDetail
.sink {[weak self] car in
guard let self = self else { return }
self.assignmentDataToUI()
}
.store(in: &anyCancelable)
I'm new to building an application but learning. I am trying to take value from the alert and save it to the label. This is to create a simple timetable to for the Week. I have two difficulties one is getting value back from the function to display it to my label which is placed behind the Button pressed on the Main.Sotryboard.
I am trying to get my value back here to assign to the Label MonB
#IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
//Trying to get my text input back here.
I am trying to get the value from the function below:
func FoodItemAdded(Food: String)
{
//return self.
//Trying to return the value from here.
}
I also looked around but don't know how to save these label values so When i close the app it still remains saved as persistent.
This is the complete Code.
import UIKit
class MenuViewController: UIViewController {
//Define all the Output Labels
#IBOutlet weak var MonB: UILabel!
#IBOutlet weak var MonL: UILabel!
#IBOutlet weak var MonD: UILabel!
#IBOutlet weak var TueB: UILabel!
#IBOutlet weak var TueL: UILabel!
#IBOutlet weak var TueD: UILabel!
#IBOutlet weak var WedB: UILabel!
#IBOutlet weak var WedL: UILabel!
#IBOutlet weak var WedD: UILabel!
#IBOutlet weak var ThuB: UILabel!
#IBOutlet weak var ThuL: UILabel!
#IBOutlet weak var ThuD: UILabel!
#IBOutlet weak var FriB: UILabel!
#IBOutlet weak var FriL: UILabel!
#IBOutlet weak var FriD: UILabel!
#IBOutlet weak var SatB: UILabel!
#IBOutlet weak var SatL: UILabel!
#IBOutlet weak var SatD: UILabel!
#IBOutlet weak var SunB: UILabel!
#IBOutlet weak var SunL: UILabel!
#IBOutlet weak var SunD: UILabel!
//Define Buttons to Enable/Disable
#IBOutlet weak var MondB: UIButton!
#IBOutlet weak var MondL: UIButton!
#IBOutlet weak var MondD: UIButton!
#IBOutlet weak var TuesB: UIButton!
#IBOutlet weak var TuesL: UIButton!
#IBOutlet weak var TuesD: UIButton!
#IBOutlet weak var WedsB: UIButton!
#IBOutlet weak var WedsL: UIButton!
#IBOutlet weak var WedsD: UIButton!
#IBOutlet weak var ThurB: UIButton!
#IBOutlet weak var ThurL: UIButton!
#IBOutlet weak var ThurD: UIButton!
#IBOutlet weak var FridB: UIButton!
#IBOutlet weak var FridL: UIButton!
#IBOutlet weak var FridD: UIButton!
#IBOutlet weak var SatuB: UIButton!
#IBOutlet weak var SatuL: UIButton!
#IBOutlet weak var SatuD: UIButton!
#IBOutlet weak var SundB: UIButton!
#IBOutlet weak var SundL: UIButton!
#IBOutlet weak var SundD: UIButton!
//Define the Edit button to enable Tap Buttons
#IBOutlet var editButton: UIBarButtonItem!
//Define all the Output Buttons
#IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func MoL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func MoD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuD(_ sender: UIButton) {
ButtonTapped(sender)
}
//VIEWDIDLOAD Parameters
override func viewDidLoad() {
super.viewDidLoad()
//Hide the Navigation Bar on the screen
self.navigationController?.isNavigationBarHidden = false
//Define the Title for the page
self.title = "~~~Weekly Menu~~~"
//Enable the buttons to edit
self.navigationItem.rightBarButtonItem = self.editButton
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Disable UIButton by default
self.MondB.isEnabled = false
self.MondL.isEnabled = false
self.MondD.isEnabled = false
self.TuesB.isEnabled = false
self.TuesL.isEnabled = false
self.TuesD.isEnabled = false
self.WedsB.isEnabled = false
self.WedsL.isEnabled = false
self.WedsD.isEnabled = false
self.ThurB.isEnabled = false
self.ThurL.isEnabled = false
self.ThurD.isEnabled = false
self.FridB.isEnabled = false
self.FridL.isEnabled = false
self.FridD.isEnabled = false
self.SatuB.isEnabled = false
self.SatuL.isEnabled = false
self.SatuD.isEnabled = false
self.SundB.isEnabled = false
self.SundL.isEnabled = false
self.SundD.isEnabled = false
}
#IBAction func editEnable(_ sender: Any) {
//self.MonB.isHidden = false
// Alert stating Changes can be made
let alert = UIAlertController(title: "Message for Cook",
message: "Time to change the Menu
for Next Week",
preferredStyle:
UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style:
UIAlertAction.Style.default))
present(alert, animated: true, completion: {
//Enable Button capabilities
self.MondB.isEnabled = true
self.MondL.isEnabled = true
self.MondD.isEnabled = true
self.TuesB.isEnabled = true
self.TuesL.isEnabled = true
self.TuesD.isEnabled = true
self.WedsB.isEnabled = true
self.WedsL.isEnabled = true
self.WedsD.isEnabled = true
self.ThurB.isEnabled = true
self.ThurL.isEnabled = true
self.ThurD.isEnabled = true
self.FridB.isEnabled = true
self.FridL.isEnabled = true
self.FridD.isEnabled = true
self.SatuB.isEnabled = true
self.SatuL.isEnabled = true
self.SatuD.isEnabled = true
self.SundB.isEnabled = true
self.SundL.isEnabled = true
self.SundD.isEnabled = true
})
}
#objc func ButtonTapped(_ sender: UIButton)
{
// Create an alert
let alert = UIAlertController(
title: "What to cook?",
message: "Add The Name Of The Dish:",
preferredStyle: .alert)
// Add a text field to the alert for the new item's title
alert.addTextField(configurationHandler: nil)
// Add a "cancel" button to the alert. This one doesn't need a handler
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
// Add a "OK" button to the alert. The handler calls addNewToDoItem()
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
if let Dish = alert.textFields?[0].text
{
self.FoodItemAdded(Food: Dish)
}
}))
// Present the alert to the user
self.present(alert, animated: true, completion: nil)
}
func FoodItemAdded(Food: String)
{
//return self.
//Trying to return the value from here.
}
//Saving the data
}
Write your function similar to this
func FoodItemAdded(Food: String)->String
{
//setting the parameter value to a variable
let string = Food
//returning new string type variable
return string
}
You can set the return value to a label like this,
self.mylabel.text = FoodItemAdded("Burger")
To store data, you can use core data/ SQLITE.
Coredata: https://youtu.be/tP4OGvIRUC4
Sqlite: https://youtu.be/c4wLS9py1rU
This is how I solved the problem:
import UIKit
import CoreData
#available(iOS 10.0, *)
class MenuViewController: UIViewController {
//Define all the Output Labels
#IBOutlet weak var MonB: UILabel!
#IBOutlet weak var MonL: UILabel!
#IBOutlet weak var MonD: UILabel!
#IBOutlet weak var TueB: UILabel!
#IBOutlet weak var TueL: UILabel!
#IBOutlet weak var TueD: UILabel!
#IBOutlet weak var WedB: UILabel!
#IBOutlet weak var WedL: UILabel!
#IBOutlet weak var WedD: UILabel!
#IBOutlet weak var ThuB: UILabel!
#IBOutlet weak var ThuL: UILabel!
#IBOutlet weak var ThuD: UILabel!
#IBOutlet weak var FriB: UILabel!
#IBOutlet weak var FriL: UILabel!
#IBOutlet weak var FriD: UILabel!
#IBOutlet weak var SatB: UILabel!
#IBOutlet weak var SatL: UILabel!
#IBOutlet weak var SatD: UILabel!
#IBOutlet weak var SunB: UILabel!
#IBOutlet weak var SunL: UILabel!
#IBOutlet weak var SunD: UILabel!
//Define Buttons to Enable/Disable
#IBOutlet weak var MondB: UIButton!
#IBOutlet weak var MondL: UIButton!
#IBOutlet weak var MondD: UIButton!
#IBOutlet weak var TuesB: UIButton!
#IBOutlet weak var TuesL: UIButton!
#IBOutlet weak var TuesD: UIButton!
#IBOutlet weak var WedsB: UIButton!
#IBOutlet weak var WedsL: UIButton!
#IBOutlet weak var WedsD: UIButton!
#IBOutlet weak var ThurB: UIButton!
#IBOutlet weak var ThurL: UIButton!
#IBOutlet weak var ThurD: UIButton!
#IBOutlet weak var FridB: UIButton!
#IBOutlet weak var FridL: UIButton!
#IBOutlet weak var FridD: UIButton!
#IBOutlet weak var SatuB: UIButton!
#IBOutlet weak var SatuL: UIButton!
#IBOutlet weak var SatuD: UIButton!
#IBOutlet weak var SundB: UIButton!
#IBOutlet weak var SundL: UIButton!
#IBOutlet weak var SundD: UIButton!
//Define the Edit button to enable Tap Buttons
#IBOutlet var editButton: UIBarButtonItem!
//Define all the Output Buttons
#IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func MoL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func MoD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuD(_ sender: UIButton) {
ButtonTapped(sender)
}
//VIEWDIDLOAD Parameters
override func viewDidLoad() {
super.viewDidLoad()
//Hide the Navigation Bar on the screen
self.navigationController?.isNavigationBarHidden = false
//Define the Title for the page
self.title = "~~~Weekly Menu~~~"
//Enable the buttons to edit
self.navigationItem.rightBarButtonItem = self.editButton
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Disable UIButton by default
self.MondB.isEnabled = false
self.MondL.isEnabled = false
self.MondD.isEnabled = false
self.TuesB.isEnabled = false
self.TuesL.isEnabled = false
self.TuesD.isEnabled = false
self.WedsB.isEnabled = false
self.WedsL.isEnabled = false
self.WedsD.isEnabled = false
self.ThurB.isEnabled = false
self.ThurL.isEnabled = false
self.ThurD.isEnabled = false
self.FridB.isEnabled = false
self.FridL.isEnabled = false
self.FridD.isEnabled = false
self.SatuB.isEnabled = false
self.SatuL.isEnabled = false
self.SatuD.isEnabled = false
self.SundB.isEnabled = false
self.SundL.isEnabled = false
self.SundD.isEnabled = false
}
#IBAction func editEnable(_ sender: Any) {
// Alert stating Changes can be made
let alert = UIAlertController(title: "Message for Cook",
message: "Time to change the Menu for Next Week",
preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default))
present(alert, animated: true, completion: {
//Enable Button capabilities
self.MondB.isEnabled = true
self.MondL.isEnabled = true
self.MondD.isEnabled = true
self.TuesB.isEnabled = true
self.TuesL.isEnabled = true
self.TuesD.isEnabled = true
self.WedsB.isEnabled = true
self.WedsL.isEnabled = true
self.WedsD.isEnabled = true
self.ThurB.isEnabled = true
self.ThurL.isEnabled = true
self.ThurD.isEnabled = true
self.FridB.isEnabled = true
self.FridL.isEnabled = true
self.FridD.isEnabled = true
self.SatuB.isEnabled = true
self.SatuL.isEnabled = true
self.SatuD.isEnabled = true
self.SundB.isEnabled = true
self.SundL.isEnabled = true
self.SundD.isEnabled = true
})
}
#objc func ButtonTapped(_ sender: UIButton)
{
// Create an alert
let alert = UIAlertController(
title: "What to cook?",
message: "Add The Name Of The Dish:",
preferredStyle: .alert)
// Add a text field to the alert for the new item's title
alert.addTextField(configurationHandler: nil)
// Add a "cancel" button to the alert. This one doesn't need a handler
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
// Add a "OK" button to the alert. The handler calls addNewToDoItem()
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
if let Dish = alert.textFields?[0].text {
if sender == self.MondB {
self.MonB.text = Dish
}
else if sender == self.MondL
{
self.MonL.text = Dish
}
else if sender == self.MondD
{
self.MonD.text = Dish
}
else if sender == self.TuesB
{
self.TueB.text = Dish
}
else if sender == self.TuesL
{
self.TueL.text = Dish
}
else if sender == self.TuesD
{
self.TueD.text = Dish
}
else if sender == self.WedsB
{
self.WedB.text = Dish
}
else if sender == self.WedsL
{
self.WedL.text = Dish
}
else if sender == self.WedsD
{
self.WedD.text = Dish
}
else if sender == self.ThurB
{
self.ThuB.text = Dish
}
else if sender == self.ThurL
{
self.ThuL.text = Dish
}
else if sender == self.ThurD
{
self.ThuD.text = Dish
}
else if sender == self.FridB
{
self.FriB.text = Dish
}
else if sender == self.FridL
{
self.FriL.text = Dish
}
else if sender == self.FridD
{
self.FriD.text = Dish
}
else if sender == self.SatuB
{
self.SatB.text = Dish
}
else if sender == self.SatuL
{
self.SatL.text = Dish
}
else if sender == self.SatuD
{
self.SatD.text = Dish
}
else if sender == self.SundB
{
self.SunB.text = Dish
}
else if sender == self.SundL
{
self.SunL.text = Dish
}
else if sender == self.SundD
{
self.SunD.text = Dish
}
}}))
// Present the alert to the user
self.present(alert, animated: true, completion: nil)
}
I'm trying to make an app to count different tokens in a board game. I thought it would be easy peasy, but not quite.
I succeeded in adding one UIStepper to count one kind of token, but I don't know how to add the other four. This is what I've got so far:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var theLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func StepperTapped(sender: UIStepper) {
self.theLabel.text = "\(self.stepper.value)"
}
}
I got one action and two outlets.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var theLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBOutlet weak var theLabel2: UILabel!
#IBOutlet weak var stepper2: UIStepper!
#IBOutlet weak var theLabel3: UILabel!
#IBOutlet weak var stepper3: UIStepper!
#IBOutlet weak var theLabel4: UILabel!
#IBOutlet weak var stepper4: UIStepper!
#IBOutlet weak var theLabel5: UILabel!
#IBOutlet weak var stepper5: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func StepperTapped(sender: UIStepper) {
self.theLabel.text = "\(self.stepper.value)"
}
#IBAction func StepperTapped2(sender: UIStepper) {
self.theLabel2.text = "\(self.stepper2.value)"
}
#IBAction func StepperTapped3(sender: UIStepper) {
self.theLabel3.text = "\(self.stepper3.value)"
}
#IBAction func StepperTapped4(sender: UIStepper) {
self.theLabel4.text = "\(self.stepper4.value)"
}
#IBAction func StepperTapped5(sender: UIStepper) {
self.theLabel5.text = "\(self.stepper5.value)"
}
}
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!