Delegate Seems to Not Be Working, According to the Console

Delegate seems to not be working, according to the console

Whenever you used delegates you need to pass that delegate from one view controller to another view controller.
According to Apple definition:

Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object–the delegate–and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.

The missing part you are doing is that you are not calling the delegate for expample you called JournalTextDelegate in your class JournalEntryController so you need to call this JournalTextDelegate to your JournalPage.

for example: Suppose your going to another view controller through push method

let vc = self.storyboard?.instantiateViewController(withIdentifier: “identifierier”) as! JournalPage
vc.delegate = self // you need to call this delegate
self.navigationController?.pushViewController(notifDetailVCObj, animated: true)

And it will work fine. For reference see documentation https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html

Delegate seems to not be working, according to the console

Whenever you used delegates you need to pass that delegate from one view controller to another view controller.
According to Apple definition:

Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object–the delegate–and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.

The missing part you are doing is that you are not calling the delegate for expample you called JournalTextDelegate in your class JournalEntryController so you need to call this JournalTextDelegate to your JournalPage.

for example: Suppose your going to another view controller through push method

let vc = self.storyboard?.instantiateViewController(withIdentifier: “identifierier”) as! JournalPage
vc.delegate = self // you need to call this delegate
self.navigationController?.pushViewController(notifDetailVCObj, animated: true)

And it will work fine. For reference see documentation https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html

Protocol not working even after setting the delegate

Since your BlueCoreManager is a singleton, you shouldn't ever create an instance of it, as you do in your view controller.

You should always use BlueCoreManager.shared() to get the singleton instance.

The delegate pattern does not fire in the class where it is implemented

You're not using your topMenuBar.

let topMenuBar = TopMenuBar()

In viewDidLoad you do topMenuBar.delegate = self, but the actual view that you set as the header is this:

guard let cell = tableView.dequeueReusableCell(withIdentifier: TopMenuBar.cellIdentifier()) as? TopMenuBar else {
return nil
}

Here, you're creating a new TopMenuBar (cell)! You're not using the let topMenuBar = TopMenuBar() at all!

What you need to do is get rid of:

let topMenuBar = TopMenuBar()

override func viewDidLoad() {
super.viewDidLoad()
topMenuBar.delegate = self
}

and instead, set the delegate of cell.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let cell = tableView.dequeueReusableCell(withIdentifier: TopMenuBar.cellIdentifier()) as? TopMenuBar else {
return nil
}

cell.delegate = self /// assign here!
return cell
}

Why does not this delegate work inside the loop?

This is because of the semantics of closures. When a closure references a local variable in its outer scope, it captures a reference to the variable, not the value contained in the variable.

In this case, the anonymous delegate delegate() { Console.WriteLine(strings[i]); } is capturing a reference to the i variable; that is, the variable is shared between the anonymous function and the scope in which i was declared. When i changes in one context, it also changes in the other.

For example (see it run):

using System;

class Foo {
static void Main() {
int i = 0;
Action increment = delegate { ++i; };

Console.WriteLine(i);

++i;
Console.WriteLine(i);

increment();
Console.WriteLine(i);

++i;
Console.WriteLine(i);

increment();
Console.WriteLine(i);
}
}

This will output:

0
1
2
3
4

In C#, the lifetime of a local is extended to include the lifetime of any closures that reference them. This enables some pretty interesting tricks that may offend the sensibilities of C/C++ developers:

static Func<int> Counter() {
int i = 0;
return delegate { return i++; };
}

Delegates and Func Cast Issue

You are missing fact that:

Func<int> f = () => { return 123; };
Delegate t = f;

is in fact using constructor:

Func<int> f = new Func<int>(() => { return 123; });

But there is no Delegate constructor taking lambda expression or implicit conversion between them.

Non-responsive stream delegate in Swift

So after a lot of trials and errors, I finally realized the stream() function did not work just because the signature of this function is incorrect/obsolete.

Here is what I was using:

func stream(aStream: Stream, handleEvent eventCode: Stream.Event)

But really it should be:

func stream(_ aStream: Stream, handle eventCode: Stream.Event)

This is likely a syntax conversion from previous Swift version to Swift 3. The XCode compiler usually detects obsolete functions/syntax, but sadly did not catch this one.

Hopefully my answer could help out those who are still suffering from this problem.

Stop propagation with jQuery delegate/live function not working

AFAIK you cannot reliably prevent an inline event handler from firing by stopping the bubbling within an attached event handler.

Furthermore, using live() or .delegate() you cannot use preventDefault() nor stopPropagation(). You need to return false to prevent the bubble phase and the default behavior.

Anyway, as I already mention you can't prevent the inline event handler to fire with that.

So either, create it completely unobtrusive (which is what I highly recommend) or remove that inline click handler in code.

Example:

$('#update-list').delegate('.popup-link', 'click', function(e){       
$.popup(); // launch popup
return false;
}).delegate('.update', 'click', function(){
window.location('some_url');
})
// the rest of this is unnecessary if you can just omit the onclick attribute
.find('.update')
.removeAttr('onclick');

Ref.: .delegate()



Related Topics



Leave a reply



Submit