I’m a newbie doing Objective-C, coming from Flex/Actionscript development.
I have an iPhone app with an UIApplicationDelegate conforming delegate called MyAppDelegate – it has a UIWindow.
Instead of adding buttons, labels and whatnot directly to the window, I guess I’m supposed to make a child class of UIViewController for every screen I wanna make. When that screen should be displayed, I add the respective controller’s view to the window as a subview. When another screen should be displayed, I should pop off any other view from the window and add the new view. Hope I got everything correct so far…
My intent is to make each view controller only know about its own things, so let’s say I wanna call view A from view B, in ActionScript I’d add a button in A firing off an event which would be caught in view A’s owning object (could be the application), which could take proper action (remove view A, instantiate view B and display it).
How do I do this in Objective-C?
A UIControl, such as UIButton, can have any number of event listeners registered with:
The target would be the view controller you want to receive the method, and the action is the method you want called. For a button, events is usually just
UIControlEventTouchUpInside. If the target is nil, the event will pass up the responder chain until a responder implements the action. If you pass@selector(buttonClicked:)then the target should have this method:The sender will be the button that was clicked. IBAction is equivalent to a void return type. You can bind the action in Interface Builder if you prefer that to doing it programmatically.
This is basically correct, but usually you use a meta view controller like
UINavigationControllerto manage view controllers. Even if you do not use the UI that a meta controller might present, it is convenient to have view switching managed for you.