Implementing Toggle Switches using UIButtons? - objective-c

I have two buttons, one named "On", and the other named "Off". They are placed on the main view controller.
When the user presses the "On" button, I want it so that when they click on other views such as "sound view" and "vocal view", that a UIAlertView pops up. So the "On" button activates the UIAlertViews, but once the user presses the "Off" button, all the UIAlertViews are disabled.

Create an toggle button method and call this method from your buttons. Your method should be like this:
onButton = [[UIButton alloc] initWithFrame:CGRectMake(140,25,160,20)];
offButton = [[UIButton alloc] initWithFrame:CGRectMake(140,125,160,20)];
- (void) toggleButton: (UIButton *) button
{
if(button == onButton )
{
// do whatever you want
}
else if(button == offButton )
{
// do whatever you want
}
I did not write the method inside the if clause but i hope you see what i mean.

Related

UIButton remove style selected state

I would like to remove the selected state effect from the UIButtons.
When pressed a blue capsule appears next to the UIButton.
UPDATE with new problem
UIButton with type:system selects the targeted button. After changing the UIButton type to custom. This effect was removed. Other buttons were pressed that are located in the same view.
This is part of the UIButton function, if pressed it should run the code in this IBAction. Two UIButtons are connected to this function. With type system it selected the right UIButton, with type custom it seems to select at random.
- (IBAction) buttonAction:(id)sender
{
UIButton *btn = sender;
btn.selected = !btn.selected;
if([sender tag] == 1){
// run code UIButton 1
}
if([sender tag] == 2){
// run code UIButton 2
}
}
I hope this is clear.
Select the button from the xib file and change its type to custom in attributes inspector.
I found the solution to the second problem I described.
The UIButton type: custom doesn't have the selected trait as default.
The traits that were selected are: Button and User Interaction Enabled.
After selecting: selected it worked.
traits

change a backgroundcolor of button inside a view that has a UITapGestureRecognizer and buttons

I have a view with a UITapGestureRecognizer(numberoftapsrequired = 1),and this view also has some buttons.
I want to change background-color of a button which is tapped, select only one to change.
I don’t know how to get this button to change its background-color. Like this figureexample
Get rid of the gesture
Create a IBOutletCollection called buttons with your buttons in it
Add the following code to your VC.
- (IBAction) didTapButton:(UIButton*) sender {
sender.backgroundColor = [UIColor redColor]; // or whatever
// create a IBOutletCollection called buttons with your buttons in it
for (UIButton *b in self.buttons) {
if (b != sender) {
b.backgroundColor = [UIColor blueColor]; // or whatever
}
}
}
Set this action to the "touch up inside" for each button.
If I got your question correctly, you are adding the tap gesture in order to recognize the click event. No need to give tap gesture in the entire view. Give actions for each of the buttons and change their background colors in the action method

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

Showing UISearchBar "X" within text field vs. adjacent Cancel button

I know I can set showsCancelButton to NO for a UISearchBar ... until you tap the search bar text field, and then the cancel button appears anyway! (At least it does for me.)
Is there a way to show the "X" in a circle within the UISearchBar text field vs. having that separate Cancel button show up adjacent to it? Note that this button only appears for me when search mode is active, and this is regardless of the setting for showsCancelButton.
try this
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar's cancel button while in edit mode
mySearchBar.showsCancelButton = NO;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
mySearchBar.showsCancelButton = NO;
}
when using this cancel button will not shown.
for (UIView *searchBarSubview in [searchBar subviews]) {
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)searchBarSubview setClearsOnBeginEditing:YES];
}
}
to enable the cancelButton try searchBar.showsCancelButton = YES;
Try the above lines of code.
if you use interface builder it is ery easy to show.it is there by default.i have used search bar in my app and the searchbartextfield has an "X" to clear the textfield
It might be that the "X" is only intended to clear the search field and not to cancel the search, and so the Cancel button must remain once a search is in play, regardless of showsCancelButton.