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;
}
Related
So i have Two Text Box , lets say , TextBox1 and TwoBox2 .
On Viewdidload TextBox1 i am using
[self.TextBox1 becomeFirstResponder];
Now how to move the Cursor to TextBox2 ,when i press Next Button in Virtual Keyboard
In Cocoa for Mac OS X you have the next responder chain, where you can ask the text field what control should have focus next. This is what makes tabbing between text fields work. But since iPhone do not have a key board, only touch, this concept has not survived the transition to Cocoa Touch.
This can be easily done anyway, with two assumptions:
All "tabbable" UITextFields are on the same parent view.
Their "tab-order" is defined by the tag property.
Assuming this you can override textFieldShouldReturn: as this:
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
return NO; // We do not want UITextField to insert line-breaks.
}
Add some more code, and the assumptions can be ignored as well.
In the callback for Next Button, use,
[self.TextBox2 becomeFirstResponder];
Easy!
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.
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 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.");
}
}
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.