I am looking for the right way to write my own interface object.
Say, I want an image that can be double-taped.
@ Interface doubletipput view: UIView {UILabel * text; UIImage * button; UIImage * button_selected; BOOL selected; } // Find the tapcket == 2 - (zero) touches the brinze: (NSSET *) touches the event: (UIEvent *) event;
This works fine - the button receives events and can detect double tap.
My question is how to handle tasks clearly. I have tried both approaches which are adding references to the original object and the delegation.
Passing the reference of the parent object is quite straightforward ...
@interface DoubleTapButtonView: UIView {UILabel * text; UIImage * button; UIImage * button_selected; BOOL selected; Main view controller * Guardian view; // added} @ property (non-nominal, retained) Main view controller * Guardian view; // Added // will be assigned during the original view init ... - (id) initWithFrame: (cgcc) View frame controller: (MainViewController *) aController; - (ID) touches the brinze: (NSSET *) touches with the event: (Uiivent *) event;
However, this will prevent my DoubleTapButtonView class from being easily added to other views and view controllers.
The delegation adds some extra abstract to the code, but it allows me to use DoubleTapButtonView in any category that fits the delegate interface.
@interface DoubleTapButtonView: UIView {UILabel * text; UIImage * button; UIImage * button_selected; BOOL selected; ID & amp; Lt; DoubleTap ViewDeliget & gt; Representative; } @protecti (nanatomic, assign) id & lt; DoubleTypeviewLet & gt; Representative; @protocol DoubleTapViewDelegate & lt; NSObject & gt; @required - (zero) doubletap received: (doubletview *) target;
The correct way to design these objects is that the button only knows that it was duplicated, and then tells the delegate how to handle this information.
I am thinking that there are other ways to think about this problem? I saw that UIButton uses UIController and addTarget: to manage sending events, is it leverage to write my own interface to my system?
Update: Another technique uses the NSNotification Center to create supervisors for various events, and then make the event in the button.
// Listen for the event in the parent object (view controller, etc.) [[NSNetification Center Default Center] addObserver: Self Selector: @Sylector (DoubleProp) Name: @ "DoubleTap Notification" Object : Zero]; // In DoubleTapButton, a notification is closed ... [[NSNotificationCenter defaultCenter] postNotificationName: @ "DoubleTaponotification" object: Self];
What are the disadvantages of this approach? Less compilation-time investigations with events flying out of the object structure, and potential spaghetti code? (And even if the namespace collision if two developers use the same event name?)
Representative is definitely the way to go here.
Comments
Post a Comment