1

こんにちは私は私のボタンに2つの異なるタイトルを付けたいです。1から7の数字のタイトルを持つ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

FirstViewController.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 に答える