以编程方式添加按钮应该移动到另一个ViewController

时间:2013-09-24 10:28:12

标签: ios uibutton

我有程序生成的按钮:

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

点击按钮转移到下一个视图后如何做到这一点?

1 个答案:

答案 0 :(得分:1)

使用:

button.tag=i;

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

UIButton *button=(UIButton *)[self.view viewWithTag:i];