Update labels from unwind segue - swift

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!

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?

Pass data to collectionViewCell made lagging scroll, How to make it more smooth? - Found issue

According my question, I have to pass a data to my collection view cell
It made laggy when scroll. It happened because my setup data function, it pass too big of data but I have no idea how to fix it.
My cell class .
class VerificationStepCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var stepLabel: UILabel!
#IBOutlet weak var stepTitleLabel: UILabel!
#IBOutlet weak var stepNoLabel: UILabel!
#IBOutlet weak var iconImageView: UIImageView!
#IBOutlet weak var stepDescriptionLabel: UILabel!
#IBOutlet weak var stepStatusLabel: UILabel!
#IBOutlet weak var stepStatusView: UIView!
#IBOutlet weak var buttonStatus: BaseButton!
var currentStepType: VerifyAccountStepType?
var currentStatus: UserLevelInfoStatus?
var data: HomeModels.ViewModel.VerifyAccountStepModel? = nil
override func awakeFromNib() {
super.awakeFromNib()
self.setupView()
}
private func setupView() {
self.stepStatusView.layer.cornerRadius = self.stepStatusView.frame.height / 2
}
func setupData() {
guard let data = self.data else { return }
self.currentStepType = data.stepType
self.currentStatus = data.stepStatus
self.stepStatusView.isHidden = data.stepStatus == .notVerify
self.stepLabel.text = "Step\(data.stepNo)"
self.stepTitleLabel.text = data.stepTitle
self.stepNoLabel.text = "\(data.stepNo)"
self.iconImageView.image = data.stepIconImage
self.stepDescriptionLabel.text = data.stepDescription
self.stepStatusLabel.text = "home_unverified_account_\(data.stepStatus.rawValue)_status".localized()
switch data.stepStatus {
case .approve:
self.buttonStatus.setIsEnable(false)
self.buttonStatus.setTitle(data.stepButtonTitle, for: .normal)
self.stepStatusView.backgroundColor = UIColor.Yellows.brighterYellow
self.stepStatusLabel.textColor = .black
case .notVerify:
self.buttonStatus.setIsEnable(true)
self.buttonStatus.setTitle(data.stepButtonTitle, for: .normal)
self.stepStatusLabel.textColor = .white
case .reject:
self.buttonStatus.setIsEnable(true)
self.buttonStatus.setTitle("แก้ไข", for: .normal)
self.stepStatusView.backgroundColor = UIColor.Reds.loss
self.stepStatusLabel.textColor = .white
case .pending:
self.buttonStatus.setIsEnable(false)
self.buttonStatus.setTitle(data.stepButtonTitle, for: .normal)
self.stepStatusView.backgroundColor = UIColor.Oranges.deepOrange
self.stepStatusLabel.textColor = .white
}
}
}
I know my data is too big but It have to, Has anyone ever face with this problem?
Thank you
Update solution
It found my problem already, It cause from my localized text function.
After I removed it everything working fine!~

unsure how to unwrap uilabel optional to calculate my to uilabels

I am new in the developer world and have been on and off since february with swift and Xcode. I am hoping someone could help me out. Im trying to make a simple tip calculator and can't seem to * two uilabels together. this is what i have so far... at the total.text = "(totalAmount) * (tipPercent)" ... thanks any help would be great. :)
#IBOutlet weak var totalAmount: UILabel!
#IBOutlet weak var textFIeld: UITextField!
#IBOutlet weak var tipPercent: UILabel!
#IBOutlet weak var slider: UISlider!
#IBOutlet weak var total: UILabel!
#IBOutlet weak var tip: UILabel!
#IBAction func sliderChanged(_ sender: UISlider) {
sldr()
total.text = "\(totalAmount) * \(tipPercent)"
}
#IBAction func buttonPressed(_ sender: Any) {
ttl()
}
func ttl() {
if let grandTotal = textFIeld.text {
totalAmount.text! = "\(grandTotal)"
}
}
func sldr() {
tipPercent.text! = "\(Int(slider.value))"
}
The below expression will just only return you a string without any multiplication that you are expecting.
total.text = "\(totalAmount) * \(tipPercent)"
You know how to unwrap the text as you have done in ttl. You should use the same approach here
if let totalTxt = totalAmount.text, let percentTxt = tipPercent.text {
//Then change to whatever scalar type you want
if let totalAmount = Float(totalTxt), let percent = Float(percentTxt) {
let totalBill = totalAmount * percent
total.text = "\(totalBill)"
}
}

pass data from tableviewcontroller to another tableviewcontroller in swift

I have a form I am creating
this form gets filled with textfields the user inputs. After answering all the questions a button pops up to save.
I am having a problem making this tableviewcontroller to pass the data to a new tableviewcontroller. I'm stuck and not sure how to go about this.
import UIKit
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var firstNameField: UITextField!
#IBOutlet var middleNameField: UITextField!
#IBOutlet weak var lastNameField: UITextField!
#IBOutlet weak var addressField: UITextField!
#IBOutlet weak var aptNumField: UITextField!
#IBOutlet weak var cityField: UITextField!
#IBOutlet weak var stateField: UITextField!
#IBOutlet weak var zipField: UITextField!
#IBOutlet weak var phoneOneField: UITextField!
#IBOutlet weak var phoneTwoField: UITextField!
#IBOutlet weak var allergiesField: UITextField!
#IBOutlet weak var DobField: UILabel!
#IBOutlet weak var sexField: UILabel!
#IBOutlet weak var hospitalField: UITextField!
#IBOutlet weak var doctorField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//Notifications to push datepicker
NotificationCenter.default.addObserver(forName: .saveDateTime, object: nil, queue: OperationQueue.main) { (notification) in
let dateVc = notification.object as! DatePopupViewController
self.DobField.text = dateVc.formattedDate
}
//Notifications to push genderpicker
NotificationCenter.default.addObserver(forName: .saveGender, object: nil, queue: OperationQueue.main) { (notification) in
let genderVc = notification.object as! GenderPopupViewController
self.sexField.text = genderVc.selectedGender
}
updateWidthsForLabels(labels: labels)
}
//Save Button Function
func textFieldDidChange(_ textField: UITextField) {
if textField == firstNameField || textField == lastNameField || textField == middleNameField || textField == addressField || textField == lastNameField || textField == cityField || textField == cityField || textField == stateField || textField == zipField || textField == phoneOneField || textField == phoneTwoField || textField == allergiesField {
saveBtn.isHidden = true
} else {
saveBtn.isHidden = false
}
}
#IBAction func saveBtnPressed(_ sender: Any) {
performSegue(withIdentifier: "saveFirstPageSegue", sender: self)
}
}
what about starting creating a model:
Form.swift
struct Form {
var firstname: String?
var middlename: String?
....
var doctor: String?
init(firstname: String, middlename: String, ..., doctor: String) {
self.firstname = firstname
self.middlename = middlename
...
self.doctor = doctor
}
}
now you can create this form instance when saving and pushing the data to the new VC:
yourCurrentForm.swift
#IBAction func saveBtnPressed(_ sender: Any) {
let formData = Form(firstname: firstNameField.text, middlename: middleNameField.text, ..., doctor: doctorField.text)
let newVC = myNewViewController()
newVC.form = formData
self.navigationController?.pushViewController(newVC, animated: true)
}
NewViewController.swift
class myNewViewController: UIViewController {
var form: Form?
.....
}
UPDATE:
Here is the repo: https://github.com/FlorianLdt/LFEasyDelegate
If you have some question just ask me
Hope it helps.
First Option - Structs - Preferred
Make use of Structs :
struct Manager
{
static var value : String = ""
}
Noe Update value of that function by just calling
Manager.value = "newValue"
Access that value anywhere Assign it to other Variables
let newStr : String = Manager.value
Second Option - AppDelegate - Not ideal
Create new object in AppDelegate
Now create a new object to access appDelegate
let appDel = UIApplication.shared.delegate as! AppDelegate
Access Value and update as below
appDel.Frequency = 1.0
Third Option - NSObjectClass
Create a new NSObject class as below
//Instance created when NSObject class is first time loaded in memory stack
static let shared = wrapperClass()
//Create a value to access Globally in Object class
var newValueInClass : String = ""
Now time to access that created Object
wrapperClass.shared.newValueInClass = "iosGeek"
Now Anywhere write this Line
print(wrapperClass.shared.newValueInClass)
Console Output
Better to use struct classes to manage data globally

ProgressBar display only end

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!