cocoa:Multiple buttons to set the picture,Tab effect - objective-c

There are several buttons,Click on one of,Change the picture of this button,Other button to set another picture。The following code where there is an error?
NSButton *button = (NSButton *)sender;
NSInteger tag = button.tag;
[button setImage:[NSImage imageNamed:#"menuBtnHover.png"]];
for (int i = 5; i <= 8; i++) {
if (tag != i) {
button =[(NSButton *)[self contentView] viewWithTag:i];
[button setImage:[NSImage imageNamed:#"menuBtn.png"]];
}
}
Add test,button = null
NSLog(#"button tag: %ld %d %#", tag,i ,button);

use this code
NSButton *button = (NSButton *)sender;
NSInteger tag = button.tag;
[button setImage:[NSImage imageNamed:#"menuBtnHover.png"]];
for (int i = 5; i <= 8; i++) {
if (tag != i) { button =[(NSButton *)[self contentView] viewWithTag:i];
if(button)[button setImage:[NSImage imageNamed:#"menuBtn.png"]];}
}

Related

Highlighting the button with sender tag

I have 7 buttons on my storyboard, I have associated a tag number to each of the button. And all the buttons are hooked up to a single IBAction.
In my action method I have a switch statement like switch ([sender tag])
which run the appropriate action according to the tag. This is all working.
But I want to add a functionality where selected button is highlight and rest of them in normal state.
You can create property with tags:
#property (nonatomic, strong) NSArray *tags;
Somewhere (for example, in viewDidLoad) initialize it with values used in storyboard:
tags = #[#1, #2, #3, #4, #5]
And select buttons using this tags
- (IBAction)buttonPressed:(UIButton *)sender {
for (int i = 0; i < tags.count; i++) {
UIButton *button = [self.view viewWithTag:tags[i]];
button.selected = (button.tag == sender.tag);
}
}
Or you can create IBOutlets for every 7 buttons and create array for it.
array = #[outlet1, ..., outlet7]
And select buttons using outlets
- (IBAction)buttonPressed:(UIButton *)sender {
for (int i = 0; i < array.count; i++) {
UIButton *button = array[i];
button.selected = (button.tag == sender.tag);
}
}
Hope this can give you some idea:
- (void)setupButtons {
for (int i = 0; i < 7; i++) {
CGFloat width = self.view.frame.size.width / 7;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * width, 100, width, 30)];
[self.view addSubview:button];
button.tag = 1000 + i;
[button setTitle:[NSString stringWithFormat:#"%d", i] forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
if (i == 0) {
// default first button selected
button.selected = YES;
}
}
}
- (void)buttonClicked:(UIButton *)sender {
for (int i = 0; i < 7; i++) {
UIButton *button = [self.view viewWithTag:(1000+i)];
button.selected = (button.tag == sender.tag);
}
}

How do I change a button's color when pressed and reset to original color when a different button are pressed?

I have created buttons dynamically based on array count ,if i pressed it will move to next page .i want to change the background color of the button if it is pressed .i pressed 1st button its background color should changed ,and den if i pressed anyother buttons the first pressed button should get into default color of the button , and the new pressed button's background color should changed ,
please help me to do this ,On button clicked method i have tried like this ,
- (IBAction)btn1Tapped:(id)sender {
UIButton *btn = (UIButton *) sender;
selected = YES;
if (selected) {
[btn setBackgroundColor:[UIColor redColor]];
}
}
and this my button creation code ,
int buttonheight = 30;
int horizontalPadding = 20;
int verticalPadding = 20;
int totalwidth = self.view.frame.size.width;
int x = 10;
int y = 150;
for (int i=0; i<array.count; i++)
{
NSString* titre = [array objectAtIndex:i];
CGSize contstrainedSize = CGSizeMake(200, 40);//The maximum width and height
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:20.0], NSFontAttributeName,
nil];
CGRect frame = [titre boundingRectWithSize:contstrainedSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];
int xpos = x + CGRectGetWidth(frame);
if (xpos > totalwidth) {
y =y +buttonheight+ verticalPadding;
x = 10;
}
UIButton *word= [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.word = word;
NSLog(#"%#", NSStringFromCGRect(frame));
word = [UIButton buttonWithType:UIButtonTypeRoundedRect];
word.frame = CGRectMake(x, y, CGRectGetWidth(frame)+5, CGRectGetHeight(frame));
[word setTitle:titre forState:UIControlStateNormal];
[word setTitle:titre forState:UIControlStateSelected];
word.backgroundColor = [UIColor colorWithRed:30.0/255.0 green:134.0/255.0 blue:255.0/255.0 alpha:1.0];
[word setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[word setTag:i];
[word addTarget:self action:#selector(btn1Tapped:) forControlEvents:UIControlEventTouchUpInside];
word.layer.borderColor = [UIColor blackColor].CGColor;
word.layer.borderWidth = 1.0f;
word.layer.cornerRadius = 5;
[self.view addSubview:word];
x =x+horizontalPadding+CGRectGetWidth(frame);
}
- (IBAction)allBtnSharedTappedevent:(id)sender {
UIButton *btn = (UIButton *) sender;
[btn setBackgroundColor:[UIColor redColor]];
//loop through all your buttons
for(UIView *view in [self.view subviews]){
if([view isKindOfClass:[UIButton class]]){
if(view != btn){
UIButton* btn1 = (UIButton*) view;
[btn1 setBackgroundColor:[UIColor grayColor]];
}
}
}
}
Keep the buttons in an array.
#property (nonatomic, copy, readonly) NSArray<UIButton *> *buttons;
Then in your method that handles the tap, do something like this.
- (IBAction)buttonTapped:(UIButton *)sender {
// Loop through all buttons, clearing the background color
for (UIButton *button in self.buttons) {
button.backgroundColor = [UIColor clearColor];
}
// Set the background color for the selected button
sender.backgroundColor = [UIColor redColor];
}
You should avoid using the tag property of UIView, it will just cause you headaches down the road.
give your button tag number like 1,2,3,4 ... etc
then
- (IBAction)btn1Tapped:(id)sender {
UIButton *btn = (UIButton *) sender;
[btn setBackgroundColor:[UIColor redColor]];
loop through all your buttons
for(int i = 1;i <= total numbers of button; i++){
if(btn.tag != i){
UIButton* btn1 = [myView viewWithTag:i];
[btn1 setBackgroundColor:[UIColor graycolor]];
}
}
}

Set title on button according to array amount

How do I set the title to each button so it sets the button title to: 1, 2, 3, 4 5 etc. button?
My buttons:
#interface PregnancyViewController () {
NSMutableArray *buttons;
}
#end
- (void)viewDidLoad
{
[super viewDidLoad];
for( int i = 0; i < 5; i++ ) {
for( int j = 0; j < 6; j++ ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(j * 50 + 10, i * 50 + 20, 40, 40);
// Add buttons to my mutable array
[buttons addObject: button];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
}
How about this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSUInteger k = 1;
for( NSUInteger i = 0; i < 5; i++ ) {
for( NSUInteger j = 0; j < 6; j++ ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(j * 50 + 10, i * 50 + 20, 40, 40);
NSString* title = [NSString stringWithFormat: #"%#", #(k++)];
[button setTitle: title forState: UIControlStateNormal];
// Add buttons to my mutable array
[buttons addObject: button];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
}

setting the value to superview,when clicking the subviews

I have dynamically created uiviews,which having many buttons.It is for selecting particular value(uibutton) from each row(uiview).While clicking the button,I have to get the button image changed(like highlighting).Now I am getting like that,but order is not matching.Buttons are not getting selected in some row.please check the code below
for (int j=0; j<[viewTagAry count]; j++)
{
if ([[viewTagAry objectAtIndex:j] isEqualToString:[NSString stringWithFormat:#"%d",v]])
{
for (int i=0; i<[lastSelectedButtonAry count]; i++)
{
lastSelectedButton=[lastSelectedButtonAry objectAtIndex:i];
int tag=[[btnTagArray objectAtIndex:i] intValue];
UIImage *numImg=[UIImage imageNamed:[NSString stringWithFormat:#"%db.png",tag]];
[lastSelectedButton setBackgroundImage:numImg forState:UIControlStateNormal];
}
}
}
-(void)highlightButton:(id)sender{
UIButton *button = (UIButton *)[sender userInfo];
int t=button.tag+1;
UIImage *numImg=[UIImage imageNamed:[NSString stringWithFormat:#"%da.png",t]];
[button setBackgroundImage:numImg forState:UIControlStateNormal];
}

Why this brings about SIGABRT error?

I have some sources below.
- (void)Button:(UIButton *)button {
NSString *imageName = ((UIButton *)[self.view viewWithTag:button.tag]).titleLabel.text;
}
- (void)viewDidLoad {
NSMutableArray *_array = [[NSMutableArray alloc] init];
NSInteger iCount = [_array count];
for (i = 0; iCount > i; i++) {
UIButton *btn = [[UIButton alloc] init];
btn.titleLabel.text = [[_array objectAtIndex:i] objectForKey:#"FILE"];
btn.tag = i;
[btn addTarget:self action:#selector(Button:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
}
When I access Button method with 0 index tag, I get SIGABRT error.
What can I do ?
Read your console output properly, by default every view has the tag '0', so it can be crashed because it is taking some other view besides of uibutton, and may be that view don't not have the titleLabel property.Because it is the property of UIButton.