-1

我有这样的程序生成按钮:

- (void)viewDidLoad
{
    [super viewDidLoad];

    int y = 127;
    for (int i=0; i<=6; i++) {
      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [button addTarget:self action:@selector(aMethod:)   forControlEvents:UIControlEventTouchDown];
      //[button setTitle:@"Show View" forState:UIControlStateNormal];
      button.frame = CGRectMake(26, y, 268, 41);
      [button setTitle:[NSString stringWithFormat:@"Button-%i", i] forState:UIControlStateNormal];
      [button setBackgroundImage:[UIImage imageNamed:@"cys_button.png"] forState:UIControlStateNormal];
      [button.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeueLTPro-Cn" size:21]];
      [button setTitleColor:[UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1]  forState:UIControlStateNormal];
      button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
      button.contentEdgeInsets = UIEdgeInsetsMake(8, 15, 0, 0);

      [scrollViewContent addSubview:button];
      y=y+43;
    }
}

以及单击后显示文本的方法:

-(void)aMethod:(id)sender{
    UIButton *button = sender;
    int y = 127;
    int i = 6;

    for (int x=0; x<=i; x++) {
      NSString *frameString = [NSString stringWithFormat:@"{{26, %i}, {268, 41}}", y];
      if ([NSStringFromCGRect(button.frame) isEqualToString:frameString])  {
        NSLog(@"button %i clicked..", x);
      }
      y= y+43;
  } 
}

单击按钮转移到下一个视图后怎么做?

4

1 回答 1

1

利用:

button.tag=i;

在创建按钮时将标签设置为按钮,viewDidLoad以便您可以使用上面的标签来识别您的按钮。

UIButton *button=(UIButton *)[self.view viewWithTag:i];
于 2013-09-24T10:50:04.420 回答