Perseus
Perseus

Reputation: 1588

Calling the delegate methods

Still I couldn't understand completely how these delegate methods are getting called. I have UIViewController,UITextFieldDelegate in one class which will call its delegate methods without specifying like textField.delegate = self;

But for some different purposes like UIWebViewDelegate we are supposed to enter like webView.delegate = self; and it seems like it is calling its delegate methods. Perfect.

But now I am facing a problem. I am using CLLocationManagerDelegate and also CALayer in same class. For both I am giving location.delegate =self; and layer.delegate =self; At some point both are conflicting each other and only one of the thing is working either CLLocationManagerDelegate or CALayer. The other thing is getting stopped. I don't why it so happens like this? Any reason? How can we overcome this. Even I planned to use some other frameWork, say UIWebView . I will face same problem for those delegate methods also. Can you tell me why it is working in that way ?

Upvotes: 0

Views: 329

Answers (1)

Pochi
Pochi

Reputation: 13459

The classes that will call its delegates without you specifying them have default implementations, which means that they already know what to do, and will only change their behavior if you override these methods.

Setting 2 or more classes to the same delegate should not interfere with each other (unless for a very weird reason in where the method is named the same in both custom classes).

Your problem is most likely the fact that you havent implemented those methods or are using those classes wrong.

For example, Location manager requires you to create an instance, configure it and START running updates. The most common method for a delegate of this type is the "did update location" (or something like that). Which you have to implement if you want to be informed of every time a new location is received. Otherwise you have to read the location manually whenever you desire.

As a suggestion, every time you set a delegate for an object, you have to do the object.delegate = self; thing. And you probably noticed that you will get a warning until you specify in the header that it conforms to that protocol: for example.

Just control click the UITextFieldDelegate word.

Look for the methods under @required, THOSE you have to always implement. the @optional have default implementations so unless you wanna change the behavior its not needed to implement them.

Upvotes: 2

Related Questions