Add buttons to UIScrollview - iphone

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.

Related

Pare the label text to next View when i clicked that Particular label from list label showed in view controller

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];
}

buttons wont add to view

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....
:)

How to get the information about the sender in the case here

I am creating UIButtons inside a loop as shown below.
for(int i = 1; i <= count; i++){
if(i ==1){
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[self addSubview:btn1];
continue;
}
x = x + 40;
y = y + 50;
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(x , y, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 addTarget:self action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
And I handle the UIButton clicked events as
-(void) buttonPressed{
}
I need the information about which button I have clicked in the event handling method. I need to ge the frame of the clicked button. How can I alter this code to get the information about the sender.
Add a tag to your button while it is created like btn.tag=i; and re define your method like this
-(void) buttonPressed:(id)sender{
// compare sender.tag to find the sender here
}
What you should do is add these lines in your code:
for(int i = 1; i <= count; i++)
{
if(i ==1){
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[btn1 setTag:i]; // This will set tag as the value of your integer i;
[self addSubview:btn1];
continue;
}
x = x + 40;
y = y + 50;
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(x , y, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 setTag:i]; // also set tag here.
[btn2 addTarget:self action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
And now change your IBAction like this:
-(void) buttonPressed: (id)sender
{
if(sender.tag==1) // determine which button was tapped using this tag property.
{
// Do your action..
}
}
Or you can do it this way..This way you copy the object thats in sender to this UIBtton object.
-(void) buttonPressed: (id)sender
{
UIButton *tempBtn=(UIButton *) sender;
if(tempBtn.tag==1) // determine which button was tapped using this tag property.
{
// Do your action like..
tempBtn.backgroundColor=[UIColor blueColor];
tempBtn.alpha=0.5;
}
}
So in this parameter sender its going to hold your UIButton on which you interact and you can access its properties like tag, text etc. Hope it helps..

how to creating dynamic buttons iphone xcode?

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;
}
}

how to lisent to each button for iphone

i have created 4 dynamic buttons but how to write method on each of them
for (i = 1; i <= [a1 count]-1; i++)
{
NSString *urlE=[a1 objectAtIndex:1];
NSLog(#"url is %#",urlE);
#pragma mark buttons
CGRect frame = CGRectMake(curXLoc, 10, 60, 30);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setImage:[UIImage imageNamed:#"tab2.png"] forState:UIControlStateNormal];
[button setTitle:(NSString *)#"new button" forState:(UIControlState)UIControlStateNormal];
[button addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
curXLoc += (kScrollObjWidth1);
[self.view addSubview:button];
}
-(void)buttonEvent:(id)sender {
NSLog(#"new button clicked!!!");
if (sender == ??) how to tell button 1 ,2,3,4
{
}
}
You should give a .tag to each button on creation
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = i; // <-- here
...
With this, you could identify the button by .tag.
-(void)buttonEvent:(UIButton*)sender {
NSLog(#"new button clicked!!!");
if (sender.tag == 2) {
NSLog(#"2nd button clicked.");
...
You can specify a separate selector for each button using NSSelectorFromString to dynamically generate selector names.
For example
NSString *selectorName = [NSString stringWithFormat:#"button%dEvent:", i];
[button addTarget:self action:NSSelectorFromString(selectorName) forControlEvents:UIControlEventTouchUpInside];
-(void)button1Event:(UIButton*)sender {}
-(void)button2Event:(UIButton*)sender {}
-(void)button3Event:(UIButton*)sender {}
-(void)button4Event:(UIButton*)sender {}