Adding an array of buttons to a particular subview - ios

I have a subview that is hidden until a user clicks a button (The subview is a UIScrollview). I'd like to programmatically add a grid of buttons in that view. I'm not sure how to specify which view the buttons get added to. I'm trying to follow http://www.raywenderlich.com/130/how-to-write-a-custom-image-picker-like-uiimagepicker
the scrollview is called graphicView
This is what I have
for(int i=0;i<64;i++)
{
UIImage *buttonImage=[UIImage imageNamed:#"image.png"];
[arrayOfThumbnails addObject:buttonImage];
}
for(int i = 0; i < arrayOfThumbnails.count; ++i) {
UIImage *thumb = [arrayOfThumbnails objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:graphicView
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[graphicView addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
This is the only part I'm not using from the tutorial....
[view setContentSize:CGSizeMake(320, (row+1) * 80 + 10)]; self.view
= view;

Make sure the UIImage is valid by checking the return value from [UIImage imageNamed:#"image.png"]
Check that your NSMutableArray has been initialized
Your for loop looks unusual, try changing to fast enumeration
for (UIImage *thumb in arrayOfThumbnails) {
// UIImage *thumb = [arrayOfThumbnails objectAtIndex:i]; <-- Not needed
...
}
Is graphicView being added as a subview somewhere? Set graphicView.backgroundColor = [UIColor redColor] so you can see at least it's being added.

Related

How to change a image when when we click a button?

I'm working with radio buttons, for this i'm writing code like this.
float x = 40;
float y = 0;
for (int i=0; i<self.radioBtnTagArr.count; i++) {
self.radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.radioButton.frame = CGRectMake(x, y+35, 35.0, 35.0);
[self.radioButton setImage:[UIImage imageNamed:#"radio_empty.png"] forState:UIControlStateNormal];
[self.radioButton addTarget:self action:#selector(radioBtnMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:self.radioButton];
self.radioButton.tag = [[self.radioBtnTagArr objectAtIndex:i] intValue];
y = y+70;
}
This "self.radioBtnTagArr" comes from server (tags comes from server), based on that count i want to create radio buttons and i'm assigning tag that tags to radio buttons.
- (void) radioBtnMethod:(UIButton *)sender {
for (int i=0; i<self.radioBtnTagArr.count; i++) {
if (sender.tag == [[self.radioBtnTagArr objectAtIndex:i] intValue]) {
[self.radioButton setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
[self.radioButton setSelected:YES];
}
}
}
Now the problem is when i click first radio button the last radio button was selected like this below image...
I want only when we click first button first button state selected, if we click second button second button state selected like this...
do like create another one array for save the UIbutton, for e.g
Step-1
#property (nonatomic, strong) NSMutableArray *buttonArray;
step-2
add the all buttons to buttonArray
// allocate the memory for your array
buttonArray = [NSMutableArray array];
for (int i=0; i<self.radioBtnTagArr.count; i++) {
// create instance value of UIButton
UIButton *radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
radioButton.frame = CGRectMake(x, y+35, 35.0, 35.0);
[radioButton setImage:[UIImage imageNamed:#"radio_empty.png"] forState:UIControlStateNormal];
[radioButton addTarget:self action:#selector(radioBtnMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:self.radioButton];
y = y+70;
// add the object to array
[buttonArray addObject:radioButton];
}
step-3
inside the target
- (void) radioBtnMethod:(UIButton *)sender {
// reset the previous selection
for (UIButton *getradioButton in buttonArray) {
[getradioButton setImage:[UIImage imageNamed:#"radio_empty.png"] forState:UIControlStateNormal];
}
// set the current selection
[sender setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateNormal];
}

how to change button image in array list using button action?

-(void)createButton
{
int row = 0;
y = 340;
int column = 0;
for(int i = 0; i < myArray.count; ++i)
{
x=column*110+10;
for (y=340; y <= (x+ 90); y=y+320)
{
x=x-10;
}
NSString *imgPath = [myArray objectAtIndex:i];
NSString *imagePath = [newPath stringByAppendingPathComponent:imgPath];
myButton = [[UIButton alloc]initWithFrame:CGRectMake(x, row*110+35, 80, 80)];
myButton.tag = i;
UIImage *buttonImage =[UIImage imageWithContentsOfFile:imagePath];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
// myImageView.backgroundColor = [UIColor orangeColor];
// myImageView.image =[UIImage imageWithContentsOfFile:imagePath];
[myScrollView addSubview:myButton];
if (row == 2)
{
row = 0;
column++;
}
else
{
row++;
}
}
[myScrollView setContentSize:CGSizeMake((column+1) * 110 + 10,372) ];
}
// in botton action
-(void)imageButtonAction:(UIButton *)sender
{
NSLog(#"sender tag is %i",sender.tag);
[myButton setImage:[UIImage imageNamed:#"close1.png"] forState:UIControlStateNormal];
}
but it change last button image of myArray
i can get tag number
You will always get last button the way you have because myButton is the last button that you have.
To get exact button with use of tag, use below code
UIButton *myButton = (UIButton *)[self.view viewWithTag:sender.tag];
Now you can set image to this button...
[myButton setImage:[UIImage imageNamed:#"close1.png"] forState:UIControlStateNormal];

i want to store the button in array

I am creating a nine button using for loop, i want to store the button in an array because i want to access all button at a time.
for(i = 0; i < 9; i++ )
{
num1 = [UIButton buttonWithType:UIButtonTypeCustom];
[num1 setExclusiveTouch:YES];
[num1 setTag:tag_start+i];
num1.tag = currentTag;
currentTag++;
[num1 setTitle:[NSString stringWithFormat:#"%d",currentTag] forState:UIControlStateNormal];
[num1 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[num1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[num1.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:35.0]];
//Store the button in array
[numBtnArray addObject:num1];
NSLog(#"%d", numBtnArray.count); //it show 0 nothing in that array
if(i >= 5) {
num1.frame = CGRectMake(430, i*60-160, 50, 50);
}
else {
num1.frame = CGRectMake(350, i*60+140, 50, 50);
}
[num1 addTarget:self action:#selector(buttonFunction:) forControlEvents:UIControlEventTouchUpInside];
[num1 setBackgroundImage:[UIImage imageNamed:#"greyBtn.png"] forState:UIControlStateNormal];
[num1 setBackgroundImage:[UIImage imageNamed:#"greyBtn.png"] forState:UIControlStateHighlighted];
[self.view addSubview:num1];
}
I don't understand what is the requirement but if you need to store them in an array simply do
NSMutableArray *btnArray = [NSMutableArray new];
for(i = 0; i < 9; i++ )
{
// ...
[btnArray addOjnect:num1];
[self.view addSubview:num1];
}
It's not clear exactly what you want, but I would encourage you not to store the buttons in an array at all, as the UIView already does that.
If you want to perform some sort of mass change to the buttons then you can, either:
Set a tag value in the button, so you can identify them later (with a minimum or maximum range).
Only target the buttons by testing their class.
So, if you want to change the background colour of the buttons in self.view, you simply:
for (id obj in self.view.subviews) {
if (![obj isKindOfClass:[UIButton class]])
continue;
UIButton *button = (UIButton *)obj;
button.backgroundColor = [UIColor blueColor];
}
Change the code as follows
numBtnArray = [[NSMutableArray alloc] init];
for(i = 0; i < 9; i++ ) {
UIButton *num1 = [UIButton buttonWithType:UIButtonTypeCustom];
//...
//Store the button in array
[numBtnArray addObject:num1];
NSLog(#"%d", numBtnArray.count);
//...
}

How to get index of uiimage subview of uiscrollview

I need to detect selected image of uiimage which is placed inside scrollview.
My code is here:
int newscrollviewY = 40;
for(int i = 0; i<1; i++)
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(5, newscrollviewY, 310, 100)];
scrollView.showsVerticalScrollIndicator = YES;
scrollView.scrollEnabled = YES;
scrollView.userInteractionEnabled = YES;
scrollView.backgroundColor = [UIColor clearColor];
scrollView.delegate=self;
int imgx = 1;
for(int img=0; img<[images count]; img++)
{
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(imgx,0,100,100)];
imageView1.backgroundColor = [UIColor whiteColor];
imageView1.image = [images objectAtIndex:img];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
[imageView1 setNeedsDisplay];
[imageView1 setUserInteractionEnabled:YES];
[imageView1 setMultipleTouchEnabled:YES];
[scrollView addSubview:imageView1];
imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(imgx,0,100,100);
[imageButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
imageButton.tag = img;
[scrollView addSubview:imageButton];
imgx = imgx + 103;
}
Please help to detect selected image using imageButton or something else.
Replace your selector with buttonClicked:
be Sure that you are adding ":" after selector method. follow or replace the code below:
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
then Add the selector definantion in .m file
- (void) buttonClicked: (id) sender
{
// here you can get the tag of button.
NSInteger tag = ((UIButton *)sender).tag;
// do your implementation after this
}
Hope this will solve your issue. Enjoy Coding..!!
They way you have set it up the tag of the 'button' is already the number of the image in your array of images. So all you need to do is get the tag from the 'button'.
Make sure the button clicked function retrieves the tag from the sender(id) that you receive.
For example a bit like this:
- (IBAction)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
UIImage selectedImages = [images objectForKey: [button tag]];
}
As par you question if you are using UIImageview you have to use UITapGestureRecognizer that same like Button. you just need to add UITapGestureRecognizer like:-
for(int img=0; img<[images count]; img++)
{
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(imgx,0,100,100)];
imageView1.backgroundColor = [UIColor whiteColor];
UITapGestureRecognizer *frameTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(frameTapGesture:)];
frameTapGesture.numberOfTapsRequired=1;
imageView1.image = [images objectAtIndex:img];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
[imageView1 setNeedsDisplay];
[imageView1 setUserInteractionEnabled:YES];
[imageView1 setMultipleTouchEnabled:YES];
imageView1.tag=img;
[_imgView addGestureRecognizer:frameTapGesture];
[scrollView addSubview:imageView1];
}.
- (void)frameTapGesture:(UITapGestureRecognizer*)sender
{
UIView *view = sender.view; //cast pointer to the derived class if needed
NSLog(#"%d", view.tag);
}
Reduce your code There are No need to add Button with same Frame of ImageView For getting Click Event or you can also set Image of UIButton for you want to like use UIBUtton then remove UIImageview Code else you simply use UITapGestureRecognizer with getting click event using - (void)frameTapGesture:(UITapGestureRecognizer*)sender
Please try this code
for(int img=0; img<[images count]; img++)
{
imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setImage:[UIImage imageNamed:[images objectAtIndex:i]] forState:UIControlStateNormal];
imageButton.frame = CGRectMake(imgx,0,100,100);
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
imageButton.tag = img;
[scrollView addSubview:imageButton];
imgx = imgx + 103;
}
- (void) buttonClicked: (id) sender
{
UIButton *button = (UIButton *)sender;
UIImage selectedImage = button.currentImage;
}
set button target
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
// get Button Action
-(void) buttonAction:(UIButton *)sender
{
NSLog(#"%dl",sender.tag);
// do your implementation after this
}

How could I make this work with UIScrollView?

I'm inserting dynamically anywhere from 1 to 300 buttons inside a UIScrollView(as subviews). Currently, the buttons line up nicely but is not scrolling as I'd like them to. It just scrolls horizontally without a paging effect. Ideally, they way I'd like it to work is:
Display a max of 48 buttons per "page"
When scrolling to the right, show next "page" with 48 additional buttons
My code:
-(void)populateScrollView:(NSMutableArray *)colors {
// Properties
NSInteger count = 0;
NSInteger rowsize = 48;
CGFloat pageNum = 6;
CGFloat pageWidth = scrollView.frame.size.width;
CGFloat pageHeight = scrollView.frame.size.height;
CGFloat visibleWidth = 465;
myButtons = [[NSMutableArray alloc]init];
// Clear the subviews if any
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}
for (Color *element in colors) {
// Set the button properties
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:#"color_overlay.png"];
[button setImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateHighlighted];
div_t temp = div(count, rowsize);
button.frame = CGRectMake(52 * temp.rem , 52*temp.quot, 52, 52);
button.tag = count++;
UIImage *effect = [UIImage imageNamed:element.image_resource];
effectImage = [[UIImageView alloc] initWithImage:effect];
effectImage.frame = CGRectMake(52 * temp.rem , 52*temp.quot, 52, 52);
// Color the graphic if color available. If not, load the image
unsigned result = 0;
if ([element.hex_value length] > 1){
NSScanner *scanner = [NSScanner scannerWithString:element.hex_value];
[scanner scanHexInt:&result];
button.backgroundColor = UIColorFromRGB(result);
}
else {
effectImage.image = effect;
}
// Set actions for the color
[button addTarget:self action:#selector(changeGraphicColor:) forControlEvents:UIControlEventTouchUpInside];
[myButtons addObject:button];
for (UIButton *myButton in myButtons){
[scrollView insertSubview:effectImage atIndex:1];
[scrollView insertSubview:myButton atIndex:2];
}
}
// Set scrollView contentSize and create pages:
CGFloat leftShift=floor(((visibleWidth-pageWidth)/2)/pageWidth)*pageWidth;
CGFloat contentSizeReduction=2*leftShift;
scrollView.contentSize = CGSizeMake(pageNum*pageWidth-contentSizeReduction,pageHeight);
[myButtons removeAllObjects];
[myButtons release];
}
I'm thinking the way I'm adding the buttons won't allow me to implement what I am trying to accomplish. What do you guys think I'm doing wrong here?
Did you set scrollView.pagingEnabled to YES?

Resources