I want to create a row with 4 buttons, the loop is doing what it should do, and it only enters the if statement 4 times as it should, but when the view pop's out, I can see only one button on it.
Why is that? am I doing something wrong?
btnFrame = 18;
for (int i = 0; i < [arrImages count]; i++)
{
if (btnFrame <= 237)
{
NSLog(#"%i",btnFrame);
UIButton * tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
tempBtn.frame = CGRectMake(btnFrame, 20, 65, 65);
[tempBtn setTitle:[NSString stringWithFormat:#"Button%i",i] forState:UIControlStateNormal];
[self.view addSubview:tempBtn];
btnFrame = btnFrame + 73;
}
}
Thanks alot!
i think when your viewWillDisappear at that time this view will cleared so do bellow thing....
if you crete one method in which you just paste your above code and then when yourViewController appear at that time call your method try this...
-(void)viewWillAppear:(BOOL)animated
{
[self SetButton];
}
-(void)setButton{
btnFrame = 18;
for (int i = 0; i < [arrImages count]; i++)
{
if (btnFrame <= 237)
{
NSLog(#"%i",btnFrame);
UIButton * tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
tempBtn.frame = CGRectMake(btnFrame, 20, 65, 65);
[tempBtn setTitle:[NSString stringWithFormat:#"Button%i",i] forState:UIControlStateNormal];
[self.view addSubview:tempBtn];
btnFrame = btnFrame + 73;
}
}
}
hope this help you....
:)
Related
I am trying to parse the text of particular label from list of label showed in UIScrollView into next view show in label. But these label text got from NSMutableArray. i tried but i got only last value of the NSMutableArray See following code for Ref: Plz suggest what i will do
-(void)dataPrinting
{
int t= 60;
NSLog(#"%#",totalData);
for (int i = 0; i < [totalData count]; i++)
{
pplname=[[UILabel alloc]initWithFrame:CGRectMake(10, t, 200, 30)];
pplname.backgroundColor=[UIColor clearColor];
pplname.text=[totalData objectAtIndex:i];
pplname.textColor=[UIColor redColor];
[scrollView addSubview:pplname];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
//initWithFrame:CGRectMake(10, t, 200, 40) ];
button.frame=CGRectMake(10, t, 200, 40);
[button addTarget:self
action:#selector(nextview)
forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
t=t+40;
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, t+2)];
[aview stopAnimating];
}
-(IBAction)nextview
{
Details *detailsPage=[[Details alloc]initWithNibName:#"Details" bundle:nil];
for (int i = 0; i < [totalData count]; i++)
{
detailsPage.pplName=[totalData objectAtIndex:i];
}
[self presentModalViewController:detailsPage animated:YES];
}
Give the tag to fetch particular name of label just replace my bellow code with your code...
-(void)dataPrinting
{
int t= 60;
NSLog(#"%#",totalData);
for (int i = 0; i < [totalData count]; i++)
{
pplname=[[UILabel alloc]initWithFrame:CGRectMake(10, t, 200, 30)];
pplname.backgroundColor=[UIColor clearColor];
pplname.text=[totalData objectAtIndex:i];
pplname.tag = i;
pplname.textColor=[UIColor redColor];
[scrollView addSubview:pplname];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
//initWithFrame:CGRectMake(10, t, 200, 40) ];
button.frame=CGRectMake(10, t, 200, 40);
button.tag = i;
[button addTarget:self
action:#selector(nextview)
forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
t=t+40;
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, t+2)];
[aview stopAnimating];
}
-(IBAction)nextview:(id)sender
{
UIButton *btnTemp = (UIButton*)sender;
Details *detailsPage=[[Details alloc]initWithNibName:#"Details" bundle:nil];
detailsPage.pplName=[totalData objectAtIndex:btnTemp.tag];
[self presentModalViewController:detailsPage animated:YES];
}
I want them to be shown on the screen 4 at a time so I'm thinking about creating a UIScrollview for that and adding a subview with the buttons on it. How can I add let's say about 20 buttons since I can't have them all on the screen?
How about adding it your button in array.
Something like this: (just a snippet)
int row = 0;
int column = 0;
for(int i = 0; i < _thumbs.count; ++i) {
UIImage *thumb = [_thumbs objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*60+10, row*60+10, 60, 75);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[scrollView addSubview:button];
// If you want to show only 4 buttons, just tweak it here. You can do this method in anyway you like
if (column == 4) {
column = 0;
row++;
} else {
column++;
}
}
[scrollView setContentSize:CGSizeMake(300, (row+1) * 60 + 10)];
Hope this helps.
-(void)setButtons_AsPerTheMatrixSelection
{
int x = 7;
int y = 60;
for (int j = 0; j <= 35; ++j)
{
if (j <= 5)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
else if (j > 5 && j <= 11)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-306, y+51, width, height)];
else if (j > 11 && j <= 17)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-612, y+102, width, height)];
else if (j > 17 && j <= 23)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-918, y+153, width, height)];
else if (j > 23 && j <= 29)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-1224, y+204, width, height)];
else if (j > 29 && j <= 35)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-1530, y+255, width, height)];
btnMatrix.tag = j;
[btnMatrix setBackgroundImage:imgDefaultBG forState:UIControlStateNormal];
btnMatrix.userInteractionEnabled = TRUE;
[btnMatrix addTarget:self action:#selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
[viewWithButtons addSubview:btnMatrix];
[self.view addSubview:viewWithButtons];
x = x + 51;
y = y;
[arrButton addObject:btnMatrix];
}
}
This is my code to add dynamic buttons as per the matrix of 6*6
here i am adding the button tag also,
Now i am going to click on button & would like to change background image with this code...
-(void)changeImage:(id)sender
{
UIImage *img = [UIImage imageNamed:#"No.png"];
UIButton *tmpButton = (UIButton *)[self.view viewWithTag:btnMatrix.tag];
NSLog(#"btn.tag is... %d",tmpButton.tag);
[btnMatrix setBackgroundImage:img forState:UIControlStateNormal];
}
When i click to any button it always changing image of last tagged button...
i have to change Background of the selected button itself.
how can it be possible.
Please guide me i am not getting a part where i am doing a mistake.
Thanks.
-(void)changeImage:(UIButton*)sender
{
UIImage *img = [UIImage imageNamed:#"No.png"];
[sender setBackgroundImage:img forState:UIControlStateNormal];
}
BTW, your code is too messy, you may do like this
int pw = 306;
int ph = 51;
int ch = 6;
int cv = 6;
for ( int i = 0 ; i < cv ; ++i )
{
for ( int j = 0 ; j < ch ; ++j )
{
UIButton *btnMatrix = [[[UIButton alloc] initWithFrame:CGRectMake(7+pw*j, 51+ph*i, width, height)] autorelease];
btnMatrix.tag = i*ch+j;
}
}
Try this change in this method:
-(void)changeImage:(id)sender
{
UIButton *Btnmatrix = (UIButton *)sender
UIImage *img = [UIImage imageNamed:#"No.png"];
[Btnmatrix setBackgroundImage:img forState:UIControlStateNormal];
}
in your function
-(void)setButtons_AsPerTheMatrixSelection
make your button local as follow
for (int j = 0; j <= 35; ++j)
{
if (j <= 5){
UIButton *btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
btnMatrix.tag = j;
[btnMatrix setBackgroundImage:imgDefaultBG forState:UIControlStateNormal];
btnMatrix.userInteractionEnabled = TRUE;
[btnMatrix addTarget:self action:#selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
[viewWithButtons addSubview:btnMatrix];
}
// and so on every button
}
Hope this will help you. sorry for my bad english :)
It seems to me like btnMatrix is an ivar, and since you assign it successively to each button as you add them, it will hold a reference to your last button after you are done creating buttons. Change your line to inspect the sender object instead:
UIButton *tmpButton = (UIButton*)sender.tag;
I want to create uibuttons dynamically. so i for loop to create button with tag and added to view.
all buttons performs the same action with respect to its tag value... but i want to change the propery of the button. Therefore i need to get the uibutton using the tag value...
my code...
UIButton *button2;
//view did load
int width = 30;
for(int i = 1; i <= 5; i++)
{
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:#selector(ratingAction:) forControlEvents:UIControlEventTouchUpInside];
[button2 setBackgroundImage:[UIImage imageNamed:#"star1.png"] forState:UIControlStateNormal];
button2.tag = i;
button2.backgroundColor = [UIColor clearColor];
button2.frame = CGRectMake(width, 78, 15, 15);
[self.view addSubview:button2];
width = width +30;
}
-(void)ratingAction:(id*)sender
{
// here using the tag value i want to get the button and change the background image….
// for example i want to change the background for tag values 1,3,6,7 ,8…
}
Use viewWithTag function of UIView to access your UIButton using the tag value.
See in Documentation viewWithTag
Use it as below.
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:1];
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:3];
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:6];
.........
-(void)ratingAction:(id*)sender
{
// here using the tag value i want to get the button and change the background image….
// for example i want to change the background for tag values 1,3,6,7 ,8…
if ([sender isKindOfClass:[UIButton class]])
{
UIButton *temp=(UIButton*)sender;
if ([temp tag]==1 || [temp tag]==3 || [temp tag]==6 || [temp tag]==7 )
{
[temp setBackgroundColor:[UIColor redColor]];
}
}
}
Check this code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int y = 100;
for (int i=0; i<=5; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80, y, 160, 40);
[button setTitle:[NSString stringWithFormat:#"Button-%i", i] forState:UIControlStateNormal];
// [button set
[self.view addSubview:button];
y=y+60;
}
}
-(void)aMethod:(id)sender{
UIButton *button = sender;
int y = 100;
int i = 5;
for (int x=0; x<=i; x++) {
NSString *frameString = [NSString stringWithFormat:#"{{80, %i}, {160, 40}}", y];
if ([NSStringFromCGRect(button.frame) isEqualToString:frameString]) {
NSLog(#"button %i clicked..", x);
}
y= y+60;
}
}
I am enclosing my sample code herewith for ready reference, please make it in center place, this is what I am facing problem in. It is not stopping on the center indicator.
Note : All button are dynamic and coming from web, but in this sample I made it from a hard coded array. But in original scenerio their count(number of buttons) may change.
- (void)viewDidLoad {
arr = [[NSMutableArray alloc] initWithObjects:#"Test1",#"Test2",#"Test3",#"Test4",#"Test5",nil];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg_content_slide.png"]];
img.frame = CGRectMake(0, 20, 320, 85);
img.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:img];
[self setScrollButtons];
[super viewDidLoad];
}
- (void) setScrollButtons
{
UIScrollView *tagScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 44)];
tagScrollView.backgroundColor = [UIColor clearColor];
[tagScrollView setShowsHorizontalScrollIndicator:NO];
[tagScrollView setShowsVerticalScrollIndicator:NO];
[tagScrollView setCanCancelContentTouches:NO];
tagScrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
tagScrollView.clipsToBounds = YES;
tagScrollView.scrollEnabled = YES;
tagScrollView.pagingEnabled = YES;
tagScrollView.bounces = YES;
tagScrollView.tag = 2;
int xaxis = 0;
for (int i = 0; i < [arr count]; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(xaxis, 0, 160, 44);
//btn.titleLabel.text = [NSString stringWithFormat:#"%#",[arr objectAtIndex:i]];
[btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
NSLog(#"%#", btn.titleLabel.text);
xaxis += btn.frame.size.width + 20;
[tagScrollView addSubview:btn];
}
[tagScrollView setContentSize:CGSizeMake([arr count] * 200, tagScrollView.frame.size.height)];
[self.view addSubview:tagScrollView];
}
Find code here UnAligned scrollview code
chage ur code with this one
int xaxis = 0;
for (int i = 0; i < [arr count]; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(320*xaxis+80, 0, 160, 44);
//btn.titleLabel.text = [NSString stringWithFormat:#"%#",[arr objectAtIndex:i]];
[btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
//[btn setBackgroundImage:[UIImage imageNamed:#"remove_fav.png"] forState:UIControlStateNormal];
NSLog(#"%#", btn.titleLabel.text);
xaxis ++;//= btn.frame.size.width + 40;
[tagScrollView addSubview:btn];
}
[tagScrollView setContentSize:CGSizeMake([arr count] * 320, tagScrollView.frame.size.height)];
[self.view addSubview:tagScrollView];
it will solve ur problem
What you want to achieve is not possible when pagination is enabled as when pagination is enabled the scroll view scroll to its width.So for this what you can do max is add this method
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGPoint offset = scrollView.contentOffset;
int temp = floor(offset.x/160);
[tagScrollView scrollRectToVisible:CGRectMake(temp*160, 0, 320, 44) animated:YES];
}
its working fine for this configuration .......
{
int xaxis = 0;
for (int i = 0; i < [arr count]; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(160*xaxis+85, 0, 150, 44);
[btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
NSLog(#"%#", btn.titleLabel.text);
xaxis ++;//= btn.frame.size.width + 40;
[tagScrollView addSubview:btn];
}
[tagScrollView setContentSize:CGSizeMake(160*xaxis+250, tagScrollView.frame.size.height)];
}
and .....
tagScrollView.pagingEnabled = NO;