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];
}
Related
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);
}
}
[self.menuBtn1 setImage:[UIImage imageNamed:#"menu1.png"] forState:UIControlStateNormal];
[self.menuBtn2 setImage:[UIImage imageNamed:#"menu2.png"] forState:UIControlStateNormal];
[self.menuBtn3 setImage:[UIImage imageNamed:#"menu3.png"] forState:UIControlStateNormal];
[self.menuBtn4 setImage:[UIImage imageNamed:#"menu4.png"] forState:UIControlStateNormal];
[self.menuBtn5 setImage:[UIImage imageNamed:#"menu5.png"] forState:UIControlStateNormal];
[self.menuBtn6 setImage:[UIImage imageNamed:#"menu6.png"] forState:UIControlStateNormal];
[self.menuBtn7 setImage:[UIImage imageNamed:#"menu7.png"] forState:UIControlStateNormal];
This Code is very inefficient. But I think it can be efficient with forsyntax. However I don't know how can I code self.menuBtn%d with for. In Objective-C, How can I code this?
This access the button properties dynamically so you don't need an array. This only works if the buttons have already been initialized (they are if they're IBOutlets)
NSString *imageName;
NSString *buttonName;
UIImage *image;
for (int i = 1; i < 8; i++) {
//#autoreleasepool {
imageName = [NSString stringWithFormat:#"menu%d", i];
buttonName = [NSString stringWithFormat:#"menuBtn%d", i];
image = [UIImage imageNamed:imageName];
UIButton *button = (UIButton *)[self valueForKey:buttonName];
if (button)
[button setImage:image forState:UIControlStateNormal];
/*
If you needed to scale this to say 1000 images, you can uncomment the first
and last line in the for-loop which will automatically release the variables
each iteration so the memory consumption doesn't spike.
*/
//}
}
One solution might be to set the tag of all your buttons to some value (e.g. 100 + button number), then to try:
for (UIButton *aButton in self.view.subviews)
{
if ((aButton.tag >= 100 and aButton.tag < 107))
[aButton setImage:[UIImage imageNamed:[NSString stringWithFormat:#"menu%d.png",aButton.tag]] forState:UIControlStateNormal];
}
First add the buttons to the array and the use for loop like this:
NSArray *array=[NSArray arrayWithObjects:menuBtn1,menuBtn2,menuBtn3,menuBtn4,menuBtn5,menuBtn6menuBtn7,nil];
int i=1;
for(UIButton *aButton in array){
[aButton setImage:[UIImage imageNamed:[NSString stringWithFormat:#"menu%d.png",i++]] forState:UIControlStateNormal];
}
NSString *menubutton;
NSMutableArray *imagearray = [[NSMutableArray alloc]initWithObjects:#"image1.png",#"image2.png",#"image3.png", nil];
for(int i=0; i<[imagearray count]; i++)
{
menubutton = [NSString stringWithFormat:#"menuBtn%d",i];
UIButton *button = (UIButton *)[self valueForKey:menubutton];
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",[imagearray objectAtIndex:i]]] forState:UIControlStateNormal];
}
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"]];}
}
I`ve got a very unexpected problem which making me crazy. This is very simple type of memory game with 36 cards. In code below I making array with 36 objets (18 double objects). Everything works great when you choose card between 1-12. When you choose higher card, matching process can't recognize match. Why???
memory.m
for (int i = 0; i < [imagesArray count]; ++i)
{
int n = i;
[imagesArray exchangeObjectAtIndex:i withObjectAtIndex:n];
NSLog(#"index obrázku %i",i);
NSLog(#"index array %i",n);
}
// Set button images
for (int i=0; i<[buttonsArray count]; i++) {
NSLog(#"index button %i",i);
UIButton *singleButton = [buttonsArray objectAtIndex:i];
[singleButton setBackgroundImage:[UIImage imageNamed:TILE_BACKGROUND] forState:UIControlStateNormal];
[singleButton setBackgroundImage:[UIImage imageNamed:TILE_BACKGROUND] forState:UIControlStateHighlighted];
if ([[imagesArray objectAtIndex:i] integerValue] == 1) {
[singleButton setBackgroundImage:[UIImage imageNamed:TILE_IMAGE1] forState:UIControlStateDisabled];
NSLog(#"Karta 1 tag %i",i);
} else if ([[imagesArray objectAtIndex:i] integerValue] == 2) {
[singleButton setBackgroundImage:[UIImage imageNamed:TILE_IMAGE2] forState:UIControlStateDisabled];
NSLog(#"Karta 2 tag %i",i);
}
objects are from 1 to 18...
- (IBAction)tileClicked:(id)sender {
// Button is clicked
if (!matchingInPogress) {
UIButton *clickedButton = sender;
tilesOpened++;
NSLog(#"Počet otočených karet je %i",tilesOpened);
[clickedButton setEnabled:NO];
if (tilesOpened == 1) {
// Only one tiled opened, remeber tag for match
openTileIndex = clickedButton.tag;
} else {
// Second tile is opened, perform match
matchingInPogress = YES;
[self performSelector:#selector(matchTiles:) withObject:[NSNumber numberWithInt:clickedButton.tag] afterDelay:0.5];
tilesOpened = 0;
}
}
button control..
- (void)matchTiles:(NSNumber *)clickedTile {
// Match opened tiles
int clickedTileIndex = [clickedTile integerValue];
NSLog(#"Karta 1 tag %i",clickedTileIndex);
NSLog(#"Karta 2 tag %i",openTileIndex);
if ([imagesArray objectAtIndex:clickedTileIndex] != [imagesArray objectAtIndex:openTileIndex]) {
NSLog(#"Porovnává hodnoty v clickedTileIndex a openTileIndex");
// If tiles don't match set them to enabled state
UIButton *firstButton = [buttonsArray objectAtIndex:openTileIndex];
UIButton *secondButton = [buttonsArray objectAtIndex:clickedTileIndex];
NSLog(#"Nesouhlas tag je %i",openTileIndex);
NSLog(#"Nesouhlas tag je %i",clickedTileIndex);
[firstButton setEnabled:YES];
[secondButton setEnabled:YES];
} else {
NSLog(#"Match!");
UIButton *firstButton = [buttonsArray objectAtIndex:openTileIndex];
UIButton *secondButton = [buttonsArray objectAtIndex:clickedTileIndex];
NSLog(#"Souhlas tag je %i",openTileIndex);
NSLog(#"Souhlas tag je %i",clickedTileIndex);
[firstButton setHidden:YES];
[secondButton setHidden:YES];
}
matchingInPogress = NO;
}
and finaly matching process
Thanks!
Since arrays can only contain objects, you don't test them with != as that will only compare the value of the pointers and not the objects themselves. Very often, this works (even though you wouldn't expect it to) for some (usually low) values but quits working on others because UIKit objects often cache objects sand return the same object address, even for different instances.
You need to change this line in tileClicked:
if ([imagesArray objectAtIndex:clickedTileIndex] != [imagesArray objectAtIndex:openTileIndex]) {
to this:
if (![(UIImage *)[imagesArray objectAtIndex:clickedTileIndex] isEqual:
(UIImage *)[imagesArray objectAtIndex:openTileIndex]]) {
I m trying to create an image gallery where the images are stored in resource bundle. I'm storing my images in NSMutable array what i want is an image gallery ....but the output is pretty different from wat i expected.the below code works perfectly fine.to be more specific cud u guys help me out below is the code...
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:#"logo1.png"]];
[_images addObject:[UIImage imageNamed:#"logo2.png"]];
[_images addObject:[UIImage imageNamed:#"logo3.png"]];
[_images addObject:[UIImage imageNamed:#"logo4.png"]];
[_images addObject:[UIImage imageNamed:#"logo5.png"]];
[_images addObject:[UIImage imageNamed:#"logo6.png"]];
[_images addObject:[UIImage imageNamed:#"logo7.png"]];
[_images addObject:[UIImage imageNamed:#"logo8.png"]];
[_images addObject:[UIImage imageNamed:#"logo9.png"]];
[_images addObject:[UIImage imageNamed:#"logo10.png"]];
[_images addObject:[UIImage imageNamed:#"logo11.png"]];
[_images addObject:[UIImage imageNamed:#"logo12.png"]];
[_images addObject:[UIImage imageNamed:#"logo13.png"]];
[_images addObject:[UIImage imageNamed:#"logo14.png"]];
[_images addObject:[UIImage imageNamed:#"logo15.png"]];
[_images addObject:[UIImage imageNamed:#"logo16.png"]];
NSLog(#"ha ha ha:%d",_images.count);
UIScrollView *view = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
int row=0;
int coloumn=0;
for (int i=0; i< _images.count; i++)
{
UIImage *thumb=[_images objectAtIndex:i];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(coloumn*100+24, row*80+30, 64, 64);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag=i;
[view addSubview:button];
if (coloumn==2) {
coloumn==0;
row++;
}else {
coloumn++;
}
}
[view setContentSize:CGSizeMake(320, (row+1)*80+10)];
self.view=view;
[view release];
The problem with your code is here:
if (coloumn==2) {
coloumn==0; //-- ERROR!
row++;
this should be:
coloumn=0; //-- correct!
using == instead of =
Use the three20 library
iPhone SDK: Creating a Photo Gallery With Three20
Edited:
Your code are excellent except on wrong statement :
coloumn==0;
that's the reason your colomn always point to the 2nd coloumn.
So change it to coloumn = 0;
This works very well but you would have to make a few adjustments to the code when you change the size. Copy and paste it in your Viewdidload; make sure you have a UIScrollview with IB already setup with a link name scrollView.
int row = 0;
int colomn = 0;
int width = 79;
int height = 79;
int offset = 1;
// int diff = width * 0.5;
CGRect rect;
for (int i = 0; i < 45; i++) {
if(i == 0){
colomn = 0;
}else{
colomn += width + offset;
};
if(colomn >= ((int)scrollView.frame.size.width) - width){
colomn = 0;
row += height + offset;
}
rect = CGRectMake(colomn, row, width, height);
UIView *view = [[UIView alloc] initWithFrame:rect];
[view setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:view];
}
[scrollView setContentSize:CGSizeMake(320, row+height)];