2013-09-24 65 views
-1

我已經programmically生成按鈕這樣的:programmically添加的按鈕應該移動到另一個視圖控制器

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

如何做到這一點點擊按鈕轉移後發生在下一個視圖?

+0

使用標籤屬性來標識您的按鈕。 – NANNAV

回答

1

使用:

button.tag=i; 

設置標籤按鈕,而在viewDidLoad創建按鈕,這樣就可以通過使用上面的標籤識別您的按鈕。

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

謝謝,這是識別我的按鈕的更好方法。 – Nizzre

相關問題