1

嗨,我想为我的按钮设置 2 个不同的标题,我有 7 个按钮,带有 1 到 7 个数字标题,还有 8 个按钮,我想将“设置”作为标题,

你能帮我知道如何在循环中指定标题吗?

提前致谢!

这是我的图片,我想在我的最后一个按钮中使用“设置”标题而不是 8

这是我的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int rows = 4, columns = 2;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 200*columns, 105*rows)];
int currentTag = 0;

for (int y = 0; y < rows; y++) {
    //     currentTag++;

    for (int x = 0; x < columns; x++) {

        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
        button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
        button.tag = currentTag; // ADDED
        currentTag++;
        [button.layer setBorderColor: [[UIColor blackColor] CGColor]];
        [button.layer setBorderWidth: 1.0];
        [button setTitle:[NSString stringWithFormat:@"%d",currentTag] forState:UIControlStateNormal];
        button.frame = CGRectMake(200*x, 105*y, 200, 105);
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [buttonView addSubview: button];

    }
}

// Center the view which contains your buttons
CGPoint centerPoint = buttonView.center;
centerPoint.x = self.view.center.x;
buttonView.center = centerPoint;
[self.view addSubview:buttonView];    

}


-(void)buttonPressed:(UIButton *)button
{
NSLog(@"button %u -- frame: %@", button.tag, NSStringFromCGRect(button.frame));
}

编辑 2 以将另一个视图连接到按钮

第一视图控制器.h

 @interface FirstViewController : UIViewController{
SecondViewController *createViewController;
}

-(void)buttonPressed:(UIButton *)button;


@end

我的按钮代码

-(void)buttonPressed:(UIButton *)button
{
NSLog(@"button %u -- frame: %@", button.tag, NSStringFromCGRect(button.frame));
{ if (!createViewController) { createViewController = [[TDDSecondViewController alloc]        initWithNibName:@"TDDSecondViewController" bundle:nil]; 

}
}
}
4

3 回答 3

3

在您的内部循环中:

  for (int x = 0; x < columns; x++) {

        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
        button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
        button.tag = currentTag; // ADDED
        currentTag++;
        [button.layer setBorderColor: [[UIColor blackColor] CGColor]];
        [button.layer setBorderWidth: 1.0];
        if (x == 1 && y == 3) {
           [button setTitle:@"Set" forState:UIControlStateNormal];
        } else {
           [button setTitle:[NSString stringWithFormat:@"%d",currentTag]forState:UIControlStateNormal];
        }

        button.frame = CGRectMake(200*x, 105*y, 200, 105);
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [buttonView addSubview: button];

    }
于 2012-06-20T07:01:54.493 回答
1

您可以检查 currentTag 的值是否等于 8,然后设置不同的标题:

if (currentTag == 8) {
    [button setTitle:@"Set" forState:UIControlStateNormal];
} else {
    [button setTitle:[NSString stringWithFormat:@"%d",currentTag]forState:UIControlStateNormal];
}
于 2012-06-20T07:05:45.007 回答
1
if (button.tag==yournumber) {
           [button setTitle:@"Set" forState:UIControlStateNormal];
        } else {
           [button setTitle:[NSString stringWithFormat:@"%d",currentTag]forState:UIControlStateNormal];
 }

像这样试试。我想这会对你有所帮助。

于 2012-06-20T07:06:38.100 回答