如何创建动态按钮 iphone xcode?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6359364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:16:12  来源:igfitidea点击:

how to creating dynamic buttons iphone xcode?

iphonexcodedynamicbuttonuibutton

提问by Udhaya

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

我想动态创建 uibuttons。所以我循环创建带有标签的按钮并添加到视图中。所有按钮都对其标签值执行相同的操作......但我想更改按钮的属性。因此我需要使用标签值来获取 uibutton...

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

回答by Jhaliya

Use viewWithTagfunction of UIViewto access your UIButtonusing the tag value.

使用viewWithTag函数UIView来访问您UIButton使用的标签值。

See in Documentation viewWithTag

请参阅文档viewWithTag

Use it as below.

如下使用。

UIButton* myButton = (UIButton*)[mySuperView viewWithTag:1];
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:3];
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:6];
.........

回答by Vijay-Apple-Dev.blogspot.com

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

回答by Sri

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