What does the init in the controller and initilize in the view do .??. I want a function that runs before a view loads . I am using a MVC .
init is used in controllers to tell you when the after the controller is instantiated, but before the browser is ready. The launch method is called in your controller when the browser is ready (dom ready).
init is used in any components or subclasses of Ext.Component and is called when the class is instantiated. The configuration system for the class will have already been initialized by then.
Related
I am writing a very basic 3D modelling program. I've used LWJGL to render my objects and JavaFX to provide an user interface in a separate window, new thread.
As I saw JavaFX likes to take control over the application, but in my case this was not an option. I tried to pass my already created scene graph to the JavaFX controller class, but I didn't find a way doing this properly.
It's seems to be impossible to pass anything from outside into the main JavaFX class. The start method loads the layout from an FXML file with reflection magic, but this method is called in the constructor, therefore variables are not initialized. Defining a new constructor with parameters throws an exception (class cannot be initialized).
After struggling many hours, I gave up, I've decided to create a new scene graph in the JavaFX controller and created a getter method to it.
public class Toolbox extends Application implements Runnable {
private ToolboxLogic logic = new ToolboxLogic(); //controller, the scene graph is instantiated
...
public SceneGraph getSceneGraph() {
return logic.sceneGraph; // returns the scene graph
}
}
Not a beautiful solution, but it shall work, I said. But it doesn't.
I tried to load a file in two locations:
with code written in the LWJGL renderer
with buttons, calling a method in the controller
If I load a file from the renderer, my objects show up on the screen, but I cannot navigate with the buttons, only the root node appears in the scene graph.
If I load a file from the user interface, I can navigate on the tree, but it doesn't show up in the renderer.
It seems like here
return logic.sceneGraph;
Java would have done a deep copy instead of returning a pointer, and each part of my program is working with its own version of the scene graph.
What is a problem, and how can I make it work properly?
Thank you!
OK, I got it working by setting the scene graph static. Strange! Now, I'm really curious, why it behaves like this.
I'm presenting a page based modal using [self presentControllerWithNames:self.controllerNames contexts:self.controllerContexts];, where controllerNames is just an NSArray of NSStrings containing my interface controllers names. The problem is that I would like to access the created controllers.
The documentations says that
WatchKit loads and initializes the new interface controllers and animates them into position on top of the current interface controller.
but I would like to have a reference to them, in order to call the becomeCurrentPage method from outside.
So, I would like to be able to save those controllers after they are created in a list, and programmatically change the page using something like [self.controllers[2] becomeCurrentPage].
Because you're allowed to provide a context when you present an interface controller, you can pass a reference to self. That way, you can establish a reference from the presented controller to its parent. Once that relationship exists, you can use things like delegation patterns to communicate.
I use this extensively in my own Watch app, and I've wrapped a lot of these mechanics in my JBInterfaceController subclass on GitHub.
I have two view controllers - one "main" view controller which displays the main content and one "settings" table view controller which is held in a container within a slide-out view. The concept is this:
User taps an item on settings panel.
Settings TVC creates a "Button States" dictionary object with all of the button settings.
Button States dictionary is passed to a class that converts the button states to a dictionary of settings that the main model object can understand.
_______???
At this point, I need to inform the main view controller (which holds the instance of my main model object) that the settings have been updated and it needs to update the settings on its model object. How do I go about doing this? Should I have a class method on the main view controller and include the header file in my settings conversion class?
Contrary to the other answers, there's really no need to use a singleton here. All you need is an object that's shared between the main view controller and the settings view controller, or some way to pass information between them. Here are some options that don't require a new singleton class:
shared model: If you're using a model class to keep track of your settings, both your main view controller and settings view controller can access the model if you simply tell them both about the model. For example, if you create both view controllers when your app starts up, you can create the model at the same time and pass the model to both controllers:
MyModel *model = [[MyModel alloc] initWithFilePath:somePath];
MainViewController *mainVC = [[MainViewController alloc] initWithNibName:nil bundle:nil];
SettingsViewController *settingsVC = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
mainVC.model = model;
settingsVC.model = model;
shared object: If you don't need/want a whole model class that sticks around and you only need to communicate some changes between the two controllers, let them share just a simple data container, like a dictionary. Let's say, for example, that the main view controller is responsible for creating the settings view controller and then pushing it onto the navigation stack. It can easily say: "Here's a mutable dictionary; use this to get any settings you need, and to record any changes." The code in the main view controller would look something like this:
self.settings = [NSMutableDictionary dictionary];
// ...code to add all the settings to the dictionary...
SettingsViewController *settingsVC = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
settingsVC.settings = self.settings;
[self.navigationController pushViewController:settingsVC];
delegation: Consider the previous situations, but instead of passing a mutable dictionary or a model object to the settings view controller, pass the main view controller itself as the shared object. If the settings are properties of the main view controller, the settings view controller can access those properties (using accessors, please) to get and set the settings. Or turn it around and have the main view controller keep maintain a reference to the settings view controller so that it can ask for the values for any settings it needs. Either way, one view controller is acting as a helper for the other, and that's the delegate pattern in a nutshell.
NSUserDefaults: The shared user defaults object is already a singleton, so you don't really need a different one. Both view controllers can simply access the shared user defaults object to get/set the settings they need.
Whichever one you choose, you'll want to make sure that you read the settings in the -viewWillAppear for each controller and update the UI as necessary.
Your question is mostly about how to get two view controllers to talk to each other. There are many ways to do that, and a singleton is never required for any of them.
I have an app that does something similar. In my settings view, whenever a setting is modified, I have the settings view call a singleton object that keeps track of the settings in the settings view. Upon switching back to the main view, in the viewWillAppear method I check the singleton to see what settings were set and update my UI accordingly.
You can create singleton class for settings.
here is link how to do it Care and Feeding of Singletons
Let's say that yours singleton is called SharedSettings.
Each time you are changing settings in view you should change them in your SharedSettings singleton and tell view to apply this changes. To "tell the view" there are 2 most common methods in objective-c: delegate and NSNotificationCenter.
Here is tutorial for delegate Example for delegate
And here is the link for notifications NSNotificationCenter Tutorial
The difference between delegate and notifications is that delegate is used to notify one class e.g. like cellForRowAtIndexPath and notification is used to notify many observers with one notification e.g. let's assume that you have 5 view controllers and in the setting you change background from red to green and you need to notify all 5 views to change their colors.
also notifications are considered to be a bit slowlier than delegates.
If you will have questions about singleton, delegates or notifications feel free to ask.
You don't need to restrict the communication with your model to just the "main" view controller, your settings table view controller can directly access the model too! Funnelling all the model access through the one place doesn't usually get you much benefit, and instead demands you invent pointless parallel systems to communicate the model values to & from that main controller.
Make the model object(s) a singleton instead and allow access from any controller.
However if you really need to keep your architecture, you need a way of signalling back that the main view controller that the settings view is closing. Perhaps post a notification that the main view controller observes, the updated settings dictionary can be the notification object.
In my project, I am having an outlet for navigation controller. This works perfectly in general, but whenever my app receives memory warning, it creates a new object of root view controller class, that is assigned to the navigation controller through interface builder. After this event, two objects of root view controller resides in the memory and events gets fired twice. This is creating a mess in my case. Can you please suggest me a solution to this problem?
release the member variables, in viewDidUnload method which is called whenever the memory warning occurs. dealloc is not called so u need to release in viewDidUnload.
hope this helps.. happy coding :)
When view is loaded manually, developer remains in control when it comes to initializations, we choose what initializer to call, what variables to set etc.
When view is loaded from the storyboard segue ... what happens to that initializer? Where should variables be set i'd like to be available once view had been loaded?
Please help me understand the sequence here. How is instance of the class created here, who creates it and how can we intervene and help set it up to our liking?
When a view is loaded from a nib or storyboard, it's -initWithCoder: method is called. Like -initWithFrame:, -initWithCoder: is a designated initializer for UIView. If you're going to do any custom initialization for a UIView subclass, you should make sure that it happens for both these methods. One common technique is to add a common initialization method that you call from both -initWithFrame: and -initWithCoder:. See my answer to Custom view and implementing init method? for a more detailed description.
Note that the documentation for -initWithFrame: explains:
If you use Interface Builder to design your interface, this method is
not called when your view objects are subsequently loaded from the nib
file. Objects in a nib file are reconstituted and then initialized
using their initWithCoder: method, which modifies the attributes of
the view to match the attributes stored in the nib file.