IBOutletCollection of UIButtons wont set highlighted - ios5

I have an IBOutletCollection of UIButtons:
#property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *Buttons;
with an ibaction i would to change the highlighted state permanently after the touch down event.
This Problem is very similar to this:
IBOutletCollection of UIButtons - changing selected state of buttons
... but with the for-loop the buttons doesnt change.
i also tried the perfomselector method from here: Keep iPhone UIButton Highlighted
but it doesnt work.
now my code:
-(IBAction)toggleButtons:(id)sender
{
NSUInteger Index = [button tag];
[[Buttons objectAtIndex:Index] setHighlighted:YES];
}
if i change line four to this:
[[Buttons objectAtIndex:3] setHighlighted:YES];
it works for the fourth element in my collection... But not with the index variable....
regards, phil
Update
SelectionViewController.h
#import <UIKit/UIKit.h>
#interface SelectionViewController : UIViewController
#property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *Buttons;
- (IBAction)toggleButtons:(id)sender;
#end
SelectionViewController.m
#import "SelectionViewController.h"
#interface SelectionViewController ()
#end
#implementation SelectionViewController
#synthesize Buttons;
-(IBAction)toggleButtons:(id)sender
{
UIButton *button = sender;
NSUInteger Index = [button tag];
[self performSelector:#selector(doHighlight:) withObject:sender afterDelay:0];
[[Buttons objectAtIndex:Index] setHighlighted:YES];
}
- (void)doHighlight:(UIButton *)b {
[b setHighlighted:YES];
}
Okey Update 2:
Now i had declared my Buttons as normal IBOutlet and this is not working:
-(IBAction)toggleButtons:(id)sender
{
UIButton *button = sender;
[button setHighlighted:YES];
}
But if change it to this:
-(IBAction)toggleButtons:(id)sender
{
[myOutletButton setHighlighted:YES]; //Normal Outlet
}
it works....
But why is not possible with the sender?
regards!
Update 3
This works also:
for(id button in self.view.subviews)
{
[button setHighlighted:YES];
}
Ok if change the delay time in the selector to 1, the state will be highlighted. I am using "touch down" event... i think after i touched up the button gets its old state. Which event is the right?

Given that your example works with a specific integer, the problem is probably that the tag property is not set properly for each of your buttons. If the buttons are created in interface builder, each of them will have a default tag value of 0. To check this, click on the button and then, in the Attributes Inspector, scroll down to View and see what value is entered in the tag field

Related

Button value copy to other button

How to get the pressed button title text value and to shows as other pressed button if pressed?i have used the following code to get the title text from button
- (IBAction) checkIt:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(#"Button text value %#", button.titleLabel.text);
}
How to show the button text into other button if i pressed? Please help me to resolve this
Do like this,
- (IBAction) checkIt:(id)sender
{
UIButton *button = (UIButton *)sender;
[your_other_button setTitle:button.titleLabel.text forState:UIControlStateNormal];
}
you need to Create Globle Variable at your app-delegate like:-
yourAppdelegate.h
#property (strong, nonatomic) NSString *buttonTitleCopy;
yourAppdelegate.m
#synthesize buttonTitleCopy;
now you just get this variable at your Particular Class like:-
yourClass.h
#import "yourAppdelegate.h"
//create Object of your Delegate Class
yourAppdelegate *objAppdelegate;
yourClass.m
- (void)viewDidLoad
{
objAppdelegate = (REMAppDelegate *) [[UIApplication sharedApplication]delegate];
[super viewDidLoad];
}
now in your Method:-
- (IBAction) checkIt:(id)sender
{
UIButton *button = (UIButton *)sender;
objAppdelegate.buttonTitleCopy=button.titleLabel.text
NSLog(#"Button text value %#", delegate.buttonTitleCopy);
}
now you can use buttonTitleCopy variable anywere in your project with app-delegate class Object. Hope your getting this and you solve your issue.
NOTE:- just set your OtherButton Title with
otherButton.titleLabel.text=objAppdelegate.buttonTitleCopy;
You can try this way
- (IBAction) checkIt:(id)sender
{
otherButton.titleLabel.text=sender.titleLabel.text;
}
you can get it to point tags or just make a variable button and add it to your button. And get btn text.

Get UIButton reference from viewDidLoad

I am very new to iPhone development. I am trying to disable an already existing button but I cant actually obtain a pointer to a specific element in the view. For instance, I have the following in the viewController header
- (IBAction)one:(id)sender;
and the implementation is
- (IBAction)one:(id)sender {
}
which are just event handlers. However, I need disable the button when view opens and I am a little bit lost on how to obtain references to elements outside of the event handler.
So in other words, my thought is to have something like:
UIButton* myButton = //something
where the something is where I am lost on what to do. Any ideas? I greatly appreciate any help I get on here!
You need to create a property for your button in the interface:
#property(nonatomic, retain) IBOutlet UIButton * button;
And add this to implementation:
#synthesize button;
Then connect the button to it in interface builder. After this you can disable the button by:
button.enabled = NO;
Hope I could help!
Just give tag to your button and access your button with tag value.
UIButton *btn = (UIButton*)[self.view viewWithTag:1];
[btn setHidden:YES];
In your .h File
#import <UIKit/UIKit.h>
#interface RpViewController : UIViewController
#property (retain , nonatomic)IBOutlet UIButton *Btn1;
#end
In your .m file , in implementation write this :
#synthesize Btn1;
Now on interface , click on button.
In button's properties - > Drawings - check Hidden checkbox.
Wherever you want to show that button , just write.
[Btn1 setHidden:FALSE];
#property (strong, nonatomic) UIButton *button;
#synthesize button;
// In View Did Load...
self.button = [UIButton buttonWithType:UIButtonTypeCustom]; // button can be of any type.
[self.button setTag:1];
// if you have more buttons initialize it and set its tag. you can get to know which button was pressed using tags.
[button addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-(void) buttonEvent:(UIButton *) sender
{
NSLog(#"%d",sender.tag);
if(sender.tag == 1)
{
[self.button setEnabled:NO]; // This makes your button disabled, i.e you can see the button but you cannot click on it.
[self.button setHidden:YES]; // This makes your button hidden.
}
}
if you have more doubts ping me back.

How to make reference to a UIButton in viewDidLoad?

This is my action UIButton:
-(IBAction)favoriteButtonPressed:(id)sender
{
if (favoriteButtonSelected == 0) {
[sender setSelected:YES];
favoriteButtonSelected = 1;
[sender setImage:[UIImage imageNamed:#"favoritedItem.png"]];
[selectedObject setValue:#"Yes" forKey:#"Favorite"];
} else {
[sender setSelected:NO];
favoriteButtonSelected = 0;
[sender setImage:[UIImage imageNamed:#"notFavorite.png"]];
[selectedObject setValue:#"No" forKey:#"Favorite"];
}
}
How to make a reference to the button in viewDidLoad? To make the following code work:
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[selectedObject valueForKey:#"Favorite"] isEqual:#"Yes"]) {
[favoriteButton setImage:[UIImage imageNamed:#"favoritedItem.png"]];
[favoriteButton setSelected:YES];
favoriteButtonSelected = 1;
} else {
[favoriteButton setImage:[UIImage imageNamed:#"notFavorite.png"]];
[favoriteButton setSelected:NO];
favoriteButtonSelected = 0;
}
}
EDIT FOR PROGRESS:
Now I did like this: Ctrl-drag from UIButton to ViewController in Assistant Editor. Connection: Outlet, name: favoriteButton, type: UIButton, storage: weak. But errors still there. + error for synthesize & error in viewDidUnload.. suggestion?
The Assistant Editor header for View Controller with the added property from Ctrl-drag:
#interface DetailViewController : UIViewController {
IBOutlet UIScrollView *viewScroller;
}
#property (nonatomic, strong) IBOutlet UILabel *mylLabel;
#property (nonatomic, strong) NSString *selectedObj;
#property (strong, nonatomic) NSArray *detailsDataSource;
#property int detailIndex;
#property (weak, nonatomic) IBOutlet UIButton *favoriteButton; //The added property
#end
Control drag from your button to the header of the viewcontroller to create a property. (You can do this while in assistant mode, second button on the top right)..
Then you can reference your button from wherever using that property
What you need is an IBOutlet for the button.
You can ctrl+drag the button from XIB file to the header of this class, and create a IBOutlet property named favoriteButton.
Well unless you are using the latest version of Xcode you do need to #synthesize favorite button.
You really should post your error messages you are just making people guess.

iphone what to use as there are no "radio buttons"

For the iPhone, I havent been able to find anything like a radio button that triggers the other buttons in a group. So what would everyone suggest using? UISwitch I guess and do something to trigger the others in the group when an other is selected?
If I had several UISwitch objects how would I trigger the others to be the opposite of what I switched?
I use buttons with images that act like radio buttons. i.e. an off image and an on image. The nice thing about this approach is it is very simple to implement and you can use the image to control the state of the button control. It works as a simple toggle and no fancy button state is needed. You can easily add code to the method that does something when buttons are on or off, in the example I just call a method which is writing some user defaults.
It should be quite easy to adapt to create radio button functionality.
-(IBAction)tickboxControl:(id)sender{
NSLog(#"%s",__FUNCTION__);
bgImageOn = [UIImage imageNamed:#"tickedBox.png"];
bgImageOff = [UIImage imageNamed:#"tickBoxEmpty.png"];
UIButton *buttonClicked = (UIButton *)sender;
UIImage *imageOfClicked = [buttonClicked imageForState:UIControlStateNormal];
if (imageOfClicked == bgImageOff) {
[self setButtonFlags: [NSNumber numberWithInt:[sender tag]] : [NSNumber numberWithInt:1] ];
[buttonClicked setImage:bgImageOn forState:UIControlStateNormal];
} else{
[self setButtonFlags: [NSNumber numberWithInt:[sender tag]] : [NSNumber numberWithInt:0] ];
[buttonClicked setImage:bgImageOff forState:UIControlStateNormal];
}
}
I created a project that shows exactly how to achieve what you are asking to do.
You can change the graphics from checkboxes (which I find a little better to see and understand on a device) to radio buttons.
Read the debug log to understand the logic.
If you have any questions give me a shout.
Radio Buttons Example Project (XCode4)
[this is in reply to your comments below]
I also used a custom UISegmentedControl. Something like this:
NSMutableArray* buttons;
- (void)touchDownAction:(UIButton*)button {
[self dimAllButtonsExcept:button];
if ([delegate respondsToSelector:#selector(touchDownAtSegmentIndex:)])
[delegate touchDownAtSegmentIndex:[buttons indexOfObject:button]];
}
-(void) dimAllButtonsExcept:(UIButton*)selectedButton {
for (UIButton* button in buttons) {
if (button == selectedButton) {
button.selected = YES;
button.highlighted = YES;
} else {
button.selected = NO;
button.highlighted = NO;
}
}
}
The full code is at https://github.com/j4n0/jobsket/tree/master/sources/main/ui/custom/segControl
I’ve used a UISegmentedControl as radio buttons.
Try this for a radio button in iOS:
#interface RadioButtonViewController : UIViewController
{
//for radio button
IBOutlet UIButton *radioButton1;
IBOutlet UIButton *radioButton2;
IBOutlet UITextField *selectedValue;
IBOutlet UILabel *label1;
IBOutlet UILabel *label2;
}
#property (nonatomic,retain) IBOutlet UIButton *radioButton1;
#property (nonatomic,retain) IBOutlet UIButton *radioButton2;
#property (nonatomic,retain) IBOutlet UITextField *selectedValue;
-(IBAction)userChangedButtonClicked:(id)sender
#end
Write code in .m file and specify the default and selected image in storyboard inspector window and give the tag value each button.
RadioButtonViewController.m
-(IBAction)userChangedButtonClicked:(id)sender
{
UIButton *senderBtn = (UIButton*)sender;
if (senderBtn.tag == 101 && !self.radioButton1.selected)
{
self.radioButton1.selected = TRUE;
self.radioButton2.selected = FALSE;
selectedValue.text = label1.text;
}else if (senderBtn.tag == 102 && !self.radioButton2.selected)
{
self.radioButton1.selected = FALSE;
self.radioButton2.selected = TRUE;
selectedValue.text = label2.text;
}
}

Connecting Multiple Buttons to one action?

I have multiple UIButtons in my app. I also use interfacebuilder. In my .h i have something like this
IBOutlet UIButton *button1;
IBOutlet UIButton *button2;
IBOutlet UIButton *button3;
- (IBAction)buttonPressed;
Then In my m i want to do something like this
- (IBAction)buttonPressed {
if (theButtonIpressed == button1)
{
// do something if
}
}
The problem is I don't have something called "theButtonIpressed" so I cant do this. What should my if statement look like? I don't want to make a -(IBAction) for each button. Is there something that I can determine which button was pressed? Thanks!
Thanks,
-David
You can also set the tag property for each button in the interface builder and then use it to find out which button was pressed.... This also means that you don't need to define all your button references (UIButton) and keep track of them in code....
- (void) doSomething:(id)sender {
int buttonPressed = [sender tag];
switch (buttonPressed) {
case 0:....
// etc
}
}
Define your - (IBAction)buttonPressed to:
- (IBAction)buttonPressed: (UIButton *) buttonIpressed
Then it will work.
- (IBAction)buttonPressed:(UIButton*)button
But if you're doing something different for each button then the proper way to do it is to create separate IBActions.
You can use tag values for each buttons
IBOutlet UIButton *button1;
button1.tag = 100;
IBOutlet UIButton *button2;
button2.tag = 200;
IBOutlet UIButton *button3;
button3.tag = 300;
- (IBAction)buttonPressed:(id)sender
{
if ([sender tag]==100)
{
NSLOG("button1");
}
else if([sender tag]==200)
{
NSLOG("button2");
}
else {
NSLOG("button3");
}
}