Highlight the UIButtons created Dynamically? - ios

In my application I have created 20 buttons with in a scroll view, now problem is that I was not able to highlight the selected button.
My intention is to show the pressed button with a different look than normal. When another button is pressed the previous one should become normal:
UIButton *Abutton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[Abutton setTag:i-1];
Abutton.frame = CGRectMake(30.0, 0+j, 40.0, 40.0);
[Abutton setTitle:#"" forState:UIControlStateNormal];
Abutton.backgroundColor = [UIColor clearColor];
[Abutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"image1.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[Abutton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"image2.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[Abutton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[Abutton addTarget:self action:#selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:Abutton];
Finally I created the method for Abutton pressed as below:
-(IBAction)buttonpressed:(id)sender{
Abutton.highlighted=YES;
//.....
//.....
}
If do it like this then only the last button created dynamically gets highlighted. That's not exactly what I wanted.

I think you should replace your current code for the button pressed:
-(IBAction)buttonpressed:(id)sender{
UIButton *b = (UIButton *)sender;
b.highlighted = YES;
//.....
//.....
}
In your example you are specifically highlighting "AButton". This code highlights the button being pressed.

Solution 1:
Create an NSSet with references to all the buttons. In your buttonPressed method, call makeObjectsPerformSelector on the NSSet's buttons, setting them to the unhighlighted state.
Solution 2:
Use a UISegmentedControl. That seems like what you should be doing in this case anyway.

Related

How to select single button on UITableView

I have an issue in an UITableView as I need to place a button on each row of the tableview and if i click the button, it gets selected at a time and selecting is like toggle. How to achieve this, please help me out to solve this problem.
Below is the following code that am trying to get it out. I am creating button on cellForRowAtIndexPath as shown below:
UIButton *Btn = [[UIButton alloc]init];
Btn .frame = CGRectMake(3, 10, 33, 30);
[Btn setImage:[UIImage imageNamed:#"deselect.png"] forState:UIControlStateNormal];
[Btn addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
Btn.tag=indexPath.row;
[cell.contentView addSubview:Btn];
and the method: for button action:
-(void)method:(UIButton*)sender
{
NSIndexPath *indexpath1 = [NSIndexPath indexPathForRow:sender.tag inSection:0];
Btn = (UIButton *)sender;
//Btn.tag=sender.tag;
[Btn setImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateNormal];
}
You have an array to store whether the button of a cell is selected or not. In the cellForRowAtIndexPath method, you should set 2 images for 2 states UIControlStateNormal and UIControlStateSelected of the button:
[Btn setImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateSelected];
[Btn setImage:[UIImage imageNamed:#"deselect.png"] forState:UIControlStateNormal];
Then in the method: function, you switch the state of the button via Btn.selected based on the value in the array.
UIImage * btnLinkNormal = [UIImage imageNamed:#"select.png"];
[cell.linkButton setImage:btnLinkNormal forState:UIControlStateNormal];
// Add image to button for pressed state
UIImage * btnLinkHighlighted = [UIImage imageNamed:#"deselect.png"];
[cell.linkButton setImage:btnLinkHighlighted forState:UIControlStateHighlighted];

iOS: How to cover entire button with image

I am currently working on a iOS application where I am constantly changing the pictures of two buttons. The problem is that the picture of the button that constantly changes does not stretch over the entire button. I was wondering what is the best way of doing this.
I believe I am setting my image of my button normally and I cant figure out what to do. Any suggestions will be greatly appreciated.
leftButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#", [orangeImageArray objectAtIndex:randomPicture2]]] forState:UIControlStateNormal];
Is your button type UIButtonTypeCustom ?
example :
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:btnImage forState:UIControlStateNormal];
Try this:
UIImage *btnImage = [UIImage imageNamed:#"image.png"];
[leftButton setImage:btnImage forState:UIControlStateNormal];
This code enables you to customize the button well:
UIImage *img = [UIImage imageNamed:#"icon.png"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake(0, 0, img.size.width, img.size.height);
[btn setBackgroundImage:img forState:UIControlStateNormal];
[btn addTarget:self action:#selector(prefer) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = barButton;

Button is supposed to change image after click, but (randomly) changes images of other buttons too

In my cellForRowAtIndex I initialize a button (it gets shown in every cell in the tableview):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f,cell.frame.size.height -15, 160.0f, 15.0f);
UIImage *buttonImage = [UIImage imageNamed:#"Image1.png"];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button.titleLabel setFont:[UIFont fontWithName:#"Arial" size:13.0]];
button.contentEdgeInsets = UIEdgeInsetsMake(1, 60, 0, 0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cell addSubview:button];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
It has a normal image and if the user clicks on the button, the image changes. Also I get the contents of the button.title from a XML. Therefore I tell it to get the right title from the web. This works fine, but sometimes (not all the time) and also randomly it changes the button from the next cell, or the cell three rows down. I don't know why and couldn't find anything while running with breakpoints/debugger.
-(void)customActionPressed :(id)sender
{
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *pathToCell = [self.tableView indexPathForCell:owningCell];
UIImage *buttonImage = [UIImage imageNamed:#"image2.png"];
[self doXMLParsing];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:pathToCell.row] pressed1]];
[sender setTitle:TableText forState:UIControlStateNormal];
((UIButton *)sender).enabled = NO;
}
So maybe someone can see where I went wrong. If you need more code, just say so.
[button addTarget:self action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
Try having UIControlEventTouchUpInside instead of UIControlEventTouchDown.
also you create a custom cell and then you can create a property in the class subclassing UITableViewCell for your button and change the image of button through that property.
Also Don't change the image from action of button .You can access the button through cell.yourbuttonName in didSelectRowAtIndexPath delegate of tableview and then change the image.

Is there anyway to add same scroll menubar at the navigation bar?

I have already posted my this problem another time but i have not got my answer perfectly.Here i am going to explain my problem another time, it is very important for me so at any cost i have to solve it.Now my problem is...
Suppose, I have 4 tabbaritem in a tabbarController and items "dashboard","order","product","customer".
every item of these tabbar is a calling there respective uiviewcontroller.
dashboar calling "DashboarViewController";
order calling "orderViewController";
product calling "ProductViewController";
customer calling "CustomerViewController";
Now, i have to set a scrolling menubar at every uiviewcontroller and this menu bar containing 4 buttons. These button names are same as tabbar items name "dashboard","order","product","customer".
Now when i press the button of the menu bar then respective controller will show same as for tabbar items. suppose i am pressing "order" tabbar item then it will show the "orderviewcontroller". when i will see this view controller it will also show me that menu bar at the top of the viewcontroller.Now, if i am click "product" button in this "orderviewcontroller" then it will sent back to me "productViewcontroller".
thats mean tabbar item and button of the scroll menubar will work same.
still now i have done these, my previous post image
How can i make same button in multiple view controller?
If some know how can do that then please explain it step by step.I do not need to give any code from you.just explain it step by step how can i do that after reading my previous post
Thanks In Advance.
Ha ha ha .....it was so much fun when i solved it.whatever i solved this problem in different way, i did not use scrollview button controller for controller just, in every controller i have made function to where buttons within a scrollview create and on action of button i have just change the selected index of the tabbar controller.
in -(void)viewDidload i wrote this code
UIView *scrollViewBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
scrollViewBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"topmenu_bg.png"]];
menuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5,0,320,40)];
menuScrollView.showsHorizontalScrollIndicator = FALSE;
menuScrollView.showsVerticalScrollIndicator = FALSE;
menuScrollView.bounces = TRUE;
[scrollViewBackgroundView addSubview:menuScrollView];
[self.view addSubview:scrollViewBackgroundView];
[self createMenuWithButtonSize:CGSizeMake(92.0, 30.0) withOffset:5.0f noOfButtons:7];
here is the button create and action
-(void)mybuttons:(id)sender{
NSLog(#"mybuttons called");
UIButton *button=(UIButton *)sender;
NSLog(#"button clicked is : %iBut \n\n",button.tag);
int m = button.tag;
for(int j=0;j<8;j++){
if(button.tag == m){
self.tabBarController.selectedIndex = m;
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_hover.png"] forState:UIControlStateHighlighted]; //sets the background Image]
}
if(button.tag != m){
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
}
}
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons{
NSLog(#"inserting into the function for menu bar button creation");
for (int i = 0; i < totalNoOfButtons; i++) {
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[button addTarget:self action:#selector(mybuttons:) forControlEvents:UIControlEventTouchUpInside];
(button).titleLabel.font = [UIFont fontWithName:#"Arial" size:12];
if(i==0){
[button setTitle:[NSString stringWithFormat:#"Dashboard"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_hover.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==1){
[button setTitle:[NSString stringWithFormat:#"Order"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==2){
[button setTitle:[NSString stringWithFormat:#"Product"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==3){
[button setTitle:[NSString stringWithFormat:#"Customers"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==4){
[button setTitle:[NSString stringWithFormat:#"Content"] forState:UIControlStateNormal];//with title
}
if(i==5){
[button setTitle:[NSString stringWithFormat:#"Site Analysis"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==6){
[button setTitle:[NSString stringWithFormat:#"Store Settings"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==7){
[button setTitle:[NSString stringWithFormat:#"CMS Settings"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
button.frame = CGRectMake(i*(offset+buttonSize.width), 6.0, buttonSize.width, buttonSize.height);
button.clipsToBounds = YES;
button.showsTouchWhenHighlighted=YES;
button.layer.cornerRadius = 5;//half of the width
button.layer.borderColor=[UIColor clearColor].CGColor;
button.layer.borderWidth=0.0f;
button.tag=i;
[menuScrollView addSubview:button];
}
menuScrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
[self.view addSubview:menuScrollView];
}

UIButton in IOS responds to first touch and then does not respond to any subsequent touch events

I have created a UIButton programmatically that correctly changes background color as desired when button is touched down for the first time. After that, the button does not respond to any subsequent touch events.
I have instrumented the color change method with NSLog and this method is not called after the first touch, even though the button visually appears to respond to subsequent touches on the iPhone screen. It seems the subsequent touches are not actually triggering any event (eg. TouchDown), but I can't figure out why not.
I have searched every post on sof related to this issue, but none of the answers seem to solve my problem.
The button is declared in the header file as:
UIButton *playerOneButton;
UIButton *playerTwoButton;
Here is the button initialization code that I use after the UIViewController loads:
- (void)viewDidLoad
{
[super viewDidLoad];
//Create initial view of Button One before any selection is made
playerOneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playerOneButton.frame = CGRectMake(5.0f, 20.0f, 149.0f, 31.0f);
[playerOneButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15.0]];
[playerOneButton setTitle:playerOneName forState:UIControlStateNormal];
[self.view addSubview:playerOneButton];
//Create initial view of Button Two before any selection is made
playerTwoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playerTwoButton.frame = CGRectMake(166.0f, 20.0f, 149.0f, 31.0f);
[playerTwoButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15.0]];
[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal];
[self.view addSubview:playerTwoButton];
// Add correct names to both buttons
[playerOneButton setTitle:playerOneName forState:UIControlStateNormal];
[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal];
// Link buttons to methods that will toggle colors between Green for selected button and Red for unselected button
[playerOneButton addTarget:self action:#selector(setPlayerOneButtonStatus) forControlEvents:UIControlEventTouchDown];
[playerTwoButton addTarget:self action:#selector(setPlayerTwoButtonStatus)
forControlEvents:UIControlEventTouchDown];
}
and the method that is invoked to actually change the colors:
-(void) setPlayerOneButtonStatus;
{
playerOneButton.selected = YES;
// Load colored images
UIImage *imageGreen = [UIImage imageNamed:#"button_green.png"];
UIImage *imageRed = [UIImage imageNamed:#"button_red.png"];
// And create the stretchy versions.
float wGreen = imageGreen.size.width / 2, hGreen = imageGreen.size.height / 2;
UIImage *stretchGreen = [imageGreen stretchableImageWithLeftCapWidth:wGreen topCapHeight:hGreen];
float wRed = imageRed.size.width / 2, hRed = imageRed.size.height / 2;
UIImage *stretchRed = [imageRed stretchableImageWithLeftCapWidth:wRed topCapHeight:hRed];
// Now create button One with Green background color
playerOneButton = [UIButton buttonWithType:UIButtonTypeCustom];
playerOneButton.frame = CGRectMake(5.0f, 20.0f, 149.0f, 31.0f);
[playerOneButton setBackgroundImage:stretchGreen forState:UIControlStateNormal];
[playerOneButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15.0]];
[playerOneButton setTitle:playerOneName forState:UIControlStateNormal];
[self.view addSubview:playerOneButton];
// Now create button Two with Red background color
playerTwoButton = [UIButton buttonWithType:UIButtonTypeCustom];
playerTwoButton.frame = CGRectMake(166.0f, 20.0f, 149.0f, 31.0f);
[playerTwoButton setBackgroundImage:stretchRed forState:UIControlStateNormal];
[playerTwoButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15.0]];
[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal];
[self.view addSubview:playerTwoButton];
}
You are creating a new playerOneButton in the action method without assigning a target/action to it.

Resources