0

これが私が達成しようとしていることです: プログラムで一連のカスタム オブジェクトを追加します。これらのタップのそれぞれは、既存のセグエを実行する必要があります。では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 です。

何が起こっているのですか?

4

1 に答える 1

0

あなたviewDidLoadは class のオブジェクトを扱っていますCustomButton。しかし、prepareForSegueあなたはオブジェクトをにキャストしますBookButton。これは意図されたものではなく、理由customPropertyはゼロであると思います.a)設定されておらず、b)にそのようなプロパティがないためBookButtonです。

于 2013-01-31T22:06:56.407 に答える