1

Objective-C でじゃんけんゲームを作成していますが、現在、あるビュー コントローラーから別のビュー コントローラーに整数を渡す際に問題が発生しています。

私はユーティリティ アプリケーション テンプレートを使用しており、FlipsideViewController を領域として使用してラウンドを最高 (3、5、7 などから) に設定しています。ただし、その整数を MainViewController に戻して使用することはできません。このトピックに関する他の質問を読みましたが、うまくいきませんでした。

FlipSideViewController.h ファイルのプロトコルは次のとおりです。

 @class FlipsideViewController;
 @protocol FlipsideViewControllerDelegate
  - (void)addItemViewController: (FlipsideViewController *)controller didFinishEnteringItem: (int)rounds;
 @end

ラウンド数を確定するためにボタンが押されたときに発生するアクションは次のとおりです。

- (IBAction)submitBOO:(id)sender {
int rounds = 0;
rounds = _textField.text.intValue;

if (rounds % 2 ==0){ 
   _labelBOO.text = @"Pick an odd number!"; 

}   
else 
    [self.delegate addItemViewController:self didFinishEnteringItem:rounds];
}

FlipsideViewController に切り替える MainViewController.m のボタン

- (IBAction)beginGame:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc]    initWithNibName:@"FlipsideViewController" bundle:nil];


controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];

}
4

1 に答える 1

5

int のプロパティの宣言は、NSNumber や NSString などのオブジェクトの宣言とは異なります。

これをヘッダーに追加するだけです。

@property int someInt;

実装で合成します。このプロパティは真のオブジェクトではないため、解放する必要はありません。それには何の保持もありません。ARC を使用している場合は、とにかく心配する必要はありません。

これで質問の答えが得られない場合は、試したことを投稿してください (つまり、動作するコードをいくつか提供してください)。おそらく、より詳しい回答が得られるでしょう。より良い質問 = より良い答え。

(nonatomic)修飾子 strong/weak は int やその他のプリミティブ データ型には適用されません(unsafe_unretained)が、iOS4 をターゲットにしている場合は、使用するのが賢明でしょう。

于 2012-07-14T19:23:10.353 に答える