I have 4 button (b1,b2,b3,b4) and a label (lab).Now I want to display button title in label when a particular button is pressed.i did it with four (IBAction) method one for every button.But i want to do it with 1(IBAction) method.so the problem is to to how to identify which button is pressed??? i knew a method something like "getBytitle" method.But i need better solution.Can anyone help??? I also need answer about how to identify button in segment control.Advanced thanx for reply.
Have a look in IB, the tag field of the button attibutes might be what you are looking for. Set each of the buttons you want to detect with a different integer tag value, and then set their IBActions to the same method. Now you can check which button was pressed by checking for the tag field in the sender
- (IBAction) buttonPressed: (id) sender
{
switch ( ((UIButton*)sender).tag ){
case 1:
<something>
break;
case 2:
<something else>
break;
default:
<default something>
}
}
The button that triggers the action gets passed as the sender. Your method probably looks a bit like this:
- (IBAction) buttonPressed: (id) sender;
The sender is the button, so that if you want to display the button title in a label, all you have to do is this:
- (IBAction) buttonPressed: (id) sender
{
label.text = [sender currentTitle];
}
That should be it.
Related
I am using same action for multiple uibuttons. Is it possible to get which button is the sender?
E.g. Button 1 has been tagged with outlet1 and button2 with outlet2 etc,
When these buttons are pressed, it will call the same action but based on what the sender is, I want to add my logic
//BY DEFAULT THERE IS A TAG OF BUTTON WHICH IS 0
if(yourButton.tag==0){
//do something
yourButton.tag=1;
}else if(yourButton.tag==1){
//do something
yourButton.tag=0;
}
YOU CAN PERFORM DOUBLE ACTION IN SINGLE OUTLET .
THANK YOU
I have search and can't find any information so I would like some help here. I am new to Xcode and objective c. I have 10 NSButtons set in Interface Builder to be Push On Push Off type. I am trying to figure out how when one of the buttons is clicked and highlighted, how do I unhightlight the other nine. I am use to Java, in java you can just make an if statement to turn off the highlight of the buttons not clicked. In IB I don't see how to send a message to the other buttons because I don't know their "names" or addresses. Can you please help me figure this out, explain it or send me to a link or video. Thank you.
This is what I've used in the past.
Create an NSArray with all your buttons in it, something like:
NSArray* buttons = #[button1, button2, button3, button4];
Then create a method like this.
- (void) toggleButtons: (id) sender {
for (Button *item in buttons) {
if (item == sender) {
item.selected = !item.selected;
} else {
item.button.selected = NO;
}
}
}
Now call it from each of your button handlers:
- (IBAction) handleButton1:(id) sender {
[self toggleButtons:sender];
<...rest of your code...>
}
Can someone please tell me how can I change the title of the button ? I have 10 buttons with tags from 1 to 10 and I want to change the title of the clicked button when I click on it. It always change the title of the last button with tag 10.
Buttons are connected by this:
#property (assign,nonatomic) IBOutlet NSButton *clickButton;
I also added synthesize in *.m file.
In *.m file I have:
NSLog(#"Button pressed: %i", (int)[sender tag]);
[clickButton setTitle:[NSString stringWithFormat:#"%c",'o']];
The method is also connected to the button:
(IBAction)startGame:(id)sender;
I do not want to make switch or make 10 buttons with no tags. Can I do this by any function to directly show text on the button which is clicked ?
The sender argument to your -startGame: method will be the NSButton that was clicked. So you could just do this:
- (IBAction)startGame:(id)sender
{
[sender setTitle:#"Title string"];
}
You need to connect all button action with same Action by doing so you well get the action at a common place with the clicked button object and by accessing the button you can change the title. :)
Now i am creating dictionary application in iOS.
In my apps, I used textDidChange Event of UISearchBar with UISearchDisplayController for Auto Complete searching.
So when i type a word in SearchBar,it's automatically search in database.
However i have a function in Preferences to turn off AutoComplete searching.
After i turn off AutoComplete function in Preferences, It's still auto complete searching with TextDidChange Event.
I don't want TextDidChange event this time.I want to search after i enter complete word in search bar and then click Search button from Keyboard.
So how can i turn off TextDidChange event from UISearchBar?
Please let me know if you ok.
Best Regards,
In your situtation you don't want to suppress the textDidChange event. Instead you want your UISearchDisplayDelegate's shouldReloadTableForSearchString: method to return NO unless the Search button has been clicked.
You could create a boolean ivar to keep track of this. Set the ivar to NO when textDidChange is called, and set it to YES when searchBarSearchButtonClicked: is called. Then in your shouldReloadTableForSearchString: you can do the test.
EDIT: Clarified by expanding code example. (In this example I am assuming you have set up a ivar named isSearchButtonClicked).
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
isSearchButtonClicked = NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
isSearchButtonClicked = YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
// returns YES if search button was clicked, otherwise return NO
return isSearchButtonClicked;
}
you can always create a bool variable and check if auto complete is on or not... based upon that you can search or not.
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope
{
//create a bool variable and check is Auto Complete searching is Yes or No
if YES then pass searchText
if no then Don't pass any thing unit user press the search button
after he press search button pass searchText
}
how should i take NSButton text value , e.g if i use 2 buttons with text Click and Cancel, i want to check which button is clicked and then show a message with NSRunAlertPanel(...) which button i have clicked..what code should i write for it when the button is clicked.
In you action method you get an argument, usually named 'sender', which is the button. So you could do something like:
- (IBAction)buttonClicked:(id)sender
{
if ([[sender title] isEqualToString:#"Click"]) {
NSLog(#"Click clicked.");
} else if ([[sender title] isEqualToString:#"Cancel"]) {
NSLog(#"Cancel clicked.");
}
}
It's better not to use the title for checking the button, since the title could change in different localizations. You could specify the tag instead, which is simply an int and which can be used to identify different senders.
The way this is typically implemented is that each button would call a different action, thus there would be no need to check the text of the button. See The Target-Action Mechanism.
In general it is almost always a bad idea to use the user visible text to control program logic because that makes localization harder.
You might also want to describe your situation further. Are you using Interface Builder to create your interface? Are these buttons in a modal dialog or a document window?
You could give the button a name in the class info tab of the inspector window in Interface Builder, then declare it as an IBOutlet in your app delegate.
AppDelegate.h:
IBOutlet NSButton *ClickButton;
IBOutlet NSButton *CancelButton;
Then hook up the outlet in Interface Builder, and just check to see which button is the sender in your method:
- (IBAction)buttonClicked:(id)sender
{
if (sender == ClickButton) {
NSLog(#"Click clicked.");
}
else {
NSLog(#"Cancel clicked.");
}
}