Objective-C NSButton Toggle - objective-c

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...>
}

Related

Hiding a button Obj-C

I'm looking to hide a button on my i-phone app and then by clicking another button it will appear. I've managed to make the button disappear with a click but can't figure out the opposite. I'm also new to Objective-C as you can probably tell so any tips on improving my code would be helpful. Thanks!
.h :
#property(nonatomic,retain) IBOutlet UIButton* button1 ;
-(IBAction)buttonTouch:(id)sender ;
.m :
#synthesize button1=_button1;
-(BOOL)hideOutlets {
_button1.hidden=TRUE;
}
-(void)buttonTouch:(id)sender {
_button1.hidden = !_button1.hidden;
}
Well to start from scratch, if you want to hide a button set its property hidden to YES,
else if you want to make it reappear then set the property to NO.
Example:
button1.hidden=YES;
button1.hidden=NO;
Your code is basically correct
-(void)buttonTouch:(id)sender {
_button1.hidden = !_button1.hidden;
}
This code will hide your button when it's shown and show it when it's hidden. This should be correct.
You are saying
then by clicking another button it will appear
Are you sure both buttons have the touch-up-inside event properly connected to this action? I guess your problem will be that the buttonTouch: is not called when you touch the other button.
#synthesize button1=_button1;
-(BOOL)hideOutlets {
_button1.hidden=TRUE;
}
-(void)buttonTouch:(id)sender {
_button1.hidden = FALSE; //Or "NO" or "0", it's a boolean
}
In addition, its weird setting a button hidden with a BOOL. If you want to have them hidden on load, go put _button1.hidden = YES; if you want it to hide it only when you have it visible, use
-(void)buttonTouch:(id)sender {
if(_button1.hidden == YES)
{
_button1.hidden = NO;
}
else { _button1.hidden = YES; }
}
I'll try to answer the question correctly as I understand it.
2 buttons, button1 and button2. Pressing button1 hides itself and shows button2. Pressing button2 hides itself and shows button1 again.
-(IBAction)button1Pressed:(id)sender {
// button1 can only be pressed when not hidden, so we can dispense with checks for hidden
[button1 setHidden:YES];
[button2 setHidden:NO]; // assuming this button was hidden at startup
}
-(IBAction)button2Pressed:(id)sender {
// button2 can only be pressed when not hidden, so no need to check for hidden
[button2 setHidden:YES];
[button1 setHidden:NO];
}
This should allow you to flip back and forth between buttons having them hide/show opposite of each other.
Two obvious problems with the code presented.
1) Cocoa uses YES and NO for boolean values not TRUE and FALSE.
2) You've declared a property, so you should use it in preference to the synthesised instance variable.
3) You're button touch method should return IBAction in the implementation as well as the interface.
Don't know if that'll fix your problem, but it's the first step to fix up your code.
#synthesize button1=_button1;
-(BOOL)hideOutlets {
self.button1.hidden=YES;
}
-(IBAction)buttonTouch:(id)sender {
self.button1.hidden = !self.button1.hidden;
}

How to turn off TextDidChange Event in iOS?

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
}

IF UIButton was pressed Xcode

Is it possible to have an if statement that looks if a button has been pressed?
if(condition) {
//Do something
}
What has to go as the condition?
A UIButton will call a method, to which it is wired up in Interface Builder or set to in code.
If you need to know in certain parts of your program, if a button was pressed, you can do something like this:
-(IBAction)buttonTapped:(UIButton *) sender
{
self.buttonPressed = YES; //bool instance variable with property
}
-(void)someOtherMethod
{
if(self.buttonPressed){
//do what you want to do, if button pressed
}
}
But I think, coupling the UI and logic so tight on a semantic level, is no good idea.

Cocoa-Touch UIButton isSelected clarification

I'm rather new to programming in Cocoa, but I've been working on learning the language quite diligently up until I hit this snag that I can't seem to circumvent/hack my way around, (not that I'd want to. I'd prefer to do it right!)
Where I stand, In IB I have a toolbar that has a button and what I'm trying to do is mimic the maps app. I want to be able to press the button and then have my location pop up, while keeping the button selected, then when it's pressed again, deselect it and thus remove the blue blip location from the map.
ideally, I would like to use the following code, but the if statement doesn't seem to want to work on the simulator (which I presume wouldn't change if I tried on the iPhone.)
-(IBAction) showLocation: (id) sender
{
if([sender isSelected]) // this doesn't work!!
{
[sender setSelected:NO];
mapView.showsUserLocation = FALSE;
}
else
{
[sender setSelected:YES];
mapView.showsUserLocation = TRUE;
}
}
obviously if I get rid of the if statement, I know that I can show the location and set the selected as I liked, but I can't seem to "get" the selected property from the button... what's the right way of doing this?
try
- (void)methodName:(UIButton *)sender
{
if (sender.selected == YES) ...

How to take text value of NSButton?

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.");
}
}