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

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.

Related

How can I pass a textfield value from one ViewController to a second ViewController (MacOS Swift)?

I'm working on a project and my ViewController file is starting to get very long. Hence, I want to define all my functions in a second ViewController so I can delete some code from my first ViewController. This works, except for when I try to refer to a value from a textField defined in my FirstViewController, it returns nil. I am new to MacOS development so I would greatly appreciate simple/specific feedback.
This is an example of my first ViewController (it initializes variables and uses functions):
class FirstViewController: NSViewController, NSTextFieldDelegate {
#IBOutlet weak var firstName: NSTextField!
let lastName = "Smith"
#IBAction func buttonPressed(_ sender: Any) {
SecondViewController().printUserName()
}
}
This is my second ViewController (it defines functions):
class SecondViewController: NSViewController, NSTextFieldDelegate {
func printUserName() {
print(FirstViewController().firstName!) // this returns nil :(
print(FirstViewController().lastName) // this returns "Smith" :)
}
}
If you use the UINavigationController to move to the second VC, you can simply define a second view and then access the variables in that view.
example, FirstViewController
class FirstViewController: NSViewController, NSTextFieldDelegate {
#IBOutlet weak var firstName: NSTextField!
let lastName = "Smith"
#IBAction func buttonPressed(_ sender: Any) {
guard let secondView = self.storyboard?.instantiateViewController(identifier: "SecondViewController") as? SecondViewController else {
return
}
self.navigationController?.pushViewController(secondView, animated: false)
secondView.textValue = firstName.text
}
}
SecondViewController
class SecondViewController: NSViewController, NSTextFieldDelegate {
var textValue: String?
override func viewDidLoad() {
printUserName()
}
func printUserName() {
if let text = textValue {
print(text)
}
}
}

reloadData() from another viewController Swift

I have two viewControllers: the first one has a tableView in it and the second one has a textField with an action, if there is a specific text inserted in the textFiled, I want to call loadData1() function which has orderTable.reloadData() to reload the tableView from the logInviewController, but it returns nil when I call it.
tableViewController code :
import UIKit
import FirebaseFirestore
import Firebase
import FirebaseAuth
class orderTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
#IBOutlet var orderTable: UITableView!
var db: Firestore!
var firstName = [String]()
var lastName = [String]()
override func viewDidLoad() {
super.viewDidLoad()
orderTable.register(UINib(nibName: "Order1TableViewCell", bundle: nil) , forCellReuseIdentifier: "orderCell")
}
func loadData1() {
Firestore.firestore().collection("hola").getDocuments() { [self]
(querySnapshot, err) in
if let err = err
{
print("Error getting documents: \(err)");
}
else
{
for document in querySnapshot!.documents {
self.firstName.append(document.get("firstname") as? String ?? "")
self.lastName.append(document.get("lastname") as? String ?? "")
}
}
orderTable.reloadData() // from here i got Unexpectedly found nil while unwrapping an Optional value:
}
}
}
}
logInViewController code :
import UIKit
import Firebase
import FirebaseAuth
class logInViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var userNameField: UITextField!
#IBOutlet var passwordField: UITextField!
#IBOutlet var logInButton: UIButton!
var db: Firestore!
var order: orderTableViewController!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func textfieldDidChange(_ sender: Any) {
print(userNameField?.text ?? "")
if userNameField.text == "v#v.com" {
let i = orderTableViewController()
i.loadData1()
}
}
}
Where you have let i = orderTableViewController(), you are not referencing your existing table view controller, but rather are creating a new one, except this time it is not instantiated in conjunction with the storyboard scene, and thus all of your #IBOutlet references will be nil. Attempts to reference those #IBOutlet references will fail.
To fix this, you should pass a reference for the first view controller to the second one, using a protocol rather than an explicit class name, and then the second view controller can call a method in the first. Thus:
Create class protocol, e.g. LoginViewControllerDelegate:
protocol LoginViewControllerDelegate: class { }
Give that protocol one method requirement, loadData1:
protocol LoginViewControllerDelegate: class {
func loadData1()
}
Make your first view controller conform to that protocol:
extension OrderTableViewController: LoginViewControllerDelegate {
func loadData1() {
... your implementation here ...
}
}
Create a property in the second view controller, that LoginViewController, for this delegate-protocol reference, e.g.:
weak var delegate: LoginViewControllerDelegate?
When first view controller instantiates second, set this delegate property (e.g. if doing segues, it would be in prepareForSegue):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? LoginViewController {
destination.delegate = self
}
}
The second view controller then would call delegate?.loadData1() rather than i.loadData1().
If you do what I understand then you can do this. But you should use delegate or closure callback to do that.
#IBAction func textfieldDidChange(_ sender: Any) {
print(userNameField?.text ?? "")
if userNameField.text == "v#v.com" {
if let i = order {
i.loadData1()
}
}
}
}

How do I present ViewController programatically?

