views:

55

answers:

2

Typically when creating an action from something like a button you have this

[button addTarget:self action:@selector(method:)...

I can guess if I want to send the action to another class but what if I want to send it to the parent? I thought super might work but that gives a warning.

EDIT:And super crashes the App

Cheers for any help.

+7  A: 
imaginaryboy
That makes sense and this might be due to a bad design decision but when I call [super method:sender]; the app crashes as it is sending the call to itself, not its parent. As a note I'm not using IB, its being created in Derived but should respond to Base
Rudiger
Maybe you can post some of your actual code, because it works just fine for me.
imaginaryboy
There is probably a bit too much code but what im doing is: I have a view controller which has 3 views. I've subclassed one of these views to implement a custom drawRect. As it's unique to the view I figured I would build the buttons that belong to that view there too but it should respond to the parent of the view. Hope that makes sense and I'm not using incorrect terms.
Rudiger
[button addTarget:self action:@selector(method:)... In this code, what is "self"? Is it the viewController or the view which you subclassed? Because, generally the action methods are defined in the viewControllers.
Raj
@Rudiger: Yeah, that clears up a LOT. You say you want it to go to the parent view, but you phrased the question as though you were trying to send a message to the superclass, not a totally different object. Also, Raj is quite right in that it is extremely unusual for a view to handle a button press. It should be handled by a controller, and ultimately it is the controller that is responsible for hooking itself up to receive the event from the button.
imaginaryboy
Yeah, thats why I thought it might be a bit of a bad design decision. Self in this case is the view, and yes I want the viewController to handle the action of the button pressed. I'm also building the buttons from an array of unknown length so I cant use properties. Think I might use the delegate pattern and be done with it. Vote up for your help though
Rudiger
A: 

The way I understand your question, you are trying to implement something that does exactly the same as the delegate pattern, but the way you are going around it you will not get any compiler hints or error if the super class does not implement the correct method for the target, it will simply crash when you run it.

It might seem as a good idea in regards of encapsulation, but I think it would be hard to debug if you reuse the view component elsewhere.

The only case I use the approach of trying to message something other than "self" is where a ViewController has X views and these views needs each other and needs to react to each others actions. Still, if I have implemented a viewController as delegate for the views, I would just let the viewController change the other views to whatever state they should go to.

(hope I didn't misunderstand the question)

RickiG
I think this is probably the best in my case.
Rudiger