1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Pasing variables between views

Discussion in 'iPhone/iPad Programming' started by portreathbeach, Mar 30, 2011.

  1. portreathbeach, Mar 30, 2011
    Last edited: Mar 30, 2011

    macrumors member

    I have worked through the 'Programming in Obj-C' book by Steven Kochan and am now going through the 'Sams, teach yourself iPhone applicaton development'. I am up to chapter 12 using TabBars, and the first exercise at the end of the chapter asks you to pass a variable from one view to another.

    Please can someone explain an easy way to pass a variable to another view, or a way to have one view update a variable on another view.

    thanks in advance
  2. macrumors member


    I would use delegates in one class that can be accessed by both.
  3. macrumors member

    How would I go about this?

    I ended up calling a function in another view and passing variables that way. If I use delegates, can I reference them directly?
  4. dejo, Mar 30, 2011
    Last edited: Mar 31, 2011



    Staff Member

    What did Chapter 6 (Model-View-Controller Application Design) of your "Sams Teach Yourself iPhone Application Development" book teach you about the model side of things?
  5. macrumors member

    Chapter 6 taught me about Model-View-Controller.

    But it still doesn't explain how to set or get variables in another view.

    This is how I understand it....

    If I have 3 views (.xib), these would have 3 view controllers (.h and .m files). Each of these view controllers hold the logic (code) for various UILables, UITextFields etc. They also hold methods and functions that carry out various tasks.

    The 'model' connects to the various view controllers.

    Is this correct? If so, how can I get and set a variable in another view controller easily without having to write functions.
  6. Sykte, Mar 31, 2011
    Last edited by a moderator: Mar 31, 2011

    macrumors regular


    Are you currently able to navigate between all views controllers? What have you learned in regards to properties or memory management.

    This isn't necessarily what you're looking for.
  7. Moderator


    Staff Member

    I don't have a copy of that book so I don't know the specifics of what it teaches in terms of the model part of the model-view-controller design pattern but I doubt it would be summarized as "The 'model' connects to the various view controllers." Maybe you could elaborate on what you mean by this.

    Anyways, here's what the Apple docs have to say about Model Objects:
    So, when you have a value that you want to share between views, a good approach to take would be to store it in a model object and then have the controllers both access the same model object. There are a number of options for storing the model object, including NSUserDefaults, a singleton, a file, or a database.
  8. macrumors member

    You are correct, the book doesn't just say "The 'model' connects to the various view controllers."

    It says "A model provides the underlying data that provide information to the rest of the application. The model does not define how the application will look or how it will act."

    Later it says that all of the exercises we'll be doing in the book are small and a separate data model is not needed as the view controllers can handle the data.

    This is why I asked the question in the first place.

    You said "There are a number of options for storing the model object, including NSUserDefaults, a singleton, a file, or a database."

    Where do you actually 'store' the model object and how is it declared. Do I make a class. Sorry for the questions, I have a lot of experience in VB.net and Realbasic, and understand classes, objects etc. but Obj-C seems very complicated in comparison.
  9. Moderator


    Staff Member

    Hmm, so the book asks you to complete an exercise but hasn't taught you the fundamentals required? I find that somewhat concerning.

    You can make a class. Or not.

    One approach would be to store the object, if it's simple enough, in the NSUserDefaults. That way both viewControllers could access/update the object as needed.

    Another, though not really recommended, approach would be to store the object as a property in the application delegate, something all classes can access (since it happens to be a singleton).

    And an even simpler approach would be to store the object in your first viewController and then have a property for it in the second viewController. Once you instantiate the second viewController you set that property to the object from the first. It's a very straight-forward approach but not usually recommended since it makes the object an attribute of the first viewController and not a separate model piece.
  10. macrumors member

    Thanks for the reply, I'll try some of these methods.

    Normally the 'Sams' books are pretty good, but this one isn't anywhere near as good as the others I've read....Adobe Premiere in 24 hours and VB.net

    As I've mentioned, I have great experience with .Net, programming an in van entertainment system, with DVD player, Sat Nav, MP3 player with wireless update from my NAS in my house all written by myself. I can't understand why Apple have made it so complicated to do what Microsoft have made so easy in 'Visual Studio'.
  11. Moderator


    Staff Member

    Please elaborate.
  12. macrumors member

  13. macrumors 603

    That post is nearly 5 years old (Jun 2006). Just sayin'.

    One reason for the difference is the separation into Model, View, and Controller. I have completely changed the views in nibs with no change at all to the code, not even recompiling it. I have completely changed the code in controllers and models with no change at all to the views. I have copied code and views and reused them without change in multiple projects.

    I don't know what the unit of modularity is in VB, but in Xcode the units are separate: the unit for views is a nib (or subviews of a nib), while the unit for code is an @interface header. Two separate units, which can be combined, reused, changed, and altered separately. It may make one-off programs harder, but I think it makes long-term programming easier.
  14. macrumors member

    OK. I'll persist with it.

    I originally bought 2 books. 'Programming in Obj-C 2.0, by Steven Kochan' and the 'Sams iPhone Development'. Obviously I needed to read the programming one first to get an understanding of the language and the syntax, but I thought that after getting through that one the Sam's book would be a lot easier, but I am struggling more with it.

    I've read a lot of posts on the internet about XCode and most people say that when you get used to it, it has it's advantages. I guess I'll have to keep going, as I have a few good ideas for some iPhone apps that havn't been done yet.

    Thanks for the help
  15. macrumors 603

    Maybe you can rephrase the question as a more general one. Instead of "How do I pass variables between views?" you should ask "How do I pass variables between classes?" Or since you'll really be passing variables between instances of classes (i.e. objects), "How do I pass variables between instances of different classes?".

    If you know how to make or use classes at all, then it shouldn't be hard to come up with examples of passing variables between instances of different classes. It happens all the time. If some class has an ivar that's an NSMutableArray, then every time you send objectAtIndex: to the array, you're passing a variable (the index number) to the object of the other class (the array). The value returned is passed back as the return-value of the method.

    Next, how do you write a new class that does these two things:
    1. Accept objects passed to it as method parameters.
    2. Return objects as method return-values.

    What if the object passed as a method parameter has properties of its own, and those are what the receiving object is interested in? For example, suppose a method is interested only in the longest and shortest NSString. That's the length method of NSString. How would you write code to access the length? Or suppose the receiver is counting how many strings have different first characters? How would you write code to access the first character in the string?
  16. macrumors 68030


    While a few of those steps are no longer needed in fact there more steps that you need to do now to set up an outlet, on iOS anyway. Create the @property, @synthesize, add code to viewDidUnload, add code to dealloc.

    It is in fact something of a mess.

    On the one hand you get a lot of power but there's also a lot of responsibility to manage things that could be done better by the framework and the tools.
  17. Moderator


    Staff Member

    These things have been simplified in Xcode 4, with the new integration between the view editor and the code editor.
  18. firewood, Mar 31, 2011
    Last edited: Mar 31, 2011

    macrumors 603

    Remember that Objective C is a clean superset of ANSI C, and some consider C to be not much more that a macro-assembly language for DEC minicomputers. If you can't figure out how to pass variables using MVC OOP and well structured code, you could always use all global variables and goto statements and get the job done with a sledgehammer.

    Because Microsoft making it so easy in Visual Studio led to a proliferation of the types of apps that caused the PocketPC and Windows Mobile to be such a huge success compared to the iPhone OS?

    But I think the tools could be intentionally complicated. It's Apple's IQ test to make sure that Mac applications and now iPhone apps are only written by people who can actually read the documentation and remember a bunch of details.
  19. macrumors regular

    I would have to say passing data between views is if not easier than passing data between forms. It's a heck of a lot easier to maintain. Don't skip the basics. VB is easier in the sense you can just skip the basic and copy and paste your way to an application.
  20. macrumors member

    Thanks for the replies, especially 'firewood' for your input....NOT! I was asking about passing and sharing variables for an answer from some more educated XCoders, not some sarcastic reply.
  21. macrumors regular

    He was answering your implicit question:

    And doing an excellent job at that.
  22. macrumors member

    I do appreciate the help, but comments like "If you can't figure out how to pass variables using MVC OOP and well structured code, you could always use all global variables and goto statements and get the job done with a sledgehammer." aren't helpful and are rude.
  23. macrumors 603

    The suggestions about global variables and goto statements are more helpful than you think. It's what the Objective C compiler sometimes turns your code into anyway. So it's better to know what's actually happening "under the hood".

Share This Page