1

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.

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.). However, I am unable to bring that integer back to the MainViewController to put it in to use. 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

 @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

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


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

}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Snorf
  • 108
  • 1
  • 8
  • Duplicate question: http://stackoverflow.com/q/5210535/11976 – Philip Regan Jul 14 '12 at 18:34
  • Note the "I read through the other questions on this topic but couldn't get it to work." – Snorf Jul 14 '12 at 18:46
  • This is still a duplicate question. As shown in the accepted answer to the question linked, you are going to have to learn the principles of working in Cocoa and ViewControllers. Passing data between two views in a utility app is not that hard after one has read the documentation. – Philip Regan Jul 14 '12 at 19:44

1 Answers1

5

Declaring a property for an int is different than declaring for an object like a NSNumber or 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.

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.

bkbeachlabs
  • 2,121
  • 1
  • 22
  • 33