I have created one dynamic button in my application and I call the following method:
[btnactions addTarget:self
action:#selector(deleteview:)
forControlEvents:UIControlEventTouchDown];</b>
This deleteview method is used for delete the specific application view whenever I click the button.
Now, I want another method to call dynamically on button event
UIControlEventTouchDownRepeat
So how can I call two different events using different control events?
You can call addTarget again with UIControlEventTouchDownRepeat
From the docs (UIButton inherits from UIControl):
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
You may call this method multiple times, and you may specify multiple
target-action pairs for a particular event. The action message may
optionally include the sender and the event as parameters, in that
order.
Related
I have a Button1 which has IBAction. Also I set target and action for my button
- (void)setTarget:(id)target action:(SEL)action {
[self.Button1 addTarget:target action:action
forControlEvents:UIControlEventTouchUpInside];
}
So when I pressed the button firstly IBAction did what he should, and than action that I set to button. Is that order always be like that ?
If you are loading you view or view controller from a nib file then yes the pattern will always be the IBAction even first followed by the target you have added to the button.
In effect adding an IBAction in Interface Builder is really just telling IB to call ["UIControl" addTarget:"id" forControlEvents:"UIControlEvent"], and you can add multiple targets to a UIButton.
In effect your code will load everything from the NIB file first (if you are using initWithNib:named:), so this will call the addTarget function on the button first with the action you have specified in Interface Builder, then at some later point the setTarget function you have above will get called, which will add another target action to the button. A UIControls targets are stored in an array which is accessed in order and will trigger if control events are met in the order they were created in.
If you look in the header file for UIControl (the super class for UIButton) you will see that NSMutableArray* _targetActions is an array. So the order is guaranteed to fire like this unless you reorder this array after it is created at some point.
Suppose I have a button that I am adding to an annotation object in a mapview:
AnnotationButton* rightButton = [AnnotationButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
You will notice that the button calls the function showDetails when it is clicked.
Show details is defined as - (void)showDetails:(id)sender; and takes a sender. Is there a way to send more variables, or associate a different sender? The reason is that I want the button clicked to tell me which annotation is associated with that button. Consider the annotation to be some other object which is available during the context where the button is created.
I thought about subclassing the UIButton class, and then storing additional information within it, but that seems like a hack.
Any ideas?
If this button is being used for the rightCalloutAccessoryView or leftCalloutAccessoryView of a MKAnnotationView, your map's delegate should receive the message mapView:annotationView:calloutAccessoryControlTapped: when the button is tapped. This hands you the MKAnnotationView instance that was tapped, which has an annotation property to give you the corresponding annotation. You should make use of that instead of trying to use an action on the button directly.
No, there is no way to change what is sent to the action message. You can ask for two arguments, but they will be the button and the event that triggered it. To get what you want, you have two options (that I can think of now).
Use the button's tag property. You can give each button a unique tag which identifiies the annotation, such as the index of the annotation in an array. Then it is easy to get the annotation in your showDetails: method.
Subclass UIButton. There is nothing wrong with adding functionality to built in objects. All you need to add is a property to hold some object. Bonus: If you use a generic id type for the property and give it a generic name, such as representedObject, you can use it in other projects in the future too.
from Anomie Use objc_setAssociatedObject to add a value to the buttons without subclassing. You will probably want to add a category to UIButton to make it easier to use.
Now that I know that NSEvent does not exist on ios - what do I need to do to capture an event like touchUpInside and cause it to call my method to handle it, WITHOUT Interface Builder?
I think this like asking how do I link an event to an outlet without IB...
I know it can be done, but I can't find anything that shows how - except Mac-only examples that use NSEvent.
Take a look at this method in UIControl:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
Each of the different event types has a constant, such as UIControlEventTouchUpInside. Here's an example of it being used:
[addButton addTarget:self action:#selector(increment:) forControlEvents:UIControlEventTouchDown];
Look here for the values of controlEvents, listed in the Constants section.
When I create UITextField inside Interface Builder, I can access Events tab for it, which has events like Value changed, Touch cancel, Touch drag, etc. I can assign my own methods to every of those events. How can I do the same, when I create UITextField programmatically with alloc?
Refer to Apple documentation for UIControl. After initializing your textField, call addTarget:action:forControlEvents:
example for the touch event ending an edit session
[textField addTarget:self action:#selector(handleTouchValueChanged:) forControlEvents: UIControlEventEditingDidEnd]
Instead of UIControlEventValueChanged, you should use UIControlEventEditingChanged:
[_titleTextField addTarget:self action:#selector(handleTitleValueChanged:) forControlEvents:UIControlEventEditingChanged];
UIControlEventEditingChanged fires whenever user changes value [synchronous with typing or keyup]
which could cause extra hits to your data handler routine, if you're saving values based on that event, expecting it to be some kind of final value from user input...
How can I determine whether my button's event is Touch Down?
I want to do a function like this:
if(users click on touchdown event)
{
NSLog(#"a");
}
else if(users click on touchupinside event)
{
NSLog(#"b");
}
Eather you set two different IBAction methods in the InterfaceBuilder or you set two different targets via:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
in your code while creating the button.
You "find out" by letting the button tell you when the event happens.
Add a method (or methods) like this:
- (IBAction)myButtonClick:(id)sender;
In Interface Builder, attach the method(s) to the events you're interested in.
You create a separate method for each type of event if you want different behavior for a TouchDown as opposed to TouchUpInside.
you attach each unique event to its own IBAction