small medium large xlarge

Generic-user-small
24 Nov 2009, 23:17
David Hall (2 posts)

Hello,

I’m a little confused about the scope of my custom objects when I’ve used them in an iPhone application. I’ve only been writing with objective C and the Iphone SDK for a short time now.

The only place I’ve been able to create a custom object and then reference it in different methods/parts of the .m file has been when creating it within the init method of a view controller. I am then able to use the objects accessors to get to the instance variables within IBAction sections of that view controller.

Is it possible to make an object global to the application? Will you be doing a screen cast that covers object scope?

Best Regards,

David

Biopic_100x100_pragsmall
24 Nov 2009, 23:37
Bill Dudney (917 posts)

Hi David,

You really don’t want to make anything global.

The general idea is that variables have scope within the curly braces they are declared in. Thus instance variables have scope in all the instance methods (ones that start with ‘-‘ instead of ‘+’), method local variables have scope inside the methods’ curly braces etc.

If you’d like an object to be used through a chain of view controllers you can pass it from one to the other via properties.

Good luck!

Generic-user-small
25 Nov 2009, 22:31
David Hall (2 posts)

Thanks for the reply Bill,

I not sure how you’d pass the object from one controller to another with a property, but have stumbled across a solution.

I passed the pointer for my object through the init method of my new view controller and this seemed to work OK!

Thanks again for replying…. Like your site!

You must be logged in to comment