Unable to implement radio button in iPhone - iphone

I am new to iPhone,
I want to implement radio button in my app, there are three buttons in my app, button should behave like radio button.
Here is my code snippet,
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn1 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn1 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn2 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn2 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn3 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn3 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
- (IBAction)RadioButton:(UIButton *)button{
for (UIButton *Radiobutton in [self.view subviews]) {
if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
[Radiobutton setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
Any help will be appreciated.

You can use following code.
//Add all the buttons to class level NSMutable array
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn1 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
[self.buttonsArray addObject:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn2 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
[self.buttonsArray addObject:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn3 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
[self.buttonsArray addObject:Btn3];
- (IBAction)RadioButton:(id)sender{
[self resetAllButtons];
UIButton* button=(UIButton*) sender;
[button setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateNormal];
}
-(void) resetAllButtons{
for(int i=0;i<[self.buttonsArray count];i++){
UIButton* button=[self.buttonsArray objectAtIndex:i];
[button setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
}
}

You are checking all views in self.view.subviews but the buttons are actually added to the UIScrollView (I guess) called scrollVw.
Change the for loop to
for (UIButton *Radiobutton in [self.scrollVw subviews]) {
if the scrollVw is added as a property in the .h file.

Any opposition to using a UISegmentedControl? It works like a radio button, in that it can only have one option selected at a time. If you are deadset on the appearance of radio buttons I would subclass it to change the appearance.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISegmentedControl_Class/Reference/UISegmentedControl.html

Alternatively you could simply use the iOS provided Control for something like that:
UISegmentedControl
It can easily handle three segments, is quite flexible and can behave like a radio button where you can select only one segment or multiple.

Related

How do I get a UIButton to change its color by clicking it?

I want to make an app for iphone. It is a Nonogram something similar to this: http://www.puzzle-nonograms.com/ How can I make the UIButtons changes their color from white to black and reverse?
.h file
UIButton *button;
BOOL clicked;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
clicked=YES;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(changeBtnColor)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#" 1 " forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
-(void)changeBtnColor{
if (clicked) {
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
clicked=NO;
}
else{
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
clicked=YES;
}
}
I hope it will help. Best of luck..
No need to ivar a boolean, buttons have selected, highlighted and normal states built into them.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 320, 50)];
[button addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Normal State" forState:UIControlStateNormal];
[button setTitle:#"Selected State" forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"normal_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[self.view addSubview:button];
}
-(void)buttonPress:(UIButton *)button{
button.selected = !button.selected;
//do your other code here for button selection
}
Otherwise set up the button states inside interface builder and just have the buttonPress selector handle the state change.

How to get button background image in iPhone?

Currently i am working in iPhone application, I have create two button and set image for both buttons, when the user touch button2 then show button1 image inside button2, i tried my level best, but i can't fix this, please any one help me
Thanks in Advance
I tried this:
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:#"4.png"] forState:UIControlStateNormal];
btn1.frame=CGRectMake(61, 60, 50, 50);
[btn1 addTarget:self action:#selector(Button1Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:#"8.png"] forState:UIControlStateNormal];
btn2.frame=CGRectMake(112, 60, 50, 50);
[btn2 addTarget:self action:#selector(Button2Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
-(void)Button2Method
{
[btn2 setImage:[UIImage imageNamed:btn1.currentBackgroundImage] forState:UIControlStateNormal];
}
Try this:-
[btn2 setImage:btn1.currentImage forState:UIControlStateNormal];
And currentBackgroundImage is an UIImage and you're treating it as a NSString this is wrong.
Use this
UIImage *img = btn1.currentImage;
[btn2 setImage:img forState:UIControlStateNormal];
This is because you are setting image to button btn1 setImage: and accessing background image btn1.currentBackgroundImage
so change your code Tested
-(void)Button2Method
{
[btn2 setImage:btn1.imageView.image forState:UIControlStateNormal];
}

Changing image on UIButton when user presses that button removing previous image

I want to change an image on my UIButton when a user presses on it. The image has transparency effects (it is a .png that shows only an edge). The problem is that when the button is tapped, all works fine (other functions connected to it), but the app overwrites the 2 different images one over the second! When the button is pressed I want to show the second image, and remove the first (that becomes unnecessary). PS: I don't want to use array of images. Here the code for the button
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside]; //just graphics
[overlayButton addTarget:self action:#selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside]; //just to set a variable i++ for my cycle
[overlayButton addTarget:self action:#selector(changeshape:) forControlEvents:UIControlEventTouchUpInside]; //function that changes another shape
[[self view] addSubview:overlayButton];
my simple cycle
- (void)buttonshapeClicked:(id)sender{
shape++;
if (shape == 4) {
shape = 1;
}}
here the main function that allows to change a primary shape on the screen
-(void) changeshape:(id)sender {
if (shape==1 && people==2) {
//bottone forma
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"passaquadrato.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
//bottone people
UIButton *overlayButtonp = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButtonp setImage:[UIImage imageNamed:#"passatre.png"] forState:UIControlStateNormal];
[overlayButtonp setFrame:CGRectMake(180, 382, 120, 60)];
[overlayButtonp addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButtonp];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"circle2.png"]];
[overlayImageView setFrame:CGRectMake(20, 88, 280, 280)];
[[self view] addSubview:overlayImageView];
}
else if (shape==2 && people==2) {
//bottone forma
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"passarettangolo.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:#selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
//bottone people
UIButton *overlayButtonp = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButtonp setImage:[UIImage imageNamed:#"passatre.png"] forState:UIControlStateNormal];
[overlayButtonp setFrame:CGRectMake(180, 382, 120, 60)];
[overlayButtonp addTarget:self action:#selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButtonp];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"square2.png"]];
[overlayImageView setFrame:CGRectMake(20, 88, 280, 280)];
[[self view] addSubview:overlayImageView];
}}
Try like this:
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:#selector(setRightActionToButton:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
-(void)setRightActionToButton:(id)sender {
UIButton *button = (UIButton *)sender;
if (shape==1 && people==2) {
[button setImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateNormal];
[self scanButtonPressed:button]
}
else if (shape==2 && people==2) {
[button setImage:[UIImage imageNamed:#"image3.png"] forState:UIControlStateNormal];
[self buttonshapeClicked:button]
}
else if (shape==3 && people==2) {
[button setImage:[UIImage imageNamed:#"image4.png"] forState:UIControlStateNormal];
[self changeshape:button];
}
}

Title for UIButton won't appear

So I am trying to create a custom UIBarButtonItem, but I can't for the life of me figure out why the title won't show. Maybe someone else can spot it :\
Here is the code I use to create the bar button:
+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)title normal:(UIImage *)normal highlighted:(UIImage *)highlight selected:(UIImage *)selected target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0.0f, 0.0f, normal.size.width, normal.size.height)];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setImage:normal forState:UIControlStateNormal];
[button setImage:selected forState:UIControlStateHighlighted];
[button setImage:selected forState:UIControlStateSelected];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:#"Cancel"
normal:[UIImage imageNamed:#"back_button_active.png"]
highlighted:[UIImage imageNamed:#"back_button_highlight.png"]
selected:[UIImage imageNamed:#"back_button_selected.png"]
target:self
action:#selector(popViewController)];
Any help would be appreciated!
P.S. This is in iOS 5 using ARC.
title and image are mutually exclusive properties in UIButton. If you want to have title with custom background then you should use backgroundImage property:
...
[button setBackgroundImage:normal forState:UIControlStateNormal];
[button setBackgroundImage:selected forState:UIControlStateHighlighted];
[button setBackgroundImage:selected forState:UIControlStateSelected];
...

Title on custom UINavigationItem is not shown

I'm creating custom UINavigationItem with custom background image and title. The image is shown but the title doesn't. What could be wrong?
I'm doing it this way:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateHighlighted];
[button setTitle:#"Cancel" forState:UIControlStateNormal];
[button setTitle:#"Cancel" forState:UIControlStateSelected];
[button setTitle:#"Cancel" forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 49, 30)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
You need to set image as backgroundImage not direct. If you set image like this
[button setImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
the title will not be displayed
set like this
[button setBackgroundImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
I think you must remove:
[button setImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
After that, the title will appear
Either remove this line
[button setImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
or you can change the Title color to some other color.i hope it will solve your problem.
make it to DrakgrayColor or some other color which can overcome your image color.
OK. Found a workaround, however I'm not sure whether it is allowed:
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Close",#"Close") style:UIBarButtonItemStylePlain target:self action:#selector(closeButtonClicked)];
[self.navigationItem setLeftBarButtonItem:closeButton];
for (UIView *view in self.navigationController.navigationBar.subviews) {
if ([[[view class] description] isEqualToString:#"UINavigationButton"]) {
UINavigationButton *button = {(UINavigationButton *)view};
[button setBackgroundImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateHighlighted];
}
}