views:

340

answers:

3

I've created a view controller and the associated view using the interface builder. I'm trying to call a function that I added to the UIView from the UIViewController. I'm not sure how to call that function though.

I've tried

 [self.view myFunction]

but that just makes my program crash.

A: 

How is the view getting initialized? Is it a custom view type? If your view is being initialized from a nib, make sure that the class on the view type is the class that has the method implemented.

Elfred
The view is something that I wrote custom code into the drawRect function and added another function myFunction to it. It being initialized by the nib. You lost me on the last part there.
Adam
+1  A: 

Did you declare your IB outlets and connect the method to it?

Your view (more accurately nib)'s file owner should be set to your viewController class.

Unless your calling drawing functions and methods you shouldn't call anything on the view. You just don't need to.

Edit: grammar corrections.

JoePasq
I know how to make the IBOutlet point at the UIView, but how do I point the outlet at the method?This is what I have in my viewcontroller. IBOutlet MyView* view1;Then to call the method I have [view1 myMethod];But this doesn't do anything.
Adam
Forgot to make the connection in interface builder. Works now, thanks!
Adam
A: 

It is probably crashing because UIView does not have a method named myFunction. In order to add myFunction to a UIView object, you need to subclass it.

I assume you have subclassed your UIView, and called it something like MyView. You also probably have subclassed your UIViewController and called it MyUIViewController. Therefore you may have:

@interface MyViewController : UIViewController {
    MyView *myView;
....
}

Now you can call [self.myView myFunction];

That's one way. Another way may be:

[(MyView *)self.view myFunction];

This depends on whether myView is the first view in the hierarchy (or should be set up in your NIB files).

mahboudz
I tried the last suggestion there and it crashes. Here's the console output:[HikerViewController] Refresh position pressed2009-10-19 20:58:47.082 UINavigationControllerWithToolbar[18268:207] *** -[UIView drawGraph::]: unrecognized selector sent to instance 0x3b0faa02009-10-19 20:58:47.083 UINavigationControllerWithToolbar[18268:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIView drawGraph::]: unrecognized selector sent to instance 0x3b0faa0'2009-10-19 20:58:47.085 UINavigationControllerWithToolbar[18268:207]
Adam