0

私はこれを持っています

UILabel *selectedLabel;
    selectedLabel = nil;
    if (is_x) {
        selectedLabel = labelField_x;
    } else if (is_y) {
        selectedLabel = labelField_y;
    } else if (is_z) {
        selectedLabel = labelField_z;
    }

これがコードで繰り返されないようにするには、タイプUILabelクラスを返すメソッドを作成するにはどうすればよいですか。

私はこれを試しました(動作しません):

ヘッダー(.h)ファイル内:

//new method
- (UILabel *) selected;

実装(.m)ファイル:

- (UILabel *) selected {
 UILabel *selectedLabel;
    selectedLabel = nil;
    if (is_x) {
        selectedLabel = labelField_x;
    } else if (is_y) {
        selectedLabel = labelField_y;
    } else if (is_z) {
        selectedLabel = labelField_z;
    }
return selectedLabel;
}

- (IBAction)buttonPressed:(id)sender{
 [self selected];

}

IBAction内でselectedLabelを返すにはどうすればよいですか。

ありがとうございました。

4

1 に答える 1

0

あなたのコードは正しいように見えUILabelます。後は、buttonPressed: メソッドに変数を作成して、返されたラベルへの参照を格納するだけです。だから変える

[self selected];

に:

UILabel *selectedLabel = [self selected];
// Now do what you want with the selectedLabel
于 2013-02-20T03:11:57.870 に答える