新参者で「“自分”って必要?および「自己参照を使用せずにObjective-Cクラスのプロパティを設定する」しかし、私のケースを説明できる明確な答えはまだ得られません。
単純なクラスがあり、UI には 2 つのテキストフィールドと 1 つのボタンがあります。コードは次のとおりです。
@interface testViewController : UIViewController {
NSString *teststring_A;
NSString *teststring_B;
IBOutlet UITextField *textfield_1;
IBOutlet UITextField *textfield_2;
}
@property (nonatomic, retain) NSString *teststring_A;
@property (nonatomic, retain) NSString *teststring_B;
- (IBAction)action1:(id)sender;
- (IBAction)action2:(id)sender;
@end
@implementation testViewController
@synthesize teststring_A;
@synthesize teststring_B;
- (void)dealloc {
[super dealloc];
}
- (IBAction)action1:sender
{
teststring_A = textfield_1.text ;
NSLog(@"teststring_A in action 1 is : %@\n", teststring_A);
teststring_B = textfield_2.text ;
NSLog(@"teststring_B in action 1 is : %@\n", teststring_B);
}
- (IBAction)action2:(id)sender
{
NSLog(@"teststring_A in action 2 is : %@\n", teststring_A);
NSLog(@"teststring_B in action 2 is : %@\n", teststring_B);
}
出力は次のとおりです。
2010-11-19 15:32:14.827 test[419:207] teststring_A in action 1 is : 123
2010-11-19 15:32:14.829 test[419:207] teststring_B in action 1 is : 456
2010-11-19 15:32:14.927 test[419:207] teststring_A in action 2 is : 123
2010-11-19 15:32:14.929 test[419:207] teststring_B in action 2 is : {(
>
)}
ボタンをクリックすると、最初にaction1がトリガーされ、次にaction2がトリガーされます。私の問題は... action2 で、teststring_B の値が正しくなくなり、アプリケーションがクラッシュすることさえあります。私を混乱させるのは、(1)teststring_Aの値が正しいのはなぜですか??? (2) teststring_B は、'alloc' で作成されていない textfield_2.text によって割り当てられるため、ポインターが常に存在する必要があるとします。では、action2 で teststring_B の値が正しくなくなるのはなぜですか ??? (3) dealloc では、teststring_A と teststring_B をリリースする必要がありますよね? (そう思います )
私が知っているのは、「self.teststring_B = textfield_2.text;」のように「self.」を追加するかどうかだけです。それなら問題ないでしょう。値が正しくなります。技術的な理由が知りたいです。