これが私が達成しようとしていることです: プログラムで一連のカスタム オブジェクトを追加します。これらのタップのそれぞれは、既存のセグエを実行する必要があります。ではperformForSegue
、destinationViewController のいくつかのプロパティを設定します。
セットアップは次のとおりです。プロパティ CustomProperty を持つカスタム オブジェクト CustomButton があります。私のメイン ビューには、NSMutableArray
これらのオブジェクトのインスタンスが取り込まれた があります。タップ認識機能がカスタム ボタンに接続されています。また、メイン ビューと別のビューの間には、カスタム ボタンをクリックしたときに実行されるセグエがあります。また、いくつかの処理を行う prepareForSegue もあります。
Main viewDidLoad は次のようになります。
- (void)viewDidLoad
{
buttons = [[NSMutableArray alloc] init];
CustomButton *customButton1 = [[CustomButton alloc] init];
// Attach touch recognizers
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(customButtonPressed:)];
[customButton1 addGestureRecognizer:singleFingerTap];
[self.view addSubview:customButton1];
// Add it to the buttons array
[self.buttons addObject:customButton1];
[super viewDidLoad];
NSLog(@"---------viewDidLoad-----------");
NSLog(@"count = %u", buttons.count);
for (id tempButton in buttons)
{
NSLog(@"Custom Property = %@", ((CustomButton *)tempButton).customProperty);
}
}
prepareForSegue は次のようになります
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"---------prepareForSegue-----------");
NSLog(@"count = %u", buttons.count);
for (id tempButton in buttons)
{
NSLog(@"Content = %@", ((CustomButton *)tempButton).customProperty);
}
}
このアプリケーションを実行すると、viewDidLoad がカウントされ、customProperty が期待どおりに出力されます。ボタンを押すと prepareForSegue が呼び出され、カウントは 1 (予想どおり) ですが、customProperty は null です。
何が起こっているのですか?