Get UIButton reference from viewDidLoad - iphone

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.

Related

Change background button2 into button1 iPhone

I am a beginner in Xcode, and I have a problem.
I have 2 buttons:
- (IBAction) HIGH1: (id) sender
- (IBAction) HIGH2: (id) sender
When the user clicks the HIGH1button, I want to change the background (with image) of the button HIGH2.
How can I achieve this?
You need to create IBOutlet for the buttons to change them.
Control+drag the buttons from Interface Builder to the .h file to create the outlets. Something like this:
#property (weak, nonatomic) IBOutlet UIButton *HIGH1;
#property (weak, nonatomic) IBOutlet UIButton *HIGH2;
//Dont forget to #synthesize
On the IBAction method you then call:
- (IBAction) HIGH1: (id) sender
{
self.HIGH2 setBackgroundImage:[UIImage imageNamed:#"myButtonImage"] forState:UIControlStateNormal];
}
Make sure you have an image with the name you choose in your application bundle, and that the IBOutlet to the buttons is properly connected (The gray circle on the left size of the IBOutlet must have a small black circle inside it)
[HIGH1 addTarget:self
action:#selector(buttonDidPushed)
forControlEvents:UIControlEventTouchUpInside];
then,
-(void)buttonDidPushed
{
HIGH2.backgroundColor = [UIColor blackColor];
[self.view addSubview:HIGH2];
}
I hope this code help you.
access your ivar from HIGH2 and set setBackgroundImage:forState: on HIGH1

IBOutletCollection of UIButtons wont set highlighted

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

Objective-C keep track of my view in subviews array

I have a question about memory management.
For example I have an iPhone application that uses multiply programmatically created views.
for example programmatically generated buttons.
UIButton *myButton=[UIButton alloc] initWithFrame:...; //etc
then, normally we add this button to subviews array:
[self.view addSubview:myButton];
then we releasing button.
[myButton release]
When I need to remove this button how can I keep track on this button in subviews array?
I know I can do this using tag property but I think exists another way to keep connection with it.
You can simply assign it to an instance variable:
UIButton *myButton = ...;
[self.view addSubView:myButton];
myInstanceVariable = myButton;
[myButton release];
You just need to be careful: as soon as you do something like [myInstanceVariable removeFromSuperview]; it might get deallocated immediately (if you haven't retained it) and it would then point to invalid memory.
You can try to declare somewhere a retain property of UIButton* type, that can be assigned with pointer value to your button instance:
#interface myclass
#property (retain, nonatomic) UIButton *savedButton;
#end
#implementation myclass
#synthesize savedButton;
- (void) someMethod...
{
...
UIButton *myButton=[UIButton alloc] initWithFrame:...;
[self.view addSubview:myButton];
self.savedButton = myButton;
[myButton release];
...
}
...
#end

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;
}
}

How can I make a UIButton get a function when I touch outside the button and drag over the button?

If I had a label and a button If i touch outside the button and swiped over the button then an event should be fired to display "hello" in the label field. tell me how can I do it?
Please do following steps.
Take UILabel and UIButton in your XIB file.
Take UILabel and UIButton as IBOutlet in your ViewController's .h file and take property of this two described below.
UILabel *lbl;
UIButton *btn;
#property(nonatomic, retain)IBOutlet UILabel *lbl;
#property(nonatomic, retain)IBOutlet UIButton *btn;
Now set UILabel and UIButton's New Reference outlet from XIB and set UIButton's TouchUpInside link to btnClicked method.
Declare this method in your .h file and implement it in your .m file for button event.
Write this in your .h file
-(IBAction)btnClicked:(id)sender;
and place this code in your .m file
-(IBAction)btnClicked:(id)sender
{
lbl.text = #"Hello";
}
This is simple...
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // init a button
[button addTarget:self action:#selector(buttonDraggedOrTouchedOutSide:)
forControlEvents:UIControlEventTouchDragEnter|UIControlEventTouchDragInside
|UIControlEventTouchUpOutside];
[self.view addSubview:button]; // add button to view
-(void)buttonDraggedOrTouchedOutSide:(id)sender {
//Do your stuff
}
Thats it..Now you can catch drag and touch outside events...