In a word, how?
I have a view controller that creates a few custom objects. One of those objects needs to call a method back in the controller. The object looks like it wants to call a class method in the controller, which might be fine, except that class in the controller needs to call an instance method in the controller, and it doesn't seem to want to do that. Does that make sense?
If not, here's pseudo code:
ViewController.m
#import "customObj.h"
-(void)viewDidLoad{
foobar=@"string";//declared in ViewController.h
}
-(void)createObj{
foobar=@"different string";
customObj *customObjInstance=[[customObj alloc] init];
}
---
customObj.m
#import "ViewController.h"
-(void)callBack{
[ViewController createObj];
}
Okay, so when callBack runs, it errors, saying it's looking for +createObj (not -createObj). I can change createObj to a class method, but then it has a problem setting foobar because foobar was initialized in -viewDidLoad, and I can't change that to +viewDidLoad. I could maybe move foobar out into a class method, but then how do I call it? Instead of self, do I refer to [ViewController ...]? I don't think that works.
My guess is I'm missing some basic concept and it's not anywhere as difficult as I'm making it. I am indebted to anyone who can straighten me out.
Thanks much.
Not only can they modify object state, instance methods can also access the class itself through the self.
Calling Instance Method:You can not call an instance method in the static method directly, so the Instance method can be invoked using an object of the class.
The methods (that is, subroutines) that the object contains are called instance methods. For example, if the PlayerData class, as defined above, is used to create an object, then that object is an instance of the PlayerData class, and name and age are instance variables in the object.
Instance method performs a set of actions on the data/value provided by the instance variables. If we use instance variables inside a method, such methods are called instance methods. Class method is method that is called on the class itself, not on a specific object instance.
There's no single correct answer; it depends on how your custom object interacts with the controller. Most UIKit views use a "target-action" pattern for communicating with the controller, while a lot of model objects use the delegate pattern. Which one you use depends on the specifics of your controller and your custom object.
However, mechanically, what you'd probably need to do is pass in a pointer to your ViewController
when you create the customObj
via a custom initializer, like so:
customObj *customObjInstance = [[customObj alloc] initWithController: self];
Be sure it makes sense for your customObj
to be tightly coupled to your controller if you do this.
You may have thrown me with you various mentions of class to instance to class, but if all you mean is that the controller needs to create objects which can call the controller (to create more of the same), that i can help you with.
I do this quite a bit, using - as john has mentioned - the delegation pattern:
Define a protocol for the calls from the object to the controller: callBack
// CallBackDelegate.h
#import <Foundation/Foundation.h>
@protocol CallBackDelegate<NSObject>
- (void)callBack;
@end
in the header file for the controller i import the protocol
#import "CallBackDelegate.h"
and specify that the controller implements this protocol:
@interface MyViewController : UIViewController < CallBackDelegate >
{ ...
meanwhile in the custom object class header also import the protocol
#import "CallBackDelegate.h"
and add an instance member which conforms to the protocol:
id<CallBackDelegate> delegate;
and a property, which must be assign not retain or you will have circular retains, the controller and the custom object would be retaining each other, which would mean they would never deallocate
@property(assign) id<CallBackDelegate> delegate;
(this member doesn't have to be called delegate)
Then make sure that when your view controller creates the custom object it sets it self as the delegate
customObject.delegate = self;
Then in your custom object you are safe to do this:
[ delegate callBack];
hope that helps, mat
if you search the apple sample code you'll find examples for something like:
[ delegate touchesEnded:touches withEvent:event];
of course if you want the method to pass you an id to the created object, the method doesn't need to return (void) as in my example
if you want to you can write an init method which takes the delegate as john also suggested
customObj *customObjInstance = [[customObj alloc] initWithController: self];
inside initWithController the controller you pass in would need to be assigned as the delegate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With