0

ボタン1とボタン2の2つのボタンがあります。タッチされた対応するボタンごとに NSSet を作成し、ボタン 2 がタッチされたときに set1 の値を表示したい、またはその逆を行いたい。ボタン 1 が押されるとセット 1 のみが印刷され、ボタン 2 が押されるとセット 2 のみが印刷されます。ボタン2が押されたときに表示/使用できるように、ボタン1アクションで作成されたセットを保持するにはどうすればよいですか。私の簡単なコードを見てください

実装では、私は持っています:

- (IBAction)button1:(UIButton *)sender {

    //somecode

    selectionButton1 = [[NSMutableArray alloc ] initWithObjects:@0,@1,@1,@4,@6,@11, nil];

    NSMutableSet *set1 = [NSMutableSet setWithArray: selectionButton1];
    NSLog(@"selectionButton1  = %@", set1);
    NSLog(@"selectionButton2  = %@", set2);
}


- (IBAction)button2:(UIButton *) sender {

    //somecode

    selectionButton2 = [[NSMutableArray alloc ] initWithObjects:@0,@5,@6,@7,@8,@10, nil];
    NSMutableSet *set2 = [NSMutableSet setWithArray: selectionButton2];
    NSLog(@"selectionButton1  = %@", set1);
    NSLog(@"selectionButton2  = %@", set2);
}
4

1 に答える 1

1

セットのプロパティを作成します。他のクラスからそれらにアクセスする必要がない場合は、.m ファイルのプライベート クラス拡張子でそれらを内部プロパティにします。次に、を使用self.propertyNameしてプロパティにアクセスします。

MyClass.m:

@interface MyClass // Declares a private class extension

@property (strong, nonatomic) NSMutableSet *set1;
@property (strong, nonatomic) NSMutableSet *set2

@end

@implementation MyClass

- (IBAction)button1:(UIButton *)sender {

    //somecode

    selectionButton1 = [[NSMutableArray alloc ] initWithObjects:@0,@1,@1,@4,@6,@11, nil];

    self.set1 = [NSMutableSet setWithArray: selectionButton1];
    NSLog(@"selectionButton1  = %@", self.set1);
    NSLog(@"selectionButton2  = %@", self.set2);
}


- (IBAction)button2:(UIButton *) sender {

    //somecode

    selectionButton2 = [[NSMutableArray alloc ] initWithObjects:@0,@5,@6,@7,@8,@10, nil];
    self.set2 = [NSMutableSet setWithArray: selectionButton2];
    NSLog(@"selectionButton1  = %@", self.set1);
    NSLog(@"selectionButton2  = %@", self.set2);
}

@end

プロパティの詳細については、Apple のドキュメントを参照してください。

于 2014-09-13T03:59:22.190 に答える