1

I want fire a button event(or maybe method) in code,the button was generate by iPhone SDK , I can not update it, so I add the action to the button, the code likes as follows:

[theButton addTarget:self action:@selector(onButtonEvent:) 
       forControlEvents:UIControlEventAllTouchEvents];

 -(void) onButtonEvent:(id)sender
{   
    AppTrace2(self, @"onButtonEvent", sender);   
}

and when I tap the button, the app will execute a function that was produce by iPhone sdk, also the new method 'onButtonEvent' will be executed, and then I try to add some code as follows:

[theButton  sendActionsForControlEvents: UIControlEventTouchDown];
[theButton  sendActionsForControlEvents: UIControlEventTouchDownRepeat];
[theButton  sendActionsForControlEvents: UIControlEventTouchDragInside];
[theButton  sendActionsForControlEvents: UIControlEventTouchDragOutside];

[theButton  sendActionsForControlEvents: UIControlEventTouchDragEnter];
[theButton  sendActionsForControlEvents: UIControlEventTouchDragExit];
[theButton  sendActionsForControlEvents: UIControlEventTouchDragInside];
[theButton  sendActionsForControlEvents: UIControlEventTouchDragOutside];


[theButton  sendActionsForControlEvents: UIControlEventTouchUpInside];
[theButton  sendActionsForControlEvents:  UIControlEventTouchUpOutside];
[theButton sendActionsForControlEvents:UIControlEventTouchDown];
[theButton sendActionsForControlEvents:UIControlEventAllEvents];

but it does not call the original functions which was produced by iphone sdk, although the new method 'onButtonEvent' also was executed when above sendActionsForControlEvents executed .... my goal is to invoke the original function ... sdk does not provide tip on this , so I have to simulate the tap event to invoke the function to executed ....

any one can give a tip on this problem ?

Thanks for your time!

Regards

Vladimir
  • 170,431
  • 36
  • 387
  • 313
iXcoder
  • 1,564
  • 4
  • 20
  • 41

2 Answers2

1

What do you mean the button was generated by the SDK? Do you mean that the button is part of a user interface not created by you, such as the UIImagePickerController or the MPMoviePlayer interfaces?

If you're finding yourself trying to add behaviour to provided user interfaces, then perhaps you need to rethink your design? User interfaces provided by API's in the SDK shouldn't be modified, or extended. You should create your own interfaces, that can look similar if you want them to.

I'm not sure if you're planning on submitting this to the App Store when you're done, but know that there have been cases in the past where automating events, like button presses and touch events have resulted in apps being rejected from the App Store.

Some notable cases were apps using the popular Three20 library, that simulated touch events for testing purposes. Simply the fact that the simulated touch events were left in the code (even if they weren't being used) was reason enough for apple to reject apps using Three20.

Jasarien
  • 58,279
  • 31
  • 157
  • 188
  • +1 Apple will definitely shoot down an app that hijacks the interface and makes it possible for the app to override a user's choices in a standard control. – TechZen Feb 09 '10 at 16:18
  • yes, I want extends the UIImagePickerController, I think that does not conflict with the Apple Rule .... well, we can ignore the issue , I just want know how to fire the event , I meaning is how to invoke the original event of the button or the method of the button...... – iXcoder Feb 09 '10 at 16:23
  • You can't modify the interface of the UIImagePickerController. There is no supported way of automating control events such as button presses or touch events. My advice would be to create a view that is similar to the UIImagePickerController and implement your functionality on that. – Jasarien Feb 09 '10 at 19:50
0

You should call allTargets on the button and see if it returns the target you want. If it does not then none of the public API methods will let you hit that target and you are wasting your time.

Apple hardwires some of its controls and developers cannot override that.

Edit01:

UIThreePartButton is a private class. If you use its methods your app will be rejected by the app store.

However, I did find a source that claims to be the header for it and its superclasses. You can see if you can find anything.

http://ericasadun.com/iPhoneDocs/_u_i_three_part_button_8h-source.html

Even if you don't care about rejection, if you ultimate goal is to access or override something in the API, I think your wasting your time. It's unlikely to work and it will be fragile if it does.

There is probably another way to do what you want.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • .... the methods also does not work... the button super class is UIThreePartButton , I think that it also is base UIButton, I do not think that is stupid idea, because the method 'sendActionsForControlEvents' is works , when I call it, the new method which I add to the button is executed...... – iXcoder Feb 09 '10 at 16:36
  • There is no guarantee that Apple's internal methods respond to the public API and vice versa. – TechZen Feb 09 '10 at 23:02