one of my view controllers has several UISwitches, and I want another view Controller to be able to access the values of the UISwitches for If/and statements. How do I do this in Objective-c?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- How to dynamically load partial view Via jquery aj
- iOS (objective-c) compression_decode_buffer() retu
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Forward request from servlet to jsp
- Custom Marker performance iOS, crash with result “
You probably don't want to do that. If you want a second ViewController to have different behavior based on switches thrown in the first, you should just bind the switches to User Defaults, which you can read anywhere in your app.
http://icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/
This is a bad idea as it would create an unneeded dependency between the view controllers.
If you still want to do it, just pass a reference of the first view controller with the switches to the second view controller. Then, in your second view controller just access the corresponding UISwitch's
on
property.However, instead of going that route, I strongly suggest that you create a custom class to hold the boolean state of each UISwitch. An instance of this class could either be a singleton, or contained in the application delegate. See this answer for how to do it with both approaches.
A custom class is better because a UISwitch is just a way to represent some property in your data model. And if tomorrow you replaced the UISwitch with another fancy control, the second view controller should still continue to work. Both view controllers have a reference to an object of this custom class. Whenever there is a change, the first controller updates this object.
The class interface could be something like this: