1

I'm a beginner at iOS programming, so I'm probably asking something obvious. Anyway, I have a UISwitch in my storyboard correctly connected to a property called mySwitch declared in my UIViewController. I'd like to know how to access that property from inside my app delegate. For instance, from inside applicationWillResignActive:

- (void)applicationWillResignActive:(UIApplication *)application
{
    ??? // don't know how to access the mySwitch property
    if ([mySwitch isOn]) {
        // perform some task
    }
}

The answer I'm looking for will probably satisfy the more general question "What's the correct way to access a view object from my app delegate?" to which I haven't found an answer. Any help is really appreciated.

Piovezan
  • 3,215
  • 1
  • 28
  • 45

1 Answers1

0

You shouldn't be trying to - that isn't what the app delegate is for. Or, depending on the task, the state of the switch should be stored to user defaults or something like that.

If the view controller should do that job, it can do this by observing the UIApplicationWillResignActiveNotification notification (if it really needs to be done then).

If the app delegate should do the job then the view controller should update the value in NSUserDefaults whenever the switch is changed.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • The task is to save the state of some view objects (a couple of `UILabel`s and a `UIDatePicker`) in `NSUserDefaults` when the application leaves the active state and restore them later (when the switch is set to On it means the screen state should be saved). I suppose it should be done by the view controller then, right? – Piovezan Aug 06 '13 at 16:42
  • Yes, view controller. There probably won't be a real overhead if you save them when they change rather than when the app resigns active. – Wain Aug 06 '13 at 19:16
  • Thank you. Just for the record, I've found the code to implement observing the appropriate notification here: http://stackoverflow.com/questions/9011868/whats-the-best-way-to-detect-when-the-app-is-entering-the-background-for-my-view – Piovezan Aug 06 '13 at 19:24