Macos Swift Master-Detail Delegate/Protocol Not Working

Passing data to main view controller with delegate in swift for macOS

Most likely the issue occurs because didSet is called sooner than viewDidLoad

But you don't need protocol / delegate at all in this case.

As the AddBook controller is presented modally use the property presentingViewController.

The sender parameter in the delegate method is unused.

let parent = presentingViewController as! ViewController
parent.addBook(_ data: [String])

I'd even create the Book in the add controller and return

parent.addBook(_ book: Book)

Delegate method is not being called

In case somebody has the same issue, it's because I'm setting delegate variable inside viewDidLoad which is invoked when view is initially loaded. It isn't invoked when view appears again when user navigates back to the view from deeper view controllers. The delegate variable should have been set inside viewWillAppear.

How can I set the delegate for a protocol from the child of the destination VC?

If you want pass some data after you touch cell in your DetailVC, you could use NotificationCenter

class MasterVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(childVCDidSelect(_:)),
name: DetailVC.selectionNotificationName,
object: nil)
}

@objc func childVCDidSelect(_ value: String) {
print("MasterVC recieve \(value) from DetailVC")
}
}

class DetailVC: UIViewController, UITableViewDelegate {

static var selectionNotificationName: NSNotification.Name {
return NSNotification.Name(rawValue: "DetailVCSelectionNotification")
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// value variabl could by any type, maybe you want pass model for selected index path.
let value = "your value"
// When you call NotificationCenter post, you past value to all subscribers,
// who has subcribed NotificationCenter.default.addObserver for DetailVC.selectionNotificationName
NotificationCenter.default.post(name: DetailVC.selectionNotificationName, object: value)
}
}

class ChildVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(childVCDidSelect(_:)),
name: DetailVC.selectionNotificationName,
object: nil)
}

@objc func childVCDidSelect(_ value: String) {
print("ChildVC recieve \(value) from DetailVC")
}
}

ios - Multiple Delegates for a ViewController

What I do in this kind of case is pass a weak reference of the master controller to any of its sub-controllers. I don't call them delegates. A delegate protocol makes sense when a wide variety of classes can use the same delegate protocol. In this case, just call it like it is.

Delegate method not being called objective C

The problem is that you've created two SecondViewController objects and made your ViewController the delegate of the wrong one.

This: [[SecondViewController alloc] init] creates an object in code. This: [self performSegueWithIdentifier:@"moveToSecondController" sender:self] creates an object from a storyboard definition.

Don't bother creating the first one, just perform the segue. Then, implement the prepareForSegue method and set your delegate there, using the destination controller (which will be the correct SecondViewController).

Xcode Table View Sections not updating

You can try to reload table by adding proper delegate to cell, for example

protocol DetailsCellDelegate: class {
func updateTable(with: Data)
}

And perform this delegate with your Data by calling delegate?.updateTable(with: data)



Related Topics



Leave a reply



Submit