I've many classes in my XCode project, and only the class from where the control comes from main.m will be having applicationDidFinishlaunching method. in my other classes will the methods in it knows how to go to its own method implementation, or do i need to write init method?
I'm not sure I understand what you are asking about, but I will try.. :-)
applicationDidFinishLaunching is part of the UIApplicationDelegate protocol. Typically, only one class will implement this protocol in one application.
In you other classes, you implement the init method if you need to do something when an instance of that class is created, like instantiating and initializing property values for that instance.
Related
There is a similar question to mine on the following link
but it doesn't quite answer my query.
I am setting a helper class for Facebook (follows the delegation pattern) . An example of one of the class methods would be:
+ (void)openSession:(id)delegate;
This method calls a the Facebook openActiveSessionWithReadPermissions method which expects a completionHandler block. Would it make sense to call the delegate method, say sessionStateChanged in the block as follows?
[delegate sessionStateChanged];
Or is it better to use instance methods for the Facebook helper class and call the delegate using [self.delegate sessionStateChanged] in the completionHandler block.
You would be better off with a block parameter rather than a delegate as a parameter if it is just for a single callback.
+ (void)openSession:(void (^)(void))sessionStateChangedBlock
That way you don't have to worry about defining a delegate protocol.
If you want to use a delegate, you will have to define a delegate variable at the class level. You can't use [self.delegate sessionStateChanged] because you are saving the delegate as a class variable. self is only available in an instance of the class.
I tried both methods i.e. using class and instance methods. Any of them will do, though to follow the proper delegation pattern I believe using instance methods is more appropriate.
I have a class with some methods neither declared in .h file nor in any category, I want to override these methods in the subclass to change the behavior slightly. I am thinking of simply redefining those methods in subclass. Is it a good idea? would it even work?
Ideally the subclass needs to know what the methods are if it wants to override the method and still call super.
One way of doing this is by having a separate header file which both the super class and subclass implementations both import.
// MyClass_protected.h
- (void)someMethodThatYouWantSubclassesToBeAbleToOverride;
Then
// MyClass.m
#import "MyClass_protected.h"
// MySubClass.m
#import "MyClass_protected.h"
It'll 'work' in that the compiler allows it. The class that defines these methods probably assumes they do particular things when called, which your implementation needs to respect when overriding them to avoid introducing bugs in the use of the class's interface.
You can override private methods in a base class but the problem is that you can't call [super someMethod]. If you wish to completely replace the original method then this isn't an issue.
Otherwise you need to let the derived class know about the methods in the parent class.
Trying to get my head around protocols and delegates when extending it further into the UIKit framework's implementation.
From my understanding of this stackoverflow post a delegate method will usually have Did, Should & Will in it's name.
Based on this, I would assume that - (void)viewDidLoad; declared in UIViewController.h is a delegate method, but of what and from where?
I've looked at UIViewController's header file and it only adhere's to the NSCoding protocol which is a dead end. UIViewController's superclass UIResponder is also a dead end as far as I can see.
I've used viewDidLoad as an example here but this could apply to any of the Did, Should & Will methods in UIViewController.
Is this simply one of those cases that is an exception to the guidelines or am I missing something?
"did", "should", and "will" are words usually used to describe when a method is called, whether it is asking if it "should" do something", giving you a hook to run code before something "will" happen, or as a callback when something "did" happen. these words are usually used in delegate and callback methods.
viewDidLoad is called when your .nib file has been loaded into memory, and your IBOutlets have been instantiated and hooked up, and are ready for configuration. you don't need to worry about calling it yourself if you intend to subclass UIViewController, if that's what you're wondering.
So this may be a really dumb question, but is it possible to create a new delegate object at runtime without having to create an entire class interface/implementation for it? I basically wanna spawn off a new webview and make a delegate for it that just implements webViewDidFinishLoad so I can do some size manipulation then. It seems like a lot of overhead to create an entirely new class just for one method.
You can implement the delegate methods in your existing class. Just set the delegate to self and implement the methods you want.
I have a questions about the iPhone application. I am the green of the iPhone application. When I read the document(PDF) download from the apple developer website (online version). I found that the document always mentions different methods of the library.
There are
1) Class method
2) Instance method
3) Delegate method
I understand the use and meaning of the instance method, which is called by a instance.
let's say the delegate methods is the connection:didReceiveAuthenticationChallenge and the class method sendSynchronousRequest:retruningResponse:error:.
However, I don't understand about the different between the class method and the delegate method. Is the class method for the whole class? or whole project? What it means of the delegate? and where should I put the code after I modify the content of the delegate? How can I call the method?
Can anyone help me. Thank you very much.
It is another question about the delegate method. And I don't how to solve the problems. Please help me. Thank you.
HTTP status code = 0 (iPhone) (objective c)
Suppose you have a class Foo and an instance of that, Foo* foo.
Then, the class method is a method which is sent to the class:
[Foo classMethod];
while the instance method is a method sent to the instance:
[foo instanceMethod];
The delegate method is a method which the instance of the class calls. So, you typically implement another class Delegate with an instance Delegate* delegate, and do
[foo setDelegate:delegate];
Then, the object foo calls the delegate method of delegate at appropriate times:
[delegate delegateMethod];
This is a way to receive an event from the system API.
Apple provides extensive documentation on the fundamentals for Objective-C and Cocoa - if in doubt, this should be your first stop.
The Objective-C Programming Language - Class Objects:
[...] a class definition can include methods intended specifically for the class object—class methods as opposed to instance methods. A class object inherits class methods from the classes above it in the hierarchy, just as instances inherit instance methods.
Cocoa Fundamentals Guide - Delegates and Data Sources:
A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.
The delegating object is often a responder object—that is, an object inheriting from NSResponder in Application Kit or UIResponder in UIKit — that is responding to a user event. The delegate is an object that is delegated control of the user interface for that event, or is at least asked to interpret the event in an application-specific manner.
And some related background in The Objective-C Programming Language - Protocols:
Class and category interfaces declare methods that are associated with a particular class — mainly methods that the class implements. Informal and formal protocols, on the other hand, declare methods that are independent of any specific class, but which any class, and perhaps many classes, might implement.
A delegate method is a method that is defined in a classes delegate protocol. They are added to your class but your class must have the objects delegate protocol. They are usually used by the object but is something that you must define for the object. NSTableView and UITableView use delegate methods to populate their data. A class method is just one that you define in your interface.