iPhone, how to fire a button event in programmatic

2020-08-01 04:51发布

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

标签: iphone
2条回答
看我几分像从前
2楼-- · 2020-08-01 05:30

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.

查看更多
Luminary・发光体
3楼-- · 2020-08-01 05:48

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.

查看更多
登录 后发表回答