-3

I am new to iOS development, but this is a question which I have a lot of trouble with and could help others. I need to pass a simple integer variable between two views. This is an example of a variable I need to pass:

int tally = 1;

People talk about messing with delegates and protocols to pass data between views, but frankly, that is all a bit over my head. Is there an easier way to pass simple variables such as integers between two view controllers?

Thank you kindly for any responses!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Enigmatical
  • 147
  • 1
  • 9
  • 1
    What is the relationship between your view controllers? Do you transition from one to another? – Connor Pearson Feb 22 '14 at 04:55
  • yes, there is button which causes a modal segue between the two view controllers. I am not actually trying to display data from one to another, I just need to keep the value in the memory – Enigmatical Feb 22 '14 at 05:00
  • I am sorry if that is a duplicate! I will look at that question now and see if it works with the context of my project – Enigmatical Feb 22 '14 at 05:15
  • This one is good answer : http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Divya Bhaloidiya Feb 22 '14 at 06:12
  • If you're new to iOS development, asking questions on Stack Overflow is not the place you need to be. You should find a good book or a series of online tutorials. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). The Big Nerd Ranch books are excellent, and lots of people like the Stanford iOS course on iTunes U. Good luck! – jscs Feb 22 '14 at 20:15

2 Answers2

1
@interface View1:UIView
@property int passingInt;
@end

@interface View2:UIView
@property int passingInt;
@end

Now, you can directly set the variables like,

view1.passingInt=view2.passingInt;

or

view1.passingInt=100;
santhu
  • 4,796
  • 1
  • 21
  • 29
1

Although these are not the correct ways to do this kind of stuff I'll give you two different methods.

Create a global variable in a class header file, say CommonVar.h. You can now use this variable with anything that has this header file imported

//CommonVar.h

int foo;

//This is the entire file

Now if you import this file in any class, you can use foo directly.

The other method is to use singletons and their variables. You can read about them here http://www.galloway.me.uk/tutorials/singleton-classes/

I am telling again this is not the correct way to do this but I think you will find them simple than handling delegates.