I have three buttons with different actions.Now I don't want to create three IBAction to my buttons.In single IBAction Method can i write the actions for those three buttons.
I am new to Xcode,Can anyone help me to do this...
Thanks in Advance....
try like this
in . h file
#property (strong, nonatomic) IBOutlet UIButton *yourbutton;
in .m
#synthesize yourbutton;
- (IBAction)yourClicked:(id)sender {
UIButton *resultebutton= (UIButton*)sender;
NSString *buttontitle=resultButton.currentTitle;
if ([buttontitle isEqual:#"firstBtitle"]) {
// perform your 1st button action
//call your method
}
else if ([buttontitle isEqual:#"secondBtitle"]) {
// perform your 2nd button action
}
else if ([buttontitle isEqual:#"thirdBtitle"]) {
// perform your 3rd button action
}
}
Assign tag for buttons, and in IBAction method, check Button tag and do action, according to tag of button.
Please correct me, if I get you wrong:
You have three buttons and you want them to trigger the same IBAction. The IBAction itself decides what to do based on which button calls it.
This sounds to me like a perfect example for the "sender" parameter.
Create something like this:
- (IBAction)doSomeAction:(id)sender
{
if ([sender isEqual:self.buttonOne]) {
NSLog(#"ButtonOne");
} else if ([sender isEqual:self.buttonTwo]) {
NSLog(#"ButtonTwo");
} else if ([sender isEqual:self.buttonThree]) {
NSLog(#"ButtonThree");
}
}
With the sender you can identify the button, which calls this method. This way, you can avoid handle tags which can be very annoying to use.
Make sure you connect all three buttons to this action - take a look at the connections inspector. This is very important and a common source for errors. If you remove any connection to an outlet or an IBAction, also check, if this connection ist remove in the Storyboard-object.
If everything is in place just compare the sender with the outlets of the buttons.
Step 1:
Assign your all three buttons different tag in storyboard/XIB,
For ex. firstButton with tag=1, secondButton with tag=2 and thirdButton with tag=3
Step 2:
Define your method like this and bind all your buttons with this method
- (IBAction)buttonAction:(UIButton *)sender
{
if (sender.tag==1) {
NSLog(#"First Button");
} else if (sender.tag==2) {
NSLog(#"Second Button");
} else if (sender.tag==3) {
NSLog(#"Third Button");
}
}
And your work is done.
Related
I have a UITableView that i would like to hide until the user taps the button searchButtonTapped. (I'm also using this button as an IBAction.)
Originally i'm hiding the table view as you see in the viewDidLoad, and i wanna show it after the button was tapped, but it does not shown up after i tap the search button. Do i missed something? For me, it seems it should be work properly, after the button was tapped i refresh the table view.
my .h file
#property (weak, nonatomic) IBOutlet UIButton *searchButtonTapped;
- (IBAction)searchButton:(id)sender;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.hidden = YES;
}
- (void)buttonTapped:(id)sender {
if (sender == self.searchButtonTapped) {
self.tableView.hidden = NO;
[self.tableView reloadData];
}
}
- (IBAction)searchButton:(id)sender {
[self searchSetup];
}
It's impossible to tell from the little bit of code that you posted. Add NSLog statements in your buttonTapped method that show entering the method, entering the if statement, the value of searchButtonTapped, and the value of self.tableView.
Then you can tell if the method is getting called, if the if statement is evaluating as true, and if the table view is non-nil. One of those things is likely to be the cause of your problem.
I'm guessing that the if statement is wrong. what type is the property self.searchButtonTapped? Post the code that declares that property.
Based on the name I would guess that searchButtonTapped is a boolean?
you have declared only one IBAction, which is for the method searchButton.
This method call the searchSetup´s method. What is the purpose of it?
- (IBAction)searchButton:(id)sender {
[self searchSetup];
}
So you must have another IBAction for buttonTapped method witch is currently a "void" method and not a IBAction. Or you make that connection from the storyBoard, or you must declare it programaticly like:
[self.searchButtonTapped addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]
I am trying to save the order in which the buttons are pressed, and then replay that order and run the actions assigned to the buttons in the order they were originally pressed? Can anyone please help me with this?
Each UIControl element has a tag which you can use to be able to identify between the various buttons that are going to be tapped. As each button is tapped, the method (selector) associated with that button will be called (you can even have a single selector be called for all the buttons and differentiate between them via their tags).
As each button is tapped, keep track of which button is tapped by adding the tag of each button to a queue (or in Objective-C: NSMutableArray). Then to replay the actions you can merely read the tag values from the queue and call the corresponding selector.
An example to illustrate:
#property (nonatomic, strong) NSMutableArray *taskArray;
// in your init or viewDidLoad:
_taskArray = [NSMutableArray new];
// in the selector that is called by *all* buttons
-(IBAction) buttonTapped:(id)sender {
[_taskArray addObject:[NSNumber numberWithInteger:sender.tag]];
[self executeActionWithTag:sender.tag];
}
-(void) executeActionWithTag:(NSUInteger)tag {
if(tag == 1) {
// perform specific action 1 ...
} else if (tag == 2) {
// perform specific action 2 ...
}
// ...
}
-(void) replayButtonActions {
for (NSNumber *tag in _taskArray) {
[self executeActionWithTag:[tag integerValue]];
}
}
I am trying to find the best approach to doing this. I have 5 custom buttons on a view controller and I am trying to have the button stay highlighted if it is clicked. I know how to do this but I am trying to only allow 1 button to be highlighted at a time. So if a user clicks a button and highlights it, but clicks another, then the most recent button clicked will stay highlighted and the previous will unhighlight. What would be the best way to accomplish this?
You should keep a reference to all your buttons (for example, if you use IB, have links in your code like #property (nonatomic, strong) IBOutlet UIButton *button1; for all your buttons).
Then link all your buttons to the same method for a press on the button. I'll call it buttonPressed.
Impement it like this :
- (IBAction)buttonPressed:(id)sender {
UIButton *buttonPressed = (UIButton*)sender;
NSArray *buttons = [NSArray arrayWithObjects:_button1, _button2, _button3, nil];
bool buttonIsHighlighted = NO;
// Check if a button is already highlighted
for (UIButton *button in buttons) {
if (button.highlighted) {
buttonIsHighlighted = YES;
}
}
// If a button is highlighted, un-highlight all except the one pressed
// If no button is highlighted, just highlight the right one
if (buttonIsHighlighted) {
for (UIButton *button in buttons) {
if (buttonPressed == button) {
buttonIsHighlighted = YES;
} else {
button.highlighted = NO;
}
}
} else {
buttonPressed.highlighted = YES;
}
}
I can't test this code but I'm pretty sure it should work. Let me know if something's wrong.
Solution 1:
Put your buttons in an NSArray and when user clicks on a button check if another is highlighted. If YES, unhighlight it and highlight the one was pressed. If NO, highlight directly the one pressed.
Solution 2:
You can save the highlighted button in a global variable declared in #interface or in a #property. When users click the new one unhighlight the previous.
I have an edit button, that I obtained through self.editButtonItem and I have set it as self.navigationItem.leftBarButtonItem, such that when it is pressed, a UITableView begins editing and it turns into a "Done" button. When pressed again the view stops editing and the button returns to its normal state.
I would also like an "add" button to turn into a "Clear" button with a different action linked to it when the edit button is pressed.
(Much like in the iPhone "Phone" app's favourites tab, just that the plus button turns into a clear button when the Edit button is pressed).
I would really like to obtain the edit action and style etc in this way (self.editButtonItem), but I would also like to have an extra selector linked to the edit button.
How should I go about doing this? I have tried to create a category for UIBarButtonItem, but I don't really know what I should do with that.
Thanks.
To create a button whose title can change, you can do the following:
Define an ivar for the button:
UIBarButtonItem *_btnAddClear;
In viewDidLoad:
_btnAddClear = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonItemStyleBordered target:self action:#selector(addClearAction:)];
_btnAddClear.possibleTitles = [NSSet setWithObjects:#"Add", #"Clear", nil];
Since you want this button's title to change when the Edit/Done button is tapped, you can add code like the following:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
_btnAddClear.title = editing ? #"Clear" : #"All";
}
And lastly, the button handler:
- (void)addClearAction:(UIBarButtonItem *)button {
if (self.editing) {
// perform "clear" action
} else {
// perform "add" action
}
}
Give tag of UIBarButton such like 101;
and in BarButton Method write following
-(void)barButtonMethod
{
UIBarButtonItem * myButton = (UIBarButtonItem *) sender;
if(sender.tag == 101)
{
yourBtn.tag = 102;
// Write Your first action method such like
[self ActionMethod1];
}
else
{
yourBtn.tag = 101;
// Write Your second action method such like
[self ActionMethod2];
}
}
You don't really need a new action for the editButtonItem.
There is a property that tracks if the UIViewController is in editing state.
#property(nonatomic, getter=isEditing) BOOL editing
In order to do what you want, you can implement the following method in your UITableViewController:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated]
//Do your thing
}
I have an UIButton array like this:
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *btn_Impact_Collection;
and I have this function:
- (IBAction)impactAction:(id)sender;
In the XIB file I have nine button, each button is connected to btn_Impact_Collection Array with the Referencing Outlet Collection. Moreover the Touch_inside property of each button is connected to the function ImpactAction.
Now, when a button is clicked the ImpactAction function is called, but inside this function, how can i know which button is pressed?
Thanks in advance for the answer!
Cast sender to UIButton class, and that will give you the instance of the clicked button. I don't have Xcode with me but something like:
if ([sender isMemberOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)sender;
// Then you can reference the title or a tag of the clicked button to do some further conditional logic if you want.
if([btn.currentTitle isEqualToString:#"title of button"])
{
// do something.
}
else if(etc...)
}
Set tags for each button in interface builder (1-9), then say
if ([sender tag] == 1) {
//First button was pressed, react.
}
else if ([sender tag] == 2) {
//Second button was pressed, react.
}
// Etc...
else {
//Last button was pressed, react.
}
And the same for all the others, or you could put it in a switch.
Rather than do a string check on the title (which is slow and can be tedious) you can:
- (void)buttonPressed:(id)sender
{
for( UIButton *button in self.buttonCollection )
{
if( sender == button )
{
// sender is your button (eg. you can access its tag)
}
}
}
Another option.. Cast sender to check if it's a UIButton, then switch sender.tag:
if ([sender isMemberOfClass:[UIButton class]]) {
switch ([sender tag]) {
case 0:
//do stuff for button with tag 0
break;
case 1:
//do stuff for button with tag 1
break;
case 2:
....
break;
default:
break;
}
}