iOS / iPhone- Hiding one button when another is clicked - iphone

I have a settings view in my app which has a couple of buttons (actually UISwitches). If the "off" setting on one of the switches is selected, I'd like to hide the second switch immediately. Can this be done?

IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
write the above 2 lines in your .h file and set the outlets with XIB.
Now create a method called hideButton
-(IBAction)hideButton
{
btn1.hidden = YES;
}
in XIB attach this method with btn2.
So now when you click on btn2 it will hide btn1.

Connect the two switches as outlets. lets say switch1 & switch2.
Connect this function to the valueChanged action.
- (IBAction)mySwitch1:(id)sender {
[switch2 setHidden:!(switch1.isOn)];
}
Now when switch1 is not on then switch2 will be hidden.

Add a target to the first switch which on value change calls the instance of the second switch and hides it.
Add the target:
[switch1 addTarget:self action:#selector(switchToggled:) forControlEvents: UIControlEventValueChanged];
Calls this method:
- (void) switchToggled:(UISwitch*)switch {
if ([switch isOn]) switch2.hidden = YES;
else switch2.hidden = NO;
}
NJones if statement is more effective though.

Swift 4
Within your function do the following:
btn1.isHidden = true

Related

How to pass UIAction without touching the UIButton

I've got a UIButton that acts as a switch.
When user taps on it, its state changes to "selected" and action "one" is called. When user taps again UIButton state changes to "not selected" and the action is no longer available.
Is there a way to set the UIButton to "selected" by taping on a completely different UIButton and have it change to "selected" and call the same action as well?
Cheers
From what I have understood from the question, I would like to give you my suggestion
if (!<your_button>.selected)
{
<your_button>.selected = YES;
// do your stuff here
}
else
{
<your_button>.selected = NO;
// do your stuff here, in your case action should be no longer available so that,
<your_button>.userInteractionEnabled = NO;
}
Enjoy Programming !
try like this,'
-(IBAction)nextbutton:(id)sender{
UIButton *btn=(UIButton *)[self.view viewWithTag:tagValue];//pass tag value of that particuler button
btn.selected=YES;
//do whatevr you want.
}
simple thing when you tap on the uibutton make an action for that and in the action for that button , you can set the button state to selected and then in the next line of code you can call that method (action)
- (IBAction)btn1Touched:(id)sender {
[_btn2 setSelected:YES];
[self btn2Touched:self];
}
- (IBAction)btn2Touched:(id)sender {
NSLog(#"btn Action called");
}
UIbutton has state called selected and it is managed via property selected
so to make a button to selected state
[buttonInstance setSelected:YES];
To make it unselected use
[buttonInstance setSelected:NO];
Kindly write a function for that button action like,
-(void)Buttonaction
{
do your action here
also set your button has selected state.
buttonname.setselected=yes;
}
call this function while you are tabbing your button.
Do This:
-(IBAction)switchButtonClicked:(UIButton*)sender
{
if(sender.isSelected)
{
.....
}
else
{
...
}
[sender setSelected:!sender.isSelected];//This will work as Switch.
}
set Selected(Switch ON) and default(Switch OFF) property (image and title) of UIButton from XIB, OR from Code as you want.
With a single button you can do you functionality with this code

how to pass parameter when different button call same class?

I am developing for iPhone the app I am developing has many buttons and I want all buttons to call the same class but with different parameter
for example I want button1 to call the displayimageclass
and button2 should call the same class displayimageclass so, how to different method call same class and in that same class how to fetch different button from where it is call to particular class.
That is pretty much standard. Just define as many IBAction methods as you need.
In the viewController's .h file:
- (IBAction)actionButton1:(UIButton *)sender;
- (IBAction)actionButton2:(UIButton *)sender;
- (IBAction)actionButton3:(UIButton *)sender;
And in its .m file:
{
//react here to button1 pressed
}
- (IBAction)actionButton1:(UIButton *)sender
{
//react here to button1 pressed
}
- (IBAction)actionButton2:(UIButton *)sender
{
//react here to button2 pressed
}
- (IBAction)actionButton3:(UIButton *)sender
{
//react here to button3 pressed
}
Then associate the action with the Button's events (TouchUpInside would be most appropriate, I guess).
You should add tag property to your buttons.
Suppose your buttons are
button1.tag =1;
button2.tag =2;
button3.tag =3;
When you click any button, NSLog shows related button tag number. i.e You will get your result what you want.
- (IBAction)buttonClicked:(id)sender {
UIButton* button = (UIButton *)sender;
NSLog(#"The number of tag:%d",button.tag);
}
I think it will be helpful to you.
you can implement your action method like this:
- (IBAction)action:(UIButton *)sender
{
//sender will be different for different button
}
Set tag value for buttons and get those buttons like
- (IBAction)action:(UIButton *)sender
{
UIButton *btn = (UIButton*)sender;
}
Are you looking for
1) several buttons performing the same method with the same behavior
2) several buttons performing the same method but having different behavior based on the button characteristics
3) each button performing its own method.
1) You can register several buttons on the same IBAction, they all will call it and perform the same code
2) In the method were all the buttons are registered (cf: 1) it is possible to sort the buttons by subclassing the UIButton class, giving an enum value that represents what the button should perform and then in the called method check the enum value with a switch in order to trigger the wanted behavior
The UIBarButtonItem already has this tag property so you could use them by giving a value to each of your UIBarButtonItem in the Interface Builder and set them to call the following method
- (IBAction) myButtonsMethod:(id)object
if ([object isKindOfClass:[UIBarButtonItem class]])
{
switch ([(UIBarButtonItem *)object tag])
{
case myFirstTagValue
...
}
3) #Hermann Klecker solution is the right one.

