/ / как да добавяте неограничени uibuttons програмно [дубликат] - iphone, object-c, uibutton

как да добавите mutiple uibuttons програмивно [duplicate] - iphone, objective-c, uibutton

Здравей Искам да добавя множество UIButtons с определена програма. всъщност имам NSMutableArray , и исках да създам толкова много UIButtons колкото много елементи в NSMutableArray, и искам също така, че във всеки ред трябва да има 3 UIB бутона UIView, създаване на UIButton не е проблем за мъжете проблемът за мен да напиша алгоритъм за създаване buttons динамично. това е кодът любезно коригирайте това благодаря предварително

-(IBAction)seeAll:(id)sender
{
if ([barBtn.title isEqualToString:@"See All"])
{


containerVw = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[containerVw setBackgroundColor:[UIColor whiteColor]];
[srcVw removeFromSuperview];
[self.view addSubview:containerVw];

NSUInteger i;
int xCoord;
int yCoord=20;
int buttonWidth=80;
int buttonHeight=50;
int buffer = 10;
for (i = 1; i <= imageArray.count; i++)
{

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight);
[aButton setBackgroundImage:[imageArray objectAtIndex:i-1] forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[containerVw addSubview:aButton];



if (i%4 == 0 )
{
yCoord += buttonHeight + buffer;

}
NSLog(@"xcoordinate %i",xCoord);
NSLog(@"ycoordinate %i",yCoord);

}

Отговори:

1 за отговор № 1
- (void)createButtonGrid
{
int x, y, width, height, exceedSpace;
exceedSpace = 6;
x = y = exceedSpace;
width = height = 100;

UIButton *btn;

for (int i=0; i< 10; i++)
{
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(x, y, width/2, height/2)];
[btn setTag:i];
[btn addTarget:self action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];

[self.scrollview addSubview:btn];

if((i+1) % 3 == 0)
{
x = exceedSpace;
y += (width/2)+exceedSpace;
}
else
x += (width/2)+exceedSpace;
}
}

1 за отговор № 2

вижте отговора ми по-долу и получете някаква идея от това ..

        int imageIndex = 0;

int yOffset = 4;

while (imageIndex < imageArray.count)
{
int yPos =  7 + yOffset * 30;

for(int i = 0; i < 4; ++i)//set 4 for 4 columns
{
CGRect  rect = CGRectMake((0 + i * 80), yPos, 80, 31);

if (imageIndex < [imageArray  count]) {

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = rect;
button.tag = imageIndex;
[button addTarget:self
action:@selector(btnTemp_Clicked:)
forControlEvents:UIControlEventTouchDown];

[button setTitle:[imageArray objectAtIndex:imageIndex] forState:UIControlStateNormal];
//                 [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
[button setBackgroundImage:[imageArray objectAtIndex:imageIndex] forState:UIControlStateNormal];

[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];

[button setNeedsDisplay];

[containerVw addSubview:button];
}
++imageIndex;
}
++yOffset;
}

0 за отговор № 3

Къде задавате xCoord на вашия бутон

for (i = 1; i <= imageArray.count; i++)
{
if (i%4 == 0 )
{
yCoord += buttonHeight + buffer;
xCoord=0;
}
else{
xCoord += buttonWidht + buffer;
}
NSLog(@"xcoordinate %i",xCoord);
NSLog(@"ycoordinate %i",yCoord);

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight);
[aButton setBackgroundImage:[imageArray objectAtIndex:i-1] forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[containerVw addSubview:aButton];
}