Reputation: 569
I am having trouble using a protocol to get some data from another class. I can't see how to set the delegate in a class that doesn't segue to the MVC that needs the data. I create the protocol in the MVC and implement the method(s) in some arbitrary class that contains the data I need. But I can't see how to refer back to the delegator MVC to set the delegate if there is no reference to the delegator MVC, like when you use segue.destinationViewController.
Upvotes: 0
Views: 138
Reputation: 104082
If MyViewController can create the instance of SomeDataClass, then you set the delegate there. If there is no connection between the controllers, then you might use an NSNotification instead. That is a completely anonymous way to connect instances -- you send out a notification, and any class that registers for that notification can get it.
Upvotes: 1
Reputation: 6806
Something like this?
@implementation MyViewController {
// keep a pointer to the data supplier class as long as this object exists
// so that it will continue to exist and send me delegate callbacks
SomeDataClass *myInstanceOfSomeDataClass; // instance variable to point to my data supplier
}
// ...
- (void)updateMyView {
if (myInstanceOfSomeDataClass == NULL) // I haven't created an instance yet
myInstanceOfSomeDataClass = [[SomeDataClass alloc] init];
SomeType *results;
if (instantResultsAreAvailable)
results = [myInstanceOfSomeDataClass getResults];
if (resultsAreOnlyAvailableFromDelegateCallback)
myInstanceOfSomeDataClass.delegate = self;
}
- (void) delegateCallbackMethod {
//...
}
@end
Upvotes: 1
Reputation: 2263
you want to pass data from one from viewcontroller to another class? just go through this Passing Data between View Controllers
Upvotes: 0