Reputation: 573
My problem is:
I've created a project object. In this project object I need to call a method in a ViewController.
The method I need to call draws some objects in the ViewController principal view using a modified -
(void)drawRect:(CGRect)rect
method.
The only thing I need to do is call this method on the actual instance (the instance that is created when the app starts) of the ViewController from a method in the project class.
The method created (to draw the objects) works. I tested it by calling it from the ViewDidLoad
method of the ViewController. In the project method I tried for example this code:
-(void)drawProject {
UIStoryboard *mainStoryboard =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
IYViewController *projectViewController = [[IYViewController alloc] init];
projectViewController = (IYViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"project"];
[projectViewController drawProject];
}
I named the principal, the target ViewController "project" in interface builder.
The problem is that anyway the ViewController instance that I create is not the instance of IYViewController that is displayed at runtime.
I didn't find any real good solution that works for me, so please help! Maybe the solution is create a delegate of a class but I didn't really understand that so please if this is the right solution help me code it!
Upvotes: 0
Views: 84
Reputation: 14068
Yes, the instance that you alloc and init is not the one that you load from the storybaoard. There is no need to alloc and init it. If you don't ARC then you even created a memory leak there.
-(void)drawProject {
UIStoryboard *mainStoryboard =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
IYViewController *projectViewController;
projectViewController = (IYViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"project"];
[projectViewController drawProject];
}
Well, this will not solve the issue. (If comments could be formatted properly then I added a comment rather than an answer). Your real problem is somewhere else and I fear that you may not yet know how to explain it so that we understand it. Give it a try. Explain in a bit more detail.
Are you sure that your drawProject is really executed? To which class belongs the drawProject Method that we are looking at?
What exactly do you do in drawRect
.
Besides, drawRect is a method of a view, not a view controller. If you implemented that for a view controller then
it may not be called at all.
Upvotes: 1