Change Action of a button depending on a Pickerview Choice iphone sdk - iphone

I explain you what I'm trying to do.
I have a PickerView, and a button, i'd like when I select the row "Action1" on my Pickerview, the action become a UIAlert when i click on my button., and when I select the row 'Action2', the action become a ModalTransition when i click on my button.
How I can do it ?
Thanks

set a method for button click as
[btn addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
in method you can implement a case where the case options can be the values from the picker output(set a integer to know the selected index of picker via UIPickerViewDelegates (pickeroption)
-(void)method1
{
switch pickeroption
case 1:
Action1
break;
case2:
action 2
break;
}

There could be multiple solution to do this
Set Button Tag as per PickerView selection and in Action/Selector Method you can create Switch Case based on Tag. And can perform Action
You can directly make switch case on Picker View in Action/ Selector Method.
But if you want to set Multiple selector on different state of button you can use
[btn addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchDown];
Hope this will help you out.

Related

How to pass multiple Selectors in UIButton

This is my code for a back button.
I have 9 buttons (button1,button2...button9)
And i want this button to activate if i press any of the other 9 buttons.
So is there a way where i can do this?
[_backButtonOutlet addTarget:self action:#selector(button1:) forControlEvents:UIControlEventTouchUpInside];
Create some method with will call all the desired methods that you want to call at the same time and call that method on button tapped event. Good Luck!
-(IBAction) button1:(id)sender
{
[self btn1Pressed:sender];
[self btn2Pressed:sender];
[self btn3Pressed:sender];
[self btn4Pressed:sender];
[self btn5Pressed:sender];
[self btn6Pressed:sender];
[self btn7Pressed:sender];
[self btn8Pressed:sender];
}
_backButtonOutlet is a reference to the button so you don't want it to be called when any buttons are pressed. button1: is the method that you want to be called when any button is pressed.
Option 1:
call:
[otherButtonOutlet_N addTarget:self action:#selector(button1:) forControlEvents:UIControlEventTouchUpInside];
on each of the other buttons (replace otherButtonOutlet_N as appropriate).
Option 2:
In your XIB / storyboard, connect all of the buttons to the button1: method. You can add multiple target/action pairs to a button.

Button triggers two actions while pressing the changed button title

I have changed button title of a UIButton programmatically. I have also assigned an action to changed button title. Now when i tap on the button, it triggers both the action (one with same button title and one with changed). However, i want to trigger only one action for the changed title button.
How can i do it? Any help would be appreciated.
Two solutions. Toggle the target/action between methods or decide what to do based on some state.
1.
- (void)method1:(id)sender;
{
[sender removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
[sender addTarget:self action:#selector(method2:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)method2:(id)sender;
{
[sender removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
[sender addTarget:self action:#selector(method1:) forControlEvents:UIControlEventTouchUpInside];
}
2a.
- (void)buttonTapped;
{
if (self.someState) {
} else {
}
}
2b.
- (void)buttonTapped:(UIButton *)button;
{
if ([[button titleForState:UIControlStateNormal] isEqualToString:#"First title"]) {
} else {
}
}
if you assign two actions for the same button for control state touchupinside, it will always trigger both.You should remove the first action while adding the second and remove second itself and add the first again at the end of selector function.
The following is the default action of the button;
[button addTarget:self:action:#selector(actionOne)forControlEvents:UIControlStateTouchUpInside]
In the actionOne you can call remove observer for the first action and add the second one.
[button addTarget:self:action:#selector(actionTwo)forControlEvents:UIControlStateTouchUpInside]
And in actionTwo function you do the reverse
Instead of making two conditions
f([urlresponse status code] == 200)
{
[myButton setTitle:#"Unfollow" forState:UIControlStateNormal]; //title changed
[myButton addTarget:self action:#selector(unfollowButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
It might be helpful for your case.

UIButton need clicks two times for firing its action

I have two round rect buttons. I have implemented its touch up inside action. It works fine but when my click second button just after clicking the first button, my first click has no effect. This is happening with both the buttons. I am actually trying to change background image of buttons on each click to get the effect of check box (checking/unchecking). Below is the code for for one of the buttons:
-(IBAction) checkButton2: (id) sender
{
self.checkButton2 = sender;
if ( isCheck == NO)
{
[self.checkButton2 setImage: [UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
isCheck = YES;
}
else
{
[self.checkButton2 setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
isCheck = NO;;
}
}
This is solved. Actually I was using isCheck BOOL type variable for two actions(One is given above) for two buttons and both actions were setting this variable separately which shows deviation from expected behavior.

Best radio-button implementation for IOS

I would like to ask if there are examples out there on how to implement radio-button options on an iPhone app.
I find the Picker View quite big for a simple selection feature.
I'm not sure if Apple excluded radio buttons on purpose, and whether if it is better to simply use a Picker View from a usability / user experience point-of-view.
I have some thoughts on how the best radio button implementation should look like. It can be based on UIButton class and use it's 'selected' state to indicate one from the group. The UIButton has native customisation options in IB, so it is convenient to design XIBs.
Also there should be an easy way to group buttons using IB outlet connections:
I have implemented my ideas in this RadioButton class. Works like a charm:
Download the sample project.
Try UISegmentedControl. It behaves similarly to radio buttons -- presents an array of choices and lets the user pick 1.
Just want to sum up, there might be 4 ways.
If you don't have much space, add a click event for text or button, then show UIPickerView:
or open a new table view control with a check mark:
If there is more space, add a table view directly to your main view:
The final solution is using UISegmentedControl:
Hope this helps.
Try DLRadioButton, works for both Swift and ObjC. You can also use images to indicate selection status or customize your own style.
Check it out at GitHub.
**Update: added the option for putting selection indicator on the right side.
**Update: added square button, IBDesignable, improved performance.
**Update: added multiple selection support.
I know its very late to answer this but hope this may help anyone.
you can create button like radio button using IBOutletCollection. create one IBOutletCollection property in our .h file.
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *ButtonArray;
connect all button with this IBOutletCollection and make one IBAction method for all three button.
- (IBAction)btnTapped:(id)sender {
for ( int i=0; i < [self.ButtonArray count]; i++) {
[[self.ButtonArray objectAtIndex:i] setImage:[UIImage
imageNamed:#"radio-off.png"]
forState:UIControlStateNormal];
}
[sender setImage:[UIImage imageNamed:#"radio-on.png"]
forState:UIControlStateNormal];
}
For options screens, especially where there are multiple radio groups, I like to use a grouped table view. Each group is a radio group and each cell a choice within the group. It is trivial to use the accessory view of a cell for a check mark indicating which option you want.
If only UIPickerView could be made just a little smaller or their gradients were a bit better suited to tiling two to a page...
I've written a controller for handling the logic behind an array of radio buttons. It's open source and on GitHub, check it out!
https://github.com/goosoftware/GSRadioButtonSetController
The following simple way to create radio button in your iOS app follow two steps.
Step1- Put this code in your in viewDidLoad or any other desired method
[_mrRadio setSelected:YES];
[_mrRadio setTag:1];
[_msRadio setTag:1];
[_mrRadio setBackgroundImage:[UIImage imageNamed:#"radiodselect_white.png"] forState:UIControlStateNormal];
[_mrRadio setBackgroundImage:[UIImage imageNamed:#"radioselect_white.png"] forState:UIControlStateSelected];
[_mrRadio addTarget:self action:#selector(radioButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
[_msRadio setBackgroundImage:[UIImage imageNamed:#"radiodselect_white.png"] forState:UIControlStateNormal];
[_msRadio setBackgroundImage:[UIImage imageNamed:#"radioselect_white.png"] forState:UIControlStateSelected];
[_msRadio addTarget:self action:#selector(radioButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
Step2- Put following IBAction method in your class
-(void)radioButtonSelected:(id)sender
{
switch ([sender tag ]) {
case 1:
if ([_mrRadio isSelected]==YES) {
// [_mrRadio setSelected:NO];
// [_msRadio setSelected:YES];
genderType = #"1";
}
else
{
[_mrRadio setSelected:YES];
[_msRadio setSelected:NO];
genderType = #"1";
}
break;
case 2:
if ([_msRadio isSelected]==YES) {
// [_msRadio setSelected:NO];
// [_mrRadio setSelected:YES];
genderType = #"2";
}
else
{
[_msRadio setSelected:YES];
[_mrRadio setSelected:NO];
genderType = #"2";
}
break;
default:
break;
}
}

cocoa UIImageView addTarget with extra parameter

With a loop I add UIImageView to a UIScrollView i need to add an extra parameter addTarget so when i click i can log the index.
[imageButton addTarget:self action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
-(IBaction) buttonPushed: (int) index
{
NSLog(#"%d",index);
}
How do i achieve this?
When you add a target, the method being call can either have no arguments (e.g. buttonPushed) or have one (buttonPushed:) which is the control sending the event (in this case your button). If you want an index, or any other value, you need to set it on the button sending the event. For example, when you setup the buttons:
myButtons = [NSArray arrayWithObjects:myFirstButton, mySecondButton, nil];
[myFirstButton addTarget:self action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
[mySecondButton addTarget:self action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
and implement your action as
- (IBaction)buttonPushed:(UIButton *)button
{
NSLog(#"%d",[myButtons indexOfObject:button]);
}
Use the tag property of the button