How to know if my non-ViewController class has loaded (like viewDidLoad) - swift

I am sure this is an amateur question, but I am starting to develop in osx, and I created an NSTableView that consists of two classes. One is the VC where it lives, and the other one is a 'TableHelper' class that sets it up.
Since the TableHelper class is the delegate and dataSource of my table, I want to be able to set some things up before it loads (like a viewDidLoad method) is there an equivalent for this NSObject, NSTableViewDelegate/DataSource class?

It depends on whether your table helper is created in code or in a storyboard/xib.
If code, then there's no delayed loading; it exists when you create it. If storyboard/xib, then you're looking for the awakeFromNib method.

Related

Using data between any controller

I have spent hours trying to use a set a tableViewCell from a picker selection in another view. I have posted two questions, that brought no answers. So i decided to approach it differently. I tired making a global variable, but then figured out, I had to make a dataClass file which wouldn't work for me because I have to use a viewController. Im beginning to lose hope. Is their any way to set the title of a table view cell from another view? Im not looking for a giant chunk of code, just a place to start. The way to actually do it, if its possible. Thanks in advance.
Yes, this is definitely possible. In Model-View-Controller systems all information sharing happens through your model class. Make it a singleton object (singleton is similar to global variables, but it has proper initialization).
Create a class with the data that must be shared. Create a class method of that class to produce the sole instance of that class. Define and initialize a static variable holding that instance. Use dispatch_once to initialize that instance. Here is an answer illustrating this approach.
With a singleton instance in place, all your view controllers can access the model as necessary. One view controller can set properties of the model, so that when the other view controller comes along, the data is ready for it to process.

How to ensure that all the UIViewController objects in the project are being created only once?

In the project i am currently working upon, there are lots and lots of UIViewController objects (of some UIViewController subclass) are created and used. And believe me it was creating lots of issues. And I am working upon it (kind of refactoring).
As I see, most of the (those)objects required initialization only once and used multiple times. As I will be working on this project from now (and also the project is of long duration), How can I be sure that each of the UIViewController Subclass object is only one alive at a time.
I doubt if I should make all the UIViewControllers Singleton. And if so, How should I implement that. Meaning; Should I initialize all the objects in applicationDidFinishedLaunching:WithOptions or where?
Another Question is: (As I think might not be true) Should all the UIViewController in project be singleton?
If you want to ensure that all the UIViewController objects in the project are being created only once then only way is Singleton. And you need not to intialize them in applicationDidFinishLaunching. You can intialize them any where(usually where you need them).
Go to link for creating singleton properly: http://cocoasamurai.blogspot.in/2011/04/singletons-your-doing-them-wrong.html
Source : Make UIViewController a singleton?
Well making them singleton if you want them to be only one object of each subclass is not a bad idea,
if you made them singletons, don't initialize them in applicationDidFinishedLaunching:WithOptions but initialize them as soon as you need them (read more about lazy initialization)
However i would suggest to make each webView a property of your Appdelegate, such that whenever you need them you will get them from the appDelegate

Best Way to Add Object Via Other Class?

In my iPhone app, I have a custom UIViewController class setup which adds some UIImageViews and things.
How can I access this class via my main UIViewController and, for example, call a method from that outside class and have it add those UIImageViews to the view?
Make property and public methods, after that you can call them in target viewcontroller.
p.s. But I never used this approach because it breaks controller's logic. Instead of I usually use some managers which has all required methods. (DBManager, NetworkManager, etc)

How do I share common methods between view controllers in Objective-C?

In my app there are numerous view controllers which have their own purposes. However, underneath they do share some need for common maintenance work that could use the same method code instead of each having its own copy of the code, literally pasted in.
Is there a way to share the code of these common methods ? I see two possibilities:
1) either one copy of code truly shared in memory as in a special maintenance object of methods
or
2) written once in a block of code but allocated many times as needed by each view.
Which of these is the correct path or what is the correct path, and HOW would it be implemented in the most simple manner ?
Kindness pls, new coder in the room.
Thanks.
-Ric
Note: This is an old question/answer reflective of Apple practices at the time, and answered for a new coder looking for a simple solution they can understand (as requested in the question). There are better and more testable ways to achieve this, using composition.
The best way to achieve what you want is to create a common parent class for your view controllers. So instead of inheriting directly from UIViewController, each of your custom classes will inherit from SomeFeatureViewController (where SomeFeature describes the common feature provided), this class inherits from UIViewController. Now, each of your actual view controllers will inherit from SomeFeatureViewController and any common methods (also any common instance variables used by these methods) can be placed in this parent class.
#interface SomeFeatureViewController : UIViewController
{
int common_iVars;
}
- (void)commonMethods;
#end
#interface ActualViewController : SomeFeatureViewController
{
int specific_iVars;
}
- (void)specificMethods;
#end
The way I see it you should do a class with all the common methods and then subclass it. Say you have MyCommonViewController : UIViewController and then for each different view controller do MySpecificVIewController : MyCommonViewController
As well stated by jhabbott, a subclass of UIViewController is one good solution.
If that's not an option (e.g. you can't change the class hierarchy), another option is to create a category on UIViewController to add the methods and properties you need. This will make your methods available on every UIViewController, including all the standard subclasses, without any extra work on your part. With this solution you cannot directly add ivars, although you can fake it up well enough using associative references.
Well, what I ended up doing was to create a Singleton object and put all my common methods there. This meant that all my View Controllers could utilize the common methods of the Singleton. Maybe this is a bit too open if the project was being developed by a team of programmers but as it is just me I know that all the controllers have similar requirements in processing. So the Singleton approach seemed to work.
It also fit the project because at the time of the question all the View Controllers had been created and to make them a subclass of a parent class seemed to be a retrofit, though I agree that if part of an initial design, it may be a better approach. I do thank the 3 who gave the time to answer.

Object and view setup and initialization in objective-C

When creating a UIViewController derived class in objective-C, what goes into the init method, what should go into loadView and what into viewDidLoad - and more importantly why, and what benefit (performance?) does this have?
Also, how does this relate to UIView derived classes where the only option you have is the init method?
I know the template code already has comments for what goes into each method, but it unclear to me why each thing goes where they say.
Clarification
I would like to know maybe at a lower level, what is the actual difference between things being done in the 'init', 'loadView' and 'viewDidLoad'. What does the framework do in between these calls that may affect the way/time I set up my views and do other work? How are these methods affected by threading?
You want to know some lower-level stuff.
init: This method gets called on ANY NSObject subclass. It is what sets up the object, which you probably already know. In many model (as in the MVC pattern) classes, init is directly used. As for the UIKit classes, very few requires init to be called directly. It should not be used. In the UIViewController, you initialize it using initWithNibNamed:. You can override this method, but in most cases this is not needed. This method is the VERY first method to EVER get called on the class (before any view setup, or such).
loadView:and viewDidLoad: read this article iPhone SDK: what is the difference between loadView and viewDidLoad? .
The only really important thing to know is that -init is the NSObject standard initialization method. -loadView and -viewDidLoad are UIViewController's methods for initialization.