How to call button Clicked method from somewhere else - iphone

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];

Related

how to pass data through #selector upon custom button click?

I'm making a button via code.
I have the following line of code to trigger a method when the button is clicked:
[imagesButton addTarget:self action:#selector(photoClicked:) forControlEvents:UIControlEventTouchDown ];
The problem is that I can't pass data to the method through the #selector;
If the button had a background image name "background.png" how would I go about sending the name of the background image to the method when the button is clicked?
This all HAS to be through code.
Thanks!
-Shredder
there must be a way to comment on an answer, but I don't know what it is. Anyway, Gobot above me forgot to write (id) before sender in the method declaration. Otherwise Gobot's example is ok.
Well if you're trying to change a property of the button whose sending the message, your selector should have a parameter of sender, which is a pointer to the object that called it, which is your button in this case. For example:
- (void)photoClicked:(id)sender {
UIImage bg = [sender currentBackgroundImage]
}

iPhone: IBAction vs Selector

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.

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.

Calling a method which is also used by UIButton?

I have a small iPhone app which I've created a button with some functionality in. My question is, how can I call this button without actually pressing it?
Any help would be appreciated!
Thanks
If you want to activate whatever target a button is wired to, you can call:
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
(TouchUpInside is the event you'd normally wire a button action to). This way if other targets are added or changed for any button (say for debugging) you don't have to alter your code.
This method is on UIControl which UIButton inherits from, which is why you might have overlooked it at first glance...
Have your button event call a function. You can also manually call the function yourself.
Example:
- (void) btnFunction {
NSLog (#"test");
}
...
UIButton *btn1 = [UIButton buttonWithType:UIButtonRoundedRect];
// other code to set up button goes here
[btn1 addTarget:self action:#selector(btnFunction) forControlEvents:UIControlEventTouchUpInside];
You can also call the function yourself:
[self btnFunction];
Your button shouldn't have functionality, it should just send a message to its target (or call a method, or call a function...).
You're free to send that message to that target yourself.
e.g. Your button's target outlet is connected to an IBAction on your controller. That IBAction is just a method of the form:
- (void) doSomething:(id)sender
In your own code do:
[controller doSomething:self];
It's exactly the same as having your button do it.

iPhone table view - some button/action questions

I created a table view that is populated with a custom UITableViewCell (like this). Each of the cells contains two UIButtons. I assign the action to the button like this:
[decreaseButton addTarget:self action:#selector(decrease) forControlEvents:UIControlEventTouchUpInside];
Is this the right way?
Anyway, it works, but in my "decrease" method I need to know in which of my 18 table view rows the button was pressed. indexPath.row doesn't work outside the cellForRowAtIndexPath method, of course.
Can someone explain me how to do this?
Thanks a lot in advance!
iYassin
You can do this in two ways.
Inspecting the Event Sender
Change your decrease method from:
- (void)decrease;
to:
- (void)decrease:(id)sender;
That way when decrease is called, you'll be given a reference to the button that had the touch up inside event.
Define the decrease Method Closer to the Information
Another solution would be to have a different target instance for each button (for example, implement the decrease function as part of the custom cell). That way you know the button that was touched was the one for the current cell.
The way i solved this is I keep track of data i might need inside my custom cell object. And the button is connected not to the external receiver but the cell it's self which in-turn knows how to call the real receiver of the action.
I make my cell with something like:
cell = [[MyTableViewCell alloc] initWithStyle:style
reuseIdentifier:CellIdentifier];
And I have a setup method so i can re-init a cell when I dequeue it:
[cell setupMyCellWithContext:objectID
target:[[UIApplication sharedApplication] delegate]
action:#selector(someAction)];
so inside your cell class you use the action and target that was sent in the setup method to call the true target:
- (void)doAction:(id)sender {
if ([target respondsToSelector:action]) {
[target performSelector:action withObject:objectID afterDelay:0];
}
}
So when your user taps the button, the os calls [cell doAction:], which calls the target and action selector you set up before hand with the correct context object.