Can we use other object than self for button’s target? - iphone

Instead of self can we use another object with addTarget in this code:
[nextMonthButton addTarget:self
action:#selector(showFollowingMonth)
forControlEvents:UIControlEventTouchUpInside];
If yes what are those objects?

Yes.
The other object just has to respond to showFollowingMonth so typically it will be a custom class you have created.

It can be any object, but the button does not retain that object when you add it as a target, so your object will have to last around for as long as the button is around, and know to release itself when the button is deallocated. This is usually hard to do, unless that object is the controller of a view containing the button, or some object that belongs to such a controller.

It can be pretty much any object that is alive and is able to respond to the given selector.
However, for actions with a visual feedback where you need to manipulate the views, staying in the controller will be the best strategy anyway.

Related

CCMenuItem doesn't respond subclassing and adding cctouch methods to CCMenu

I'm making a menu, and I want one of the buttons to respond when user touch it down, so I made a subclass of CCMenu in order to add cctouchbegan method And manage it there.
The problem is that I can make it to respond both things (menuItem & cctouch), is this normal?
is there a way to force it to do both things?
Thank you in advance, let me know if you need me to put some of the code here
You should look at CCMenu.m -- it already implements ccTouchBegan and sets a selected flag on CCMenuItems. Your approach is probably not working because you're stealing the messages from your parent class.
Your subclass should call [super ccTouchBegan...] first, then check the selected state of the CCMenuItems to determine which button to change visually.
EDIT:
Or, even easier! -- Subclass the appropriate CCMenuItem subclass (e.g. CCMenuItemSprite) and overload the selected method from it's default to include your visual alterations:
-(void) selected
{
[super selected];
//call method to update visuals here
}
You can simple create your own CCLayer subclass, set it's isTouchEnabled property to YES and implement any touch logic you want

iOS attaching callbacks and associating different sender possible?

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.

What does obj.delegate=self mean?

What does it actually mean to set the delegate of a textfield?
For example: txtField.delegate = self
"In short, that you are receiving calls from the txtField. You are setting the object 'self' as the delegate for txtField."
"That means that your 'txtField' will receive events from itself
These two answers essentially mean the same thing. But seemingly contradictory. But the first makes more sense to me. I can see why a beginner gets confused, I've been there!
Basically one is the caller one is the receiver Think of it as a chef in a kitchen call his assistant to cut up some onions. In this particular case, txtField is the chef, "self" is the assistant. txtField orders self "Do this, this and this!" Like it or not the assistant has to oblige cuz he has wife and kids to feed. :)
It means that self will be the recipient of certain method calls that are made in response to actions on the text field.
In short, that you are receiving calls from the txtField. You are setting the object 'self' as the delegate for txtField.
Delegating is a programming pattern that is widely used in Objective-C.
The basic idea is let an object delegate some tasks to another object. For example, your UITextField object delegate some tasks to your view controller. In this case, your UITextField object becomes a delegating object, and the view controller the delegate of the UITextField object. The delegating object sends certain messages to its delegate in order to get necessary information, or to notify certain events, etc.
That means that your 'txtField' will receive events from itself (kind of a weird example, maybe a larger source code section could be provided?)
For some of its methods, the textfield (any object in a class using the delegation pattern) is going to try to call some other object to so that that object can customize some of the textfield's behaviors. The object that the textfield will try call is called it's delegate. The delegate is initially set to nil, so, by default, no customization happens.
If a class has a line of code like: textfield.delegate = self; then it says that this object in this class wants to get called to handle the textfield's customization for certain of the textfield's defined delegate methods.
It means the actual class where 'txtField.delegate =self' is called will receive callsbacks from events. This is often a convenient way to do things.

Can I make a UIControl take ownership of "disposable" event targets?

In my view controller I'm programmatically creating N buttons in my UIView. Each button needs to do something different, so I've made a custom handler class, which I create N instances of, each initialized with custom data. I create them in the body of a for loop and add them as targets, after which I don't need them anymore.
However, because UIControls don't take ownership of their event targets, I need to hang on to these custom handlers myself (in the controller), introducing an extra ivar and the necessary release code. Is there a more elegant way to handle this problem?
Add those custom handlers to an NSArray that you retain in your controller (whatever controller is creating the buttons). Adding an object to an array retains it, so you will know that it is always around.
Associative references?

creating a reference for use in identifiying if a button was clicked

How is it possible create a reference for an uibutton to check if it was pressed and do something like make the value for that button true in order to be able to use that value later on. I heard of ivars but i havent seen any documentation on how to use them and im not sure if they would work for my ibaction uibutton...
This is really a fundamental question and a full explanation will take along time.
Basically, you need to understand the concept of MVC (Model-View-Controller). In this case, the "View" will be your UIButton created in Interface Builder. It needs to have a target/action setup to point to some "Controller". This will be a custom class you design that performs some action when the UIButton is released. This controller can also track finger up/down/move/drag and what not. This Controller will store the fact the button was pressed in a "Model" class.
Here is some more reading:
http://developer.apple.com/iphone/library/documentation/general/conceptual/devpedia-cocoacore/MVC.html
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html
In the simplest cases, this can be done with Core Data and IB without writing any code and simply making connections.