PDA

View Full Version : Pasing variables between views




portreathbeach
Mar 30, 2011, 10:24 AM
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



ritsard
Mar 30, 2011, 01:22 PM
I would use delegates in one class that can be accessed by both.

portreathbeach
Mar 30, 2011, 03:29 PM
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?

dejo
Mar 30, 2011, 04:20 PM
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?

portreathbeach
Mar 30, 2011, 04:46 PM
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.

Sykte
Mar 31, 2011, 07:43 AM
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.


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

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

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

dejo
Mar 31, 2011, 09:56 AM
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.
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:
Model objects encapsulate the data specific to an application and define the logic and computation that manipulate and process that data. For example, a model object might represent a character in a game or a contact in an address book. A model object can have to-one and to-many relationships with other model objects, and so sometimes the model layer of an application effectively is one or more object graphs. Much of the data that is part of the persistent state of the application (whether that persistent state is stored in files or databases) should reside in the model objects after the data is loaded into the application. Because model objects represent knowledge and expertise related to a specific problem domain, they can be reused in similar problem domains. Ideally, a model object should have no explicit connection to the view objects that present its data and allow users to edit that data—it should not be concerned with user-interface and presentation issues. [emphasis mine]
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.

portreathbeach
Mar 31, 2011, 01:17 PM
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.

dejo
Mar 31, 2011, 01:31 PM
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.
Hmm, so the book asks you to complete an exercise but hasn't taught you the fundamentals required? I find that somewhat concerning.

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.
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.

portreathbeach
Mar 31, 2011, 04:10 PM
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'.

dejo
Mar 31, 2011, 04:12 PM
I can't understand why Apple have made it so complicated to do what Microsoft have made so easy in 'Visual Studio'.
Please elaborate.

portreathbeach
Mar 31, 2011, 04:21 PM
Here is one example that someone has posted on another site:

http://macosx.com/forums/mac-os-x-system-mac-software/272806-why-so-complicate-vb-net-versus-xcode.html


Don't get me wrong, I am still learning Obj-C and XCode, so there may be a good reason for all of this, but up until now, everything seems to be a lot more complicated and take a lot longer to program.

chown33
Mar 31, 2011, 04:36 PM
Here is one example that someone has posted on another site:

http://macosx.com/forums/mac-os-x-system-mac-software/272806-why-so-complicate-vb-net-versus-xcode.html

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.

portreathbeach
Mar 31, 2011, 04:44 PM
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

chown33
Mar 31, 2011, 05:02 PM
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.

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?

PhoneyDeveloper
Mar 31, 2011, 05:54 PM
That post is nearly 5 years old (Jun 2006). Just sayin'.

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.

dejo
Apr 1, 2011, 12:02 AM
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.

These things have been simplified in Xcode 4, with the new integration between the view editor and the code editor.

firewood
Apr 1, 2011, 12:31 AM
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.

I can't understand why Apple have made it so complicated to do what Microsoft have made so easy in 'Visual Studio'.

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.

Sykte
Apr 1, 2011, 07:02 AM
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.

portreathbeach
Apr 1, 2011, 08:48 AM
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.

ChOas
Apr 1, 2011, 11:09 AM
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.

He was answering your implicit question:

I can't understand why Apple have made it so complicated to do what Microsoft have made so easy in 'Visual Studio'.

And doing an excellent job at that.

portreathbeach
Apr 1, 2011, 04:05 PM
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.

firewood
Apr 1, 2011, 07:46 PM
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".