I am developing an iPhone application using Objective-C. I want to access a data member which is of type NSMutableArray of the RootViewController class in another class. I tried making the array static. But I would like to have a non static array. How do I achieve this?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- Visual Studio Code, MAC OS X, OmniSharp server is
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- IntelliJ IDEA can't open projects or add SDK o
- Automator: How do I use the Choose from List actio
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
You can also access the root view controller uising the SharedApplication in one line of code:
You need two things:
RootViewController
RootViewController
.In a situation where you have (most likely) one
RootViewController
per application, it makes sense to keep a reference to that object in your application's delegate. You can get the app delegate like this:In your app delegate class you should add a property called
rootViewController
that exposes yourRootViewController
object. Now you can write things likeThis satisfies the first requirement. Now, in order to access the object owned by the view controller, you'll need to add a property to
RootViewController
. You didn't really say what the object is or does, so let's just call itmyMutableArray
. Create areadonly
property with that name onRootViewController
, and now you'll be able to write things like this:This will let you do what you want to do.
I have to warn you, though: exposing an
NSMutableArray
is generally not a great idea. The reason is that if you change the contents of that array,RootViewController
will have no idea that you've done this. So if you were creating, say, a master-detail view, yourRootViewController
would not know that you've added a new object.It would be better if you wrote methods that let
RootViewController
modify the array internally. For example, you could have a method namedaddFooObject:
that manages the array. Then the view controller will know what you've done to it. For access, you could very easily return an immutable, autoreleased copy of the mutable array from your property.