-1

I'm quite new to OOP and design patterns, but I've implemented the Singleton pattern once before to pass static arrays and string objects between different ViewControllers.

I was wondering if there was an easy way to have all my ViewControllers listen for a gesture or event, and then execute some code once said gesture/event occurs. I was thinking of either using the UIGestureRecognizer object or motionBegan method of the UIResponder class.

I've already gotten this working by making a new UIWindow class, lodging the code in there, and changing the class of MainWindow.xib to my custom class. This works, and is a nice solution, but I'm still wondering if there's a non-IB way of implementing this solution (e.g., singletons)

Thanks ahead of time for your guidance.

Sample code is greatly appreciated!

ArtSabintsev
  • 5,170
  • 10
  • 41
  • 71

2 Answers2

1

You subclassed UIWindow? That's very uncommon. Read about the UIApplication delegate object, and the delegate pattern in general. It's basically your app's main singleton in Cocoa.

If you don't want to pass it around, you can always retrieve the app's delegate by calling

[[UIApplication sharedApplication] delegate]
benzado
  • 82,288
  • 22
  • 110
  • 138
  • you definitely don't want to be subclassing `UIWindow` - or even using it much as a beginner, set the root controller for the window and be done with it – bshirley Jul 15 '11 at 17:35
  • I'll look into this - I'm thinking what I want to do is impossible, aside from subclassing UIWindow, or just forego the implementation of this feature. Thanks! – ArtSabintsev Jul 18 '11 at 14:53
  • Another thought: you should read about UIResponder and the responder chain: [this question](http://stackoverflow.com/questions/3356863/how-do-i-add-the-uiapplicationdelegate-to-the-end-of-the-uiresponder-chain) might be a useful approach. – benzado Jul 18 '11 at 16:24
0

If you want lots of unconnected objects/controllers to be notified of a single event, you can trigger it however you like (target/action from a control(s) or gesture recognizer(s)).

You might want to look into NSNotificationCenter and NSNotification, you could have multiple objects listing for the notification of the event/change.

bshirley
  • 8,217
  • 1
  • 37
  • 43
  • Can this be done without adding too much code to the existing application? I wrote a bug-reporter that is triggered on a shake event, which I stored in a subclassed UIWindow object. All I do now is make a connection in Interface Builder to access this new window and the shake-event code is available. I did it this way so I could revert the code back to a non-debug mode without too much effort (change the connection back to UIWindow). – ArtSabintsev Jul 15 '11 at 18:02