Change UIButton image within a cell - objective-c

I am having difficulty in changing the image of my UIButton which is contained within a table view cell. My code as follows:
// In tableview for cell at rowIndex method
self.iconBtn = [[UIButton alloc]initWithFrame:CGRectMake(670,10,80, 80)];
self.iconBtn.tag = kIconValueTag;
[cell.contentView addSubview:self.iconBtn];
//Add icon to cell
UIImage *btnImage = [UIImage imageNamed:#"blank_star.png"];
[self.iconBtn setImage:btnImage forState:UIControlStateNormal];
// I am calling method changeIconState after user clicks on icon.
[self.iconBtn addTarget:self action:#selector(changeIconState) forControlEvents:UIControlEventTouchUpInside];
After the button is pressed, a method outside the table view method is called:
-(void)changeIconState
{
if (self.iconSelectState == kIconNotSelected)
{
self.iconSelectState = kIconSelected;
}
else
{
self.iconSelectState = kIconNotSelected;
}
[self changeIcon];
}
-(void)changeIcon
{
if (self.iconSelectState == kIconSelected)
{
UIImage *btnImageHighlighted = [UIImage imageNamed:#"star.png"];
[self.iconBtn setImage:btnImageHighlighted forState:UIControlStateNormal];
}
else
{
UIImage *btnImageNormal = [UIImage imageNamed:#"blank_star.png"];
[self.iconBtn setImage:btnImageNormal forState:UIControlStateNormal];
}
}
After I run the program, the icon did not change from blank to star as I wanted. It just remained as a blank star. Is there anything I am missing out here?

Why are you managing the image/button state yourself? Why not load the two images for the defined states and let the OS handle it?
//Add icon to cell
UIImage *btnImage = [UIImage imageNamed:#"blank_star.png"];
UIImage *btnImageHighlighted = [UIImage imageNamed:#"star.png"];
[self.iconBtn setImage:btnImage forState:UIControlStateNormal];
[self.iconBtn setImage:btnImageHighlighted forState:(UIControlStateHighlighted && UIControlStateSelected)];
If you need to manage your property (iconSelectedState) you can without worrying about the image. Alternatively, you can query the buttons state property to determine which state it is in.

Related

Remove Buttons Dynamically with an Array

I have a view that has labels being dynamically added by the user. If the use would like to edit any of the labels, they press a button and all the labels get highlighted with a delete button and move button. (Editing is another bridge I’ll cross later).
My issue is: What is the best way to turn the buttons on and off? I have a method that turns the buttons on… but I am at a loss as to how I turn them off when done editing. Do I need to tag my buttons and then just ‘hide them’? Or do I just remove them all totally? How do I parse all the buttons that are turned on then, turn them off. Do I need to put them in an array as well? The labels are tagged with unique numbers so I know which label is which.
Any thoughts? Guidance? If I am doing it all wrong please tell me.
Here is a couple methods I have:
- (void) showEditableText {
// Parse the array of labels
if(textArray.count > 0){
for(UILabel *label in textArray){
//Add Delete Button
UIImage * delButtonImage = [UIImage imageNamed:#"GUI_Delete.png"];
UIButton * delThisButton = [[UIButton alloc] initWithFrame:CGRectMake(label.frame.origin.x - delButtonImage.size.width, label.frame.origin.y - delButtonImage.size.height, delButtonImage.size.width, delButtonImage.size.height)];
[delThisButton setBackgroundImage:delButtonImage forState:UIControlStateNormal];
[delThisButton addTarget:self action:#selector(deleteThisLabel:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:delThisButton];
//Add a move button
UIImage * moveButtonImage = [UIImage imageNamed:#"GUI_Move.png"];
UIButton * moveThisButton = [[UIButton alloc] initWithFrame:CGRectMake((label.frame.origin.x + label.frame.size.width + moveButtonImage.size.width), label.frame.origin.y - moveButtonImage.size.height, moveButtonImage.size.width, moveButtonImage.size.height)];
[moveThisButton setBackgroundImage:moveButtonImage forState:UIControlStateNormal];
[moveThisButton addTarget:self action:#selector(moveThisLabel:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:moveThisButton];
//Make the text highlighed
label.highlighted = YES;
label.backgroundColor = [UIColor colorWithRed:203/255.0f green:230/255.0f blue:239/255.0f alpha:1];
label.highlightedTextColor = [UIColor redColor];
}
}
}
- (void) doneEditingText {
if(textArray.count > 0){
for(UILabel *label in textArray){
//THIS IS WHERE I AM STUCK? WHAT DO I DO?
label.highlighted = NO;
label.backgroundColor = [UIColor clearColor];
}
}
}
//inside your first method set the same tag to your all buttons
-(void) showEditableText {
........
.......
.......
delThisButton.tag = 10;
moveThisButton.tag = 10;
}
//inside your second method delete all the subviews using this tag as shown below..
-(void) doneEditingText {
if(textArray.count > 0){
for(UILabel *label in textArray){
..............................
//THIS IS WHERE I AM STUCK? WHAT DO I DO?
for (UIView *subview in [self.view subviews]) {
if (subview.tag == 10) {
[subview removeFromSuperview];
}
}
...............................
label.highlighted = NO;
label.backgroundColor = [UIColor clearColor];
}
}
}
Move all your buttons code to viewDidLoad, hide them (button.hidden = YES). When you start/finish editing your text, unhide and hide your buttons. You need to have an ivar to contain the buttons too. So add them in your .h file.
Try This
UIView * seletetButton = nil;
for (UIView * btn in self.view.subviews){
if ([btn isKindOfClass:[UIButton class]])
{
if (seletetButton.tag != btn.tag)
{
[btn removeFromSuperview];
}
}
}

How do I add resizing without IB

Since starting with iPhone app development (last 9 months) I have only used IB. I have a project to work on already built by another developer that I need to optimise for iPhone screen. No problem in IB, I know how to do that, in this project however the Nav bar is added using code only and is an image view. Could someone advise me how I go about resizing/positioning the nav bar when IB isnt used? Im trying to enhance this app for the iphone 5 screen.
#define BAR_FRAME CGRectMake(0,0,320.0f,43.0f)
#implementation ICNavbarView
#synthesize homeButton=__homeButton;
#synthesize prevButton=__prevButton;
#synthesize nextButton=__nextButton;
#synthesize delegate=__delegate;
- (id)initWithFrame:(CGRect)frame
{
LogCmd();
self = [super initWithFrame:BAR_FRAME];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.alpha = 0.9f;
// Add Navigation bar background // <<<<<< navigation bar from ui image
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BgNavBarNew"]];
[self addSubview:bgView];
// Add back button
__prevButton = [UIButton buttonWithType:UIButtonTypeCustom];
__prevButton.frame = CGRectMake(30.0f, 6.0f, 29.0f, 31.0f);
UIImage *prevButtonPressed = [UIImage imageNamed:#"BtnPrevPressed"];
[__prevButton setImage:[UIImage imageNamed:#"BtnPrev"] forState:UIControlStateNormal];
[__prevButton setImage:prevButtonPressed forState:UIControlStateSelected];
[__prevButton setImage:prevButtonPressed forState:UIControlStateHighlighted];
[__prevButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__prevButton];
// Add next button
__nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
__nextButton.frame = CGRectMake(262.0f, 6.0f, 29.0f, 31.0f);
UIImage *nextButtonPressed = [UIImage imageNamed:#"BtnNextPressed"];
[__nextButton setImage:[UIImage imageNamed:#"BtnNext"] forState:UIControlStateNormal];
[__nextButton setImage:nextButtonPressed forState:UIControlStateSelected];
[__nextButton setImage:nextButtonPressed forState:UIControlStateHighlighted];
[__nextButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__nextButton];
// Add home button
__homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
__homeButton.frame = CGRectMake(145.0f, 6.0f, 31.0f, 30.0f);
UIImage *homeButtonPressed = [UIImage imageNamed:#"BtnHomePressed"];
[__homeButton setImage:[UIImage imageNamed:#"BtnHome"] forState:UIControlStateNormal];
[__homeButton setImage:homeButtonPressed forState:UIControlStateSelected];
[__homeButton setImage:homeButtonPressed forState:UIControlStateHighlighted];
[__homeButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__homeButton];
}
return self;
}
- (id)init
{
return [self initWithFrame:CGRectZero];
}
#pragma mark - Button handlers
- (void)buttonPressed:(id)sender
{
if (sender == __prevButton) {
[self.delegate performSelector:#selector(navBarPrevButtonPressed)];
} else if (sender == __homeButton) {
[self.delegate performSelector:#selector(navBarHomeButtonPressed)];
} else {
[self.delegate performSelector:#selector(navBarNextButtonPressed)];
}
}
#end
So far I tried UIViewAutoresizingFlexibleHeight; like this
// Add Navigation bar background
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BgNavBarNew"]];
[self addSubview:bgView];
//resize
bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
This seems to make no difference and the nav bar hasn't moved
I've used this before, but I used the setter function instead of using the property. I don't know if that would change anything or not, but try it this way:
[bgView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
The parens are important, if you have multiple re-sizing options.

Change UISegmentedControl selectedSegmentIndex doesn't change the selected index

I'm trying to create a toggle button in a navigation bar in iOS5 with UISegmentControl. The problem I'm having is that setSelectedSegmentIndex isn't persistent, i.e. when the users clicks one of the segments, it's action gets called but it isn't set as selected. My current code looks like this:
// In viewDidLoad
toolSegment = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"ToolPenIcon.png"],
[UIImage imageNamed:#"TextIcon.png"],
nil]];
[toolSegment addTarget:self action:#selector(changeMode:) forControlEvents:UIControlEventValueChanged];
toolSegment.frame = CGRectMake(0, 0, 85, 30);
toolSegment.segmentedControlStyle = UISegmentedControlStyleBezeled;
toolSegment.momentary = YES;
[toolSegment setSelectedSegmentIndex:1];
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:toolSegment];
self.navigationItem.rightBarButtonItem = segmentBarItem;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
- (void)changeMode:(id)sender
{
UISegmentedControl * control = (UISegmentedControl*)sender;
if (control.selectedSegmentIndex == 0) {
NSLog(#"Print");
[toolSegment setSelectedSegmentIndex:0];
} else {
// Should change the selected index.
[toolSegment setSelectedSegmentIndex:1];
[pageView changeToText];
if (pageView.canvas.image != nil) {
[self createNewPage];
}
writing = YES;
}
}
Any tips on what I might be doing wrong would be appreciated.
You probably don't want the control to be in momentary mode as this would allow multi selection. Set momentary to NO.

how to change the button image inside the table cell while clicking the button in iphone

I have a tableview in my application. I created a button inside the table cell and assigned a background image to it. I wanted to change the background image when i click that button. I assigned an action for the button like this
-(void)goodBtnClicked:(id)sender {
[goodBtn setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState:UIControlStateNormal];
}
But, the image is not getting replaced with the new image. Can any one help me with this?
You should make sure that your image is actually there.
-(void)goodBtnClicked:(id)sender {
UIImage *img = [UIImage imageNamed:#"camera.png"];
NSAssert(img, #"Image shouldn't be nil");
[goodBtn setBackgroundImage:img forState:UIControlStateNormal];
}
try with
-(void)goodBtnClicked:(id)sender {
[sender setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState:UIControlStateNormal];
}
Go through all the subviews and locate your button then change background image
- (void)buttonPressed:(UIButton *)sender{
UITableViewCell *cell = (UITableViewCell *)sender.superview;
for (UIView *sub in cell.subviews) {
if([sub isMemberOfClass:[UIButton class]])
{
UIButton * button=[[UIButton alloc] init];
button=sub;
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
}
}
}

Change UIbutton Image on click

I have stored 5 images in an mutable array and displayed them randomly on iPhone view by appending them in UIButton . now I want to change the image on a button on which I will click. but in my code only the last image changes not the image on which I called the action.
Here is the code for 3 buttons which highlights the clicked button
-(void) changeButtonImage:(id) sender{
[button1 setBackgroundImage:[UIImage imageNamed:#"button1Image_off.png"] forState:UIControlStateNormal];
[button2 setBackgroundImage:[UIImage imageNamed:#"button2Image_off.png"] forState:UIControlStateNormal];
[button3 setBackgroundImage:[UIImage imageNamed:#"button3Image_off.png"] forState:UIControlStateNormal];
UIButton *button = sender;
if (button.tag == 0) {
[button1 setBackgroundImage:[UIImage imageNamed:#"button1Image_on.png"] forState:UIControlStateNormal];
}else if (button.tag == 1) {
[button2 setBackgroundImage:[UIImage imageNamed:#"button2Image_on.png"] forState:UIControlStateNormal];
}else if (button.tag == 2) {
[button3 setBackgroundImage:[UIImage imageNamed:#"button3Image_on.png"] forState:UIControlStateNormal];
}
}
hope this helps...
hAPPY cODING...
If you are looking for how to change the image on a button just do:
[myButton setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
You can have it loop through your array but creating a variable to store what image index you are on. Then just go to the next one using the statement above to assign the image.
Do you have an Outlet (IBOutlet) for each of the buttons. You should list each one as an outlet, and just use Interface Builder to connect each button to those variables. Then create a function for the touchUpInside event of each button. Make this buttonPressed. make this function something like:
-(void) buttonPressed:(id) sender
{
((UIButton *)sender).image = [UIImage imageNamed:#"Image.png"];
}
You will want to set a variable like currentImage to track what image is set. Each time you click increase that variable (currentImage++). If it gets > some final amount set it back to 0. Then you can just do
if (currentImage == 0) { set first image; } else if (currentImage == 1) { set second image.. }
etc...
Does this help?