I have two classes. One class is named ViewController and the other class is named TabView.
My goal is to call a function changeTab() which is inside the TabView class from the ViewController.
Somehow I am having trouble with it because everytime my delegate is nil.
Here is my code for ViewController:
protocol TabViewProtocol: class {
func changeTab()
}
class ViewController: NSViewController {
// delegate
weak var delegateCustom : TabViewProtocol?
override func viewDidLoad() {
print(delegateCustom) // outputs "nil"
}
buttonClickFunction() {
print(delegateCustom) // outputs "nil"
delegateCustom?.changeTab() // doesn't work
}
}
Here is my code for TabView:
class TabView: NSTabViewController, TabViewProtocol {
let myVC = ViewController()
override func viewDidLoad() {
super.viewDidLoad()
myVC.delegateCustom = self
}
func changeTab() {
print("test succeed")
}
}
Can someone explain me what I am doing wrong? - I am new to delegates and protocols...
You are using the delegate pattern wrongly. It is hard to tell which controller you want to define the protocol for and which one you want to adopt it - but here is one possible way.
// 1. Define your protocol in the same class file as delegate property.
protocol TabViewProtocol: class {
func changeTab()
}
// 2. Define your delegate property
class ViewController: NSViewController {
// delegate
weak var delegateCustom : TabViewProtocol?
override func viewDidLoad() {
// It should be nil as you have not set the delegate yet.
print(delegateCustom) // outputs "nil"
}
func buttonClickFunction() {
print(delegateCustom) // outputs "nil"
delegateCustom?.changeTab() // doesn't work
}
}
// 3. In the class that will use the protocol add it to the class definition statement
class TabView: NSTabViewController, TabViewProtocol {
let myVC = ViewController()
override func viewDidLoad() {
super.viewDidLoad()
myVC.delegateCustom = self
// Should output a value now
print(myVC.delegateCustom) // outputs "self"
}
func changeTab() {
print("test succeed")
}
}
you are creating a new instance in this line:
let myVC = ViewController()
you should get existing instance of your ViewController.then set
myVC.delegateCustom = self
Related
I need to post a notification on viewDidLoad of every one of my ViewControllers. I have a BaseViewController with a postNotification method, and it gets an enum as a parameter to identify the screen. It looks like this
class BaseViewController {
func postNotification(for screen: ScreenName) {
NotificationCenter.default.post(name: notification,
object: nil,
userInfo: ["ScreenName": screen])
}
}
class AViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
postNotification(for: screenA)
}
}
class BViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
postNotification(for: screenB)
}
}
Suppose we need to add another view controller later in the future, such as CViewController. I want to force the developer of CViewController to call this postNotification method with screen enum.
What is the best practice to achieve this on Swift?
Edit
Thanks to Loren's suggestion, I added a protocol on my base class
typealias BaseController = BaseViewController & BaseProtocol
protocol BaseProtocol {
var screenName: ScreenName { get }
}
This forces all my viewcontrollers to conform protocol and initialize screenName, but now I can't get it from my BaseViewController. If I can get child view controller's screenName property from BaseViewController, I would eliminate calling postNotification method on each child, and call it only on BaseViewController
class BaseViewController: UIViewController {
var screenName: ScreenName!
override func viewDidLoad() {
super.viewDidLoad()
if let screenName = self.screenName {
self.postNotification(for: screenName)
} else {
fatalError("screenName must be instantiated")
}
}
func postNotification(for screen: ScreenName) {
NotificationCenter.default.post(name: notification,
object: nil,
userInfo: ["ScreenName": screen])
}
}
class AViewController: BaseViewController {
override func viewDidLoad() {
self.screenName = screenA
super.viewDidLoad()
}
}
class BViewController: BaseViewController {
override func viewDidLoad() {
self.screenName = screenB
super.viewDidLoad()
}
}
Try this since you are using inheritance, have BaseViewController inherit UIViewController superclass, then create a screenName variable. In your childViewControllers instantiate the screenName before calling super.viewDidLoad(). You can use protocols like you mentioned to force the implementation of the variable, but it just seems like overkill for just one variable.
called class:
class LoginViewController: UIViewController {
let chipField: UITextField = {
........
return textField1
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(chipField)
}
}
table to be called:
class MainTableViewController: UITableViewController {
....
}
MainTableViewController call chipfield in LoginViewController
You should really use a delegate in this case, something like
Read more about delegates Here
protocol MainTableViewControllerDelegate {
func getChipFieldValue()
}
class MainTableViewController: UITableViewController {
var delegate: MainTableViewControllerDelegate?
// to get chipFieldValue self.delegate?.getChipFieldValue)
}
In LoginViewController define the function in delegate
class LoginViewController: UIViewController,MainTableViewControllerDelegate {
func getChipFieldValue() ->String {
return chipField.text
}
// later in the code when you present MainTableViewController view pass delegate to self to MainTableViewController object
// something like the MainTableViewControllerObject.delegate = self
}
From your situation all you need is just a property in MainTableViewController
class MainTableViewController: UITableViewController {
var chipFieldCopy:UITextField?
...
}
But I would suggest you rethink if you need the entire UITextField, in most situation you will only need it's text so
class MainTableViewController: UITableViewController {
var chipInfoString:String?
...
}
Is enough.
In your handleLogin() method, you can pass it into MainTableViewController like:
//let navController = UINavigationController(rootViewController: MainTableViewController()) //Replace this line
let mainTableVC = MainTableViewController()
mainTableVC.chipFieldCopy = chipField
let navController = UINavigationController(rootViewController: mainTableVC)
or like I suggest
mainTableVC.chipInfoString = chipField.text
and you can access it in MainTableViewController
I have a viewController with another containerView insider set up to appear temporarily (added programmatically). The containerView is a sort of operation bar, which allows you to change values of the viewController. The protocol called from an IBAction of a button however, does not call the protocol set up inside the viewController class.
Here is the code from both classes:
class viewController: UIViewController, updateListDelegate {
let dataSource = containerView()
override func viewDidLoad() {
super.viewDidLoad()
dataSource.delegate = self
}
func updateList(sender: containerView) {
print("is called") //is not printed
}
}
The code from the containerView:
protocol updateListDelegate {
func updateList(containerView)
}
class containerView: UIViewController {
var delegate: updateListDelegate?
#IBAction func AddSong(_ sender: UIButton) {
self.delegate?.updateList(sender: self)
}
}
If this method is only to be called from one object, then, in my opinion, I would not define a protocol. If multiple objects are to call this method, then I would define a protocol. This is typically how you would call a method backwards, using a basic delegate.
class ViewController: UIViewController {
let container = ContainerView()
override func viewDidLoad() {
super.viewDidLoad()
container.viewControllerDelegate = self
// push to this instance of container at some point
}
func doSomething() {
print("great success")
}
}
class ContainerView: UIViewController {
weak var viewControllerDelegate: ViewController?
#objc func someAction() {
if let viewControllerDelegate = viewControllerDelegate {
viewControllerDelegate.doSomething()
}
}
}
// prints "great success" when someAction() called
One of the most common mistakes people make is not keeping track of instances. For delegates to work, you must be sure you are using the specific instances that you've instantiated and assigned those delegates to.
Usually, I write this function to init my presenter from viewController. But I want to use init() for making this. What can I do?
ViewController :
class ViewController: UIViewController {
private let viewPresenter = viewPresenter()
override func viewDidLoad() {
self.viewPresenter.attachView(controller: self)
}
#IBAction func faceBookButtonTapped(sender: UIButton) {
self.viewPresenter.showLoginWindow()
}
}
ViewPresenter :
class ViewPresenter {
var controller: ViewController?
func attachView(controller: ViewController) {
self.controller = controller
}
}
So, If I make this Init in my presenter :
let controller: ViewController?
init (controller:ViewController) {
self.controller = controller
}
And try to init like this in viewController:
private let viewPresenter = ViewPresenter(controller: self)
Xcode give me this error:
Cannot convert value of type '(NSObject) -> () -> ViewController' to expected argument type 'ViewController'
the problem is that there is no "self" at that point in time where you want to initialize your presenter because your view controller is initialized later. You could work around this problem by declaring your presenter as lazy like this
fileprivate lazy var viewPresenter: ViewPresenter = {
return ViewPresenter(controller: self)
}()
or you could just initialize your presenter later in viewDidLoad
private var viewPresenter: ViewPresenter!
override func viewDidLoad() {
super.viewDidLoad()
self.viewPresenter = ViewPresenter(controller: self)
}
If you ask me the best approach is (as you already did) to attach the view in viewDidLoad to the presenter.
Hope this helps.
I have a UIViewController -let's call it parentViewController - and it contains a container. This container has embeddedViewController embedded in it.
Now, my parentViewController contains a method that prints something into the console:
func printSomeData(){
print("some data")
}
embeddedViewController has a button with action asigned to it:
#IBAction func printSomething(sender: AnyObject) {
is there a way that I could call method printSomeData from printSomething in Swift?
There are couple of ways such as by implementing delegate or by posting NSNotification. Here I show the sample delegation pattern. This is exact scenario as your own controller but from this you can get some concept and if you implement this hope this will accomplished your goal.
class ParentController:UIViewController,printing {
override func viewDidLoad() {
//
}
func presentEmbadedController(){
let embadedVC = EmbadedController()
embadedVC.delegate = self
}
func printSomeData() {
print("some date")
}
}
Here is the protocol something like this
protocol printing{
func printSomeData()
}
And then the EmbadedController like this
class EmbadedController:UIViewController {
var delegate: printing?
override func viewDidLoad() {
//
}
#IBAction func printSomething(sender: AnyObject) {
if let _ = delegate{
delegate?.printSomeData()
}
}
}
You can use NSNotificationCenter.defaultCenter().addObserver(...)