简体   繁体   English

如何在两个视图控制器之间传递整数?

[英]How to pass an integer between two view controllers?

I am creating a Rock Paper Scissors game in Objective-C and currently I'm having an issue with bringing an integer from one View Controller to another. 我在Objective-C中创建了Rock Paper Scissors游戏,目前在将整数从一个View Controller带到另一个View Controller时遇到问题。

I am using the utility application template and I am using the FlipsideViewController as an area to set the rounds in the best (Best out of 3, 5, 7, etc.). 我正在使用实用程序应用程序模板,并且正在使用FlipsideViewController作为将轮次设置为最佳的区域(3、5、7等中的最佳)。 However, I am unable to bring that integer back to the MainViewController to put it in to use. 但是,我无法将该整数带回MainViewController以便使用。 I read through the other questions on this topic but couldn't get it to work. 我通读了有关该主题的其他问题,但无法正常工作。

Here is the protocol in the FlipSideViewController.h file 这是FlipSideViewController.h文件中的协议

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

Here is the action that occurs when the button is pressed to finalize the amount of rounds 这是按下按钮以确定回合数量时发生的动作

- (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];
}

The button in MainViewController.m that switches to FlipsideViewController 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];

}

Declaring a property for an int is different than declaring for an object like a NSNumber or NSString. 声明一个int的属性与声明一个类似NSNumber或NSString的对象是不同的。

All you need to do is to add this to your header: 您需要做的就是将其添加到标题中:

@property int someInt;

and synthesize in your implementation. 并在您的实施中进行综合。 You do not need to release this property because it isnt a true object. 您不需要释放此属性,因为它不是真正的对象。 It doesnt have any retains on it. 它没有任何保留。 And if you're in ARC you dont need to worry about that anyways. 而且,如果您使用的是ARC,则无需担心。

If this doesnt answer your question, please post what you have tried (ie give us some code to work with) and you'll probably get a more informed answer. 如果这不能解决您的问题,请张贴您尝试过的内容(例如,给我们一些代码以供使用),您可能会得到更详尽的答案。 Better question = better answer. 更好的问题=更好的答案。

The qualifiers strong/weak do not apply to int's or other primitive data types, but you would still be wise to use (nonatomic) and also maybe (unsafe_unretained) if you are targeting for iOS4. 强/弱修饰符不适用于int或其他原始数据类型,但是如果您要针对iOS4,则最好还是使用(nonatomic) (unsafe_unretained)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM