2012-07-14 126 views
1

我在Objective-C中創建了一個Rock Paper Scissors遊戲,目前我遇到了將一個View Controller中的整數從一個View Controller帶到另一個的問題。如何在兩個視圖控制器之間傳遞整數?

我正在使用實用程序應用程序模板,並且我正在使用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]; 
} 

在MainViewController按鈕的量發生的操作。 m切換到FlipsideViewController

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


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

} 
+0

重複問題:http://stackoverflow.com/q/5210535/11976 – 2012-07-14 18:34:29

+0

請注意「我通讀了關於此主題的其他問題,但無法使其正常工作。」 – Snorf 2012-07-14 18:46:17

+0

這仍然是一個重複的問題。正如所接受問題的答案所示,您將不得不學習在Cocoa和ViewControllers中工作的原則。在閱讀文檔後,在實用程序應用程序中的兩個視圖之間傳遞數據並不難。 – 2012-07-14 19:44:03

回答

5

聲明一個int的屬性不同於聲明像NSNumber或NSString這樣的對象。

所有你需要做的是將它添加到您的標題:

@property int someInt; 

,並在您的實現合成。你不需要釋放這個屬性,因爲它不是一個真正的對象。它沒有任何保留。如果你在ARC,你不需要擔心。

如果這不能回答你的問題,請發佈你已經嘗試過的(即給我們一些代碼工作),你可能會得到一個更明智的答案。更好的問題=更好的答案。

限定詞強/弱並不適用於廉政局或其他原始數據類型,但你仍然是明智的使用(nonatomic),也可能(unsafe_unretained)如果你正在爲iOS4的目標。

+0

請注意,沒有限定符=(原子,分配),原子是_SLOW_,並不一定需要@synthesize。 – CodaFi 2012-07-14 19:47:04

+0

公平點!我已經編輯過在原語中包含一些限定符的討論 – bkbeachlabs 2012-07-14 19:51:51

相關問題