2012-07-14 122 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

相关问题