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];
}
Related
How I can set UIImage in the normal state and set title in the selected state in my UIButton?
Hi, I try to set a UIImage in the normal state of UIButton, but when set an UImage in normal state this change the title of UIButton in the selected state changing the title for the image . I am using
myButton setTitle:content forState:UIControlStateSelected
in the state selected.
How I can set UIImage in the normal state and set title in the selected state in my UIButton?
PDT: sorry for my english!.
Try This:
BOOL selected;
-(IBAction)btnClicked:(UIButton *)sender{
if(selected)
{
[chkBtn setTitle:#"selected" forState:UIControlStateNormal];
chkImage.image=[UIImage imageNamed:#" "];
NSLog(#"%#",chkBtn.titleLabel);
selected=NO;
}
else
{
[chkBtn setTitle:#" " forState:UIControlStateNormal];
chkImage.image=[UIImage imageNamed:#"button_minus_pink.png"];
selected=YES;
}
}
// set with UIColorĀ
[self.button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
// set with UIImage
[self.button setBackgroundImage:[UIImage imageNamed:#"BlackRightBtn.png"] forState:UIControlStateNormal];
For Button Selected
if (self.button.isSelected == YES)
{
[self.button setTitle:#"Your_ButtonTitle" forState:UIControlStateNormal];
}
add your code like this:
[myButton setTitle:#"Your String" forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton setTitle:#"" forState:UIControlStateNormal];
where img.png should not be there in your bundle
you can set this through Interface Builder as well.
You can define button and setImage for normal and selected state. Like this way.
buttonJobs = [UIButton buttonWithType:UIButtonTypeCustom ];
buttonJobs.tag = 1;
[buttonJobs setTitle:#"Jobs" forState:UIControlStateNormal];
buttonJobs.frame = CGRectMake(2, 56, 109, 33);
[buttonJobs setImage:[UIImage imageNamed:#"tabButtonImage_Off.png"] forState:UIControlStateNormal];//buttonJobs.png
[buttonJobs setImage:[UIImage imageNamed:#"tabButtonImage_On.png"] forState:UIControlStateDisabled];
[buttonJobs addTarget:self action:#selector(btnNextTab:) forControlEvents:UIControlEventTouchUpInside];
And in button selector method you can set your button selected according sender's tag.
-(void)btnNextTab:(UIButton*)sender
{
if(sender.tag == 1)
{
sender.selected = YES;
}
}
And for Title you can change color of text.
Hope this will help you.
Check with this code:
[myButton setTitle:content forState:UIControlStateSelected];
[myButton setImage:nil forState:UIControlStateSelected];
[myButton setImage:yourImage forState:UIControlStateNormal];
Try this code:
[myButton setTitle:#"Your String" forState:UIControlStateSelected];
[myButton setImage:nil forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton setTitle:#"" forState:UIControlStateNormal];
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.
I'm customizing the UIButton programmatically here:
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setSelected:YES];
button.frame = CGRectMake(x, y, width, height);
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitle:#"Button Title" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateDisabled];
The issue is if I'm holding down the image background image disappears until I'm releasing it...
I think you're in overkill mode :). Try setting button.png for UIControlStateNormal and buttonActive.png for UIControlStateHighlighted. No need for the rest. See if this works.
EDIT:
Also, remember: Image file names are case sensitive
Are you testing on device? Image names are case sensitive for device builds, but not for the simulator. For example, if your actual image file is named buttonactive.png, but you're calling it as buttonActive.png from your code, it will show up on the simulator, but not on the device.
Please ensure that the case for both image names matches the name of the actual file.
EDIT #2:
Try this code
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setSelected:YES];
button.frame = CGRectMake(x, y, width, height);
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitle:#"Button Title" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateHighlighted];
Figure it out, it works in this way:
[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:#"ActivateButton.png"] forState:UIControlStateSelected];
[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:#"ActivateButton.png"] forState:(UIControlStateHighlighted|UIControlStateSelected)];
While we add a button from IDE
sample:
.h file
-(IBAction)BtnAdd:(id)sender
in .m file it is
-(IBAction)BtnAdd:(id)sender
{
}
It is a method that cannot enable or disable.
so if you want to enable or disable button make it as -(IBOutlet)BtnAdd
add IBOutlet to .h file And connect it to the particular button
then BtnAdd.enabled=NO will work
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];
}
}
i need to place a check box in UItableview.
For that i place a box with out check image is place on button.
my code is,
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=CGRectMake(100, 10, 100, 100);
[customButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:customButton];
Now i need to change button image when button clicked.
where can i done this.
and how can i impalement this.
can any one please help me.
Thank u in advance.
//add this line
[customButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//then
-(void) buttonClicked:(id) sender
{
UIButton * button = (UIButton*) sender;
[button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateNormal];
}
You can write this line of code
[customButton setImage:[UIImage imageNamed:#"unselected2.png"] forState:UIControlEventTouchDown];
before
[cell.contentView addSubview:customButton];
and you ca select different images for different events and states