iPhone: IBAction vs Selector - iphone

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.

Related

How to make multiple UIButtons connect to the same IBAction?

I need several UIButtons to all connect to the same IBAction so the buttons can all do the same thing without having to copy and paste the code. Please tell me if there is a way to do this! It might be right under my nose, but I can't find it. Thanks!
Simply add the same target and selector to each button:
[button1 addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
// etc.
For this You need to use set IBOutlet for Each Button or Set tag for each button if you are using Outlet then used this code .h
#interface RootViewController_Phone : UIViewController
{
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
}
- (IBAction)buttonPressed:(id)sender;
-(void)CallButtonsMethod;
#end
Now in .m file
- (IBAction)buttonPressed:(id)sender{
if([sender isEqual:btn1])
{
[self CallButtonsMethod];
}
if([sender isEqual:btn2])
{
[self CallButtonsMethod];
}
}
-(void)CallButtonsMethod
{
//your Code
}
Use IBOutletCollections.
Here is tutorial for that:
http://useyourloaf.com/blog/2011/03/28/interface-builder-outlet-collections.html
I've found that I'm usually not able to hook up multiple buttons (or even a single button, for that matter) in IB to an already existing IBAction in code, by Ctrl-dragging from the button to the IBAction in the m file. Xcode tries to create a new action in the dragged-to file, but won't connect to an existing action. [This is true for Xcode 4.6.1 and several previous (maybe all) versions.]
The approach in the accepted answer in the following link works if the IBAction is in the view controller which is the "File Owner" for the view containing the button:
Connect object in .xib to existing IBAction
However if you want your IBAction to be in the view's own class, rather than the view controller's class, then the following approach works:
Drag only one button onto the canvas in IB first. Control-drag from
the button to the view's .m file.
Drop the dragged line in any vacant space in the implementation section of the code.
Define a new IBAction in the little popup dialogue box. So now you have one
button hooked up to an IBAction.
Now instead of dragging more buttons from the object library, hold
down the Option key and drag out a new button from the original one.
This new button will come hooked up to the same IBActions as the
original one. (If you need to create lots and lots of buttons and
this seems tedious, you can select a bunch of already created ones simultaneously and
Option-drag to create as many more in a single go.)

How to call button Clicked method from somewhere else

I am developing an application in which I want to select the button with some given tag. For example tag=12. So, what I want is that when the button with tag 12 is selected the button clicked method also gets called.
One more thing I want to ask, if I write
button.selected=YES
will the button method automatically get called? If not then how to call the button method from somewhere else where I do not have sender (button properties) value?
The only thing I have is the button tag.
Please help and ask me for any clarification.
Create a temporary UIButton and give the tag of button you want to call.
For eg. call button action method with temporary button of tag 12
UIButton *button = [[UIButton alloc] init];
button.tag = 12;
[self buttonTapped:button];
Hope it helps. Comment down for any query.
If you're setting button's property to Selected manually , then for it's click event you will have to call it manually , When you set button Selected like :
[self buttonCick];
iUser is close. You'll want to call the method you've linked to your button manually.
[self buttonClick:nil]
will work, if you're calling the buttonClick method from an object of the same class that contains the buttonClick method. Otherwise, you'll need to keep a reference to the object (perhaps a controller) containing the buttonClick method and use that instead of self.
[self.controller buttonClick:nil];

How to see activities while performing actions?

In my iPhone app,
I am working with XIB file.
There is a segmented control,
But there is no IBAction associate with any field,
Whenever I am selecting or touching an segment which method from my code will be called ?
Can I trace or see it in the xcode.
Actually I am looking through some complex code and could not catch the methods by breakpoints ....
Like this Question of stackoverflow.
How to print or see method call stack in xcode?
If you want to see the target method you can right click the controller and see the the method associated with segmented control. If you want to add add some target method with segment controller then do this -
Right click the controller
drag the plus button of desired action(like valueChanged ) to .h file,one popup will come
enter the action name click connect
now go to .m file and write your logic in action
First of all, you do not associate any IBAction with the segmented control's "fields". You associate an IBAction with the control itself.
So, to get you started:
Create an IBAction like :
-(IBAction)segmentChanged:(UISegmentedControl *)sender;
Bind that action with the "Value Changed" event of your UISegmentedControl object
Now every time the segment changes, this method will be fired.
To get the selected index use:
uint selectedIndex = [sender selectedSegmentIndex];

Code for Reset Button in iPhone

I have a button called Reset in my iPhone application. It is for resetting purposes. I called viewDidLoad() method for this. Is it right?
How to reset a page in iPhone?
how to write code for this? Any help would be appreciated.
You should not call -viewDidLoad in your own code. It gets called on view controllers (VCs) when their view has just finished loading.
To return a VC to its original state depends largely on the specifics of the situation, but you could probably either set its properties and whatnot back to their original values or you could alloc and init a new VC, remove the old VC's view from the view hierarchy and add the new VC's view.
Alternately, you could just implement a -resetToOriginalState method on your VC.
resetToOriginalState is not a inbuilt method, you have to write this method on your own and instead of calling viewDidLoad call this method 'resetToOriginalState:' when the respective button is clicked. If you are creating button programmatically you can set the target/action of that button like this:
resetBtn is the instance of the UIButton:
UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake(x,y,w,h)];
[resetBtn addTarget:self action:#selector(resetToOriginalState:) forControlEvents:UIControlEventTouchUpInside];
- (void)resetToOriginalState:(id)sender {
//Do your stuff here
}
If your using Interface Builder connect the action method (resetToOriginalState) to the button.

UIBarButtonItem ignoring just about every action, ever

I have a toolbar, in which is placed a UIBarButtonItem. The selector is targeted at a custom view of mine; a method with this signature:
-(IBAction)pop{code}
However, clicking it does not cause any action to occur. The buttonitem doesn't appear to respond to the click either, it just stays gray.
Linking a UIButton's TouchUpInside event to the pop method is fine, it operates the method and displays the popover. But as soon as I connect the BarButtonItem's selector to it instead, it stops responding.
Make sure the selector has no colon after it - #selector(pop). If you use #selector(pop:) it expects a (void)pop:(id)sender { ... } function.