Replace or extend UIBarButtonItem selector

I'm trying to make a custom UIToolbar which handles rotation and custom arrangement. When it's in portrait mode some of the barbuttonitems will not be visible, so I add a "more" button from which pops up a little view with these items. My problem is that when my current orientation is portrait and when I select a visible barbuttonitem ( which is not in popup ) I want to close the popup if it's open. I want the smae behavior for the uibarbuttons in the popupview to close the popup after tap.
So I'm trying to replace somehow the UIBarButtonItem's selector with my own, in which I call the already defined actions, like this :
-(SEL)extendOriginal:(UIBarButtonItem *) uibb
{
if (popup) popup.hidden = YES;
[uibb.target performSelector:uibb.action];
// return ?? do what ??
}
But how do I replace the original selector to call this custom method with my custom UIToolbar as its target ? Or how could I "extend" the original selector with this call ? Sorry if question is lame :)
Edit: In other words, I want 2 actions with 2 separate targets to be executed when UIBarButtonItem is tapped.
Thanks!
This -(SEL)extendOriginal:(UIBarButtonItem *) uibb doesn't make any sense.
I assume your are setting the target and the action of the bar button item somewhere. There you can set any method with one argument id or UIBarButtonItem* as the selector.
Therefore try to change your code to
- (void)myMethod:(UIBarButtonItem *) uibb
{
if (popup) popup.hidden = YES;
// do cool stuff here
}
and set the target as
[[UIBarButtonItem alloc] initWithTitle: #"Blabla" style: UIBarButtonItemStylePlain target: self action: #selector(myMethod:)];
Finally, I found a way to do it, not the prettiest, but it works.
In the custom toolbar class I created in its layout method a UITapGestureRecognizer to handle taps. I've set the cancelsTouchesInView to false and in the
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
method I'm returning true only for my toolbar's subviews.
This way the original target and action of each UIBarButtonItem remains the same and the supplementary code to handle my popup is the action of the UIGestureRecognizer.
Another problem was to distinguish between the tapped items on the toolbar (the tapped view in touch.view is some undocumented view, yay), eventually I did it with some BOOL iVars.
I hope this will help somebody with the same problem.

iPhone - call UISwitch that is generated in a UIView when a button is pressed

To clarify my question, my program has three lightbulb on the screen (Customized UIButton)
when any lightbulb is pressed, I programatically generate a UIView with a switch on it
when I turn on the switch, corresponding lightbulb will light up (change its background image)
However, I have trouble accessing this UISwitch since I can't declare it publicly
My code goes something like this:
#property buttonA;
#synthesize buttonA;//all three buttons have their background image set to 'off.png'
- (IBAction)lightBulbPressed:(UIButton *)sender
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(1,1, 64, 64)];
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0,0,64,64)];
[mySwitch addTarget:self action:#selector(onOrOff) forControlEvents:UIControlEventValueChanged];
[myView addSubview:mySwitch]
[self.view addSubview:myView];
}
So what troubles me is how to program the selector onOrOff, so that it knows which switch is being touched and change the background image of corresponding button accordingly.
Think about your method:
- (IBAction)lightBulbPressed:(UIButton *)sender {
// your method
}
You already know who called it. This piece of information is stored in sender.
So you can save it and use later in onOrOff
By the way, if you are using UISwitch you have to check
UIControlEventValueChanged
and not UIControlEventTouchUpInside.
EDIT: To pass your sender you can store its value to a NSString *buttonTapped declared in your .h file
- (IBAction)lightBulbPressed:(UIButton *)sender {
if (sender == bttOne) {
buttonTapped = #"ButtonOneTapped";
} else if (sender == bttTwo) {
buttonTapped = #"ButtonTwoTapped";
} else if (sender == bttThree) {
buttonTapped = #"ButtonThreeTapped";
}
// your method
}
- (void)onOrOff {
if ([buttonTapped isEqualToString:#"ButtonOneTapped"]) {
// Button One
} else if ([buttonTapped isEqualToString:#"ButtonTwoTapped"]) {
// Button Two
} else if ([buttonTapped isEqualToString:#"ButtonThreeTapped"]) {
// Button Three
}
}
One way to do so, is taht you give them distinct tag numbers in IB, and in - (IBAction)lightBulbPressed:(UIButton *)sender method, get their tag. e.g. NSInteger pressedButtonTag = [sender tag];, and go from there.
Also, instead of alloc/init myView every time user presses a button, you can add that view in IB, add the switch to it, put in the hierarchy of the owner but not the view, and set an outlet to it in .h. Call it whenever you need it, and again, access the switch by tag e.g. ( UISwitch *mySwitch = (UISwitch *)[myView viewWithTag:kSwitchTag]; ) and do whatever you want to do (on or off), add it to the subview and remove it later. This is more efficient.

IPHONE - How to initialize a button using editButtonItem

I want to use table edit function. I know
self.navigationItem.rightBarButtonItem = self.editButtonItem;
However I want to set a UIButton set it as editButtonItem or can do edit table function
You cant use self.editButtonItem because it is present when your class is subclass of UITableView.
But you can achieve same functionality by the following code-
use a bool variable (declare in .h file says isEdit).
-(IBAction)editTable
{
if(!isEdit)
{
isEdit=YES;
yourTableView.editing=YES;
}
else
{
isEdit=NO;
yourTableView.editing=NO;
}
}
bind this IBAction to the button. you can also change the text of the button and style also by setting property of the button.
just write your code in onclick of UIButton ,that s
-(IBAction)deletebuttonPresed:(id)sender
{
table.editing=YES;
}