-6

サンプル: textfield1 textfield2 textfield3 textfield4 textfield5

                        button

質問: iOS には 5 つのテキスト フィールドがあり、ボタンを値 1 と仮定します。ボタンが 1 回目に押されると、値 1 が最初のテキスト フィールドに表示され、ボタンの値が 2 にインクリメントされます。ボタンが押されると、 2 回目は、値 2 が 2 番目のテキスト フィールドに表示され、ボタンの値が 3 に増加します。5 つの値すべてについて同様に続けます。

4

1 に答える 1

2

タグを使ってみてもいいかもしれません。タグ 1 から 5 を使用してテキスト フィールドをセットアップし、ビュー コントローラーを次のように実装します。

#import "ButtonValueViewController.h"

@interface ButtonValueViewController ()
// Properties
@property (nonatomic) int buttonValue;

// Actions
- (IBAction)buttonTapped:(UIButton *)sender;
@end

@implementation ButtonValueViewController

@synthesize buttonValue;

// ...

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Initialise buttonValue to 1
    self.buttonValue = 1;
}

- (IBAction)buttonTapped:(UIButton *)sender
{
    // Get the textfield with the tag number the same as buttonValue
    UITextField * textField = (UITextField *)[self.view viewWithTag:self.buttonValue];

    // Set textfield's text to buttonValue
    textField.text = [NSString stringWithFormat:@"%d", self.buttonValue];

    // Increment buttonValue
    self.buttonValue++;
}

@end
于 2013-06-10T01:02:01.643 に答える