import UIKit
//EventList ViewController
class EventPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
class EventForm: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
//IBOutlets
#IBOutlet weak var eventNameField: UITextField!
#IBOutlet weak var fromTimePicker: UIDatePicker!
#IBOutlet weak var toTimePicker: UIDatePicker!
#IBOutlet weak var colorPreview: UIView!
#IBAction func cancel(_ sender: Any) {
//empty text field
eventNameField.text = ""
}
#IBAction func save(_ sender: Any) {
if (eventNameField.hasText) {
//fix error handling
eventNameField.backgroundColor = UIColor.systemGray2
//pull data from fields
let text = eventNameField.text!
let fromTime = fromTimePicker.date
let toTime = toTimePicker.date
//initialize object
let currentEvent = EventModel(eventName: text, fromTime: fromTime, toTime: toTime, color: storedColor)
//append to data model
EventDataArray.append(currentEvent)
//transition
present(EventPage(), animated:true)
}
else {
eventNameField.backgroundColor = UIColor.systemRed
}
}
}
I currently have an EventPage class declared as type UIViewController, but upon pressing the save button with a populated text field a transition to a blank ViewController occurs. I've attached the class to the correct ViewController in main.storyboard.
The problem in here is that you are creating a new EventPage but it doesn't inherit from Storyboard.
1
Go to the inspector in your storyboard, select your View Controller, and write an identifier for your View Controller (can be anything)
Write it in Storyboard ID:
2
Replace
present(EventPage(), animated:true)
With
(don't forget to replace 'MYIDENTIFIER' with the id you entered earlier)
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MYIDENTIFIER") as! EventPage
// If you need to do any configurations to your view controller, do that in here.
// For example:
// viewController.label.text = "Hello, world!"
present(viewController, animated:true)
Note
If the name of your Storyboard file name is not called Main, replace "Main" in step 2 with the name of your storyboard file (excluding .storyboard)

Having issues passing a delegate from NSWindowController subclass to my ViewController

I'm having issues with passing a custom protocol (MainWindowControllerProtocol) to the EditorViewController from the MainWindowController, which is subclass of NSWindowController. Please help.
EditorViewController.swift
extension EditorViewController: MainWindowControllerProtocol {
func didOpenFile() {
print("TODO: Open File") // never called, but it should be
}
}
class EditorViewController: NSViewController {
// - IBOutlets
#IBOutlet weak var treeOutlineView: NSOutlineView!
#IBOutlet var codeTextView: NSTextView!
#IBOutlet weak var counterTextField: NSTextField!
#IBOutlet weak var languageTextField: NSTextField!
//public var editor = Editor()
//var rootNode: Node?
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
//rootNode = Path(Path.userDownloads).node
// Issue is here
if let windowController = NSApplication.shared.mainWindow?.windowController as? MainWindowController {
windowController.delegate = self
}
else {
print("Doesnt work") // prints this
}
//treeOutlineView.reloadData()
}
}
MainWindowController
public protocol MainWindowControllerProtocol {
func didOpenFile()
}
class MainWindowController: NSWindowController {
var delegate: MainWindowControllerProtocol?
override func windowDidLoad() {
super.windowDidLoad()
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
#IBAction func openFile(_ sender: Any) {
print("In here") // this is called?
delegate?.didOpenFile() // but this never is apparently
}
}
Maybe this topic should help.
This method might return nil if the application’s nib file hasn’t
finished loading, if the receiver is not active, or if the application
is hidden.
Have you checked if NSApplication.shared.mainWindow is nil or just NSApplication.shared.mainWindow?.windowController cannot be casted to your controller class ?

Swift Text Label Nil Even With Default Value

This is driving me crazy. The function updateTextView() is being called, verified by the print statements, but it is not setting the label in my view controller, and the print statements for the label are returning nil even though it has a default value set which is visible when the app is loaded. Whats more perplexing is that I set up a test button to call this function separately, and when I call it with test(), then the label updates properly.
class GoalDetailViewController: UIViewController, TextDelegate {
#IBAction func test(sender: AnyObject) {
updateTextView()
}
func updateTextView() {
print(goalSummaryTextBox?.text)
print("delegate called")
self.goalSummaryTextBox?.text = GoalsData.summaryText
print(goalSummaryTextBox?.text)
}
#IBOutlet weak var goalTitle: UILabel?
#IBOutlet weak var goalCreationDate: UILabel?
#IBOutlet weak var goalSummaryTextBox: UITextView?
override func viewDidLoad() {
super.viewDidLoad()
goalSummaryTextBox?.text = GoalsData.summaryText
}
}
updateTextView() is being called through a delegate method after I pop a different view controller, as can be seen below:
class TextEditViewController: UIViewController {
var textDelegate: TextDelegate?
#IBOutlet weak var textView: UITextView?
func configureView() {
navigationItem.title = "Edit Description"
navigationItem.setRightBarButtonItem((UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "segue")), animated: true)
}
func segue() {
textDelegate = GoalDetailViewController()
if let text = textView?.text {
GoalsData.summaryText = text
}
textDelegate?.updateTextView()
self.navigationController?.popViewControllerAnimated(true)
}
}
The line causing the issue is in the TextEditViewController below:
textDelegate = GoalDetailViewController()
What this line does is creates a new instance of GoalDetailViewController, and sets it as the delegate to the TextEditViewController. But, what you want is the original instance of GoalDetailViewController to be the delegate. This is why you were seeing the logs when popping TextEditViewController, since it was executing the other instance (which wasn't part of the view hierarchy). It also explains why all your IBOutlets are nil when stepping through updateTextView() on the delegate call, and that the button you added updates the text properly.
The solution is to make the delegate connection in the prepareForSegue method:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let destination = segue.destinationViewController as? TextEditViewController {
destination.textDelegate = self
}
}
Add the above code to your GoalDetailViewController.
EDIT:
The below code will ensure that this problem does not happen in the future. Change the delegate's definition to:
weak var textDelegate: TextDelegate?
and change your protocol to:
protocol TextDelegate: class {
func updateTextView()
}