What "UIViewController" class really does? - swift

I was working in the ViewController.swift and noticed that when I open it, it declares a UIViewController class. I was wondering if that is a new class or an already declared class that we can add code to.

I was wondering if that is a new class or an already declared class that we can add code to.
UIViewController is a class defined in the UIKit framework. It's the class that's used as the base class for all other view controllers in iOS applications, so it provides the behaviors that are expected of any view controller as well as a number of methods that are meant to be overridden. The usual way to "add code" is to create your own subclass of UIViewController, which is exactly what you're doing in the ViewController.swift file.
You're going to run across many classes that have the UI prefix, and you can look up any of them in the documentation that comes with Xcode.

UIViewController is not the class that you can add code to. But ViewController is default class that has UIViewController as a parent.
ViewController class is already connected with storyboard view controller when you create your project by default. So you can add some code to ViewController class and connect your UI controls (buttons, labels and so on) with you ViewController class and add some functionality to it.
So yes, you can add some code to it and see what happens when you build and run the simulator.

Its a class that manages view controller functions.
For example, "YourClass:UIViewController" means that You extend from UIViewControllerClass, so You are able to use all the functions from mother class, but also add new in "YourClass".

Related

Why doesn't Interface Builder see my class in the static framework?

There is a Cocoa framework project called MyFramework with Mach-O Type set to Static Library that features MyView.swift with a following declaration:
open class MyView: UIView { /* ... */ }
In the meantime, another project called MyApp statically links against the static framework MyFramework. Additionally, Main.storyboard of MyApp has a scene in which there is a view, whose custom class is set to MyView and module is set to MyFramework.
Whenever the view controller that backs this scene is instantiated, the view behaves as a default UIView and I see the message in console:
2018-06-23 18:22:29.114096+0700 MyApp[2318:415810] Unknown class _TtC15MyFramework15MyView in Interface Builder file.
Then, I add to MyApp's target a MyNewView.swift with the following declaration:
class MyNewView: MyView { }
Without changing anything in the Main.storyboard, I launch the app, the MyView class gets instantiated, it behaves as supposed and the message doesn't appear.
Question #1: why does this happen? Is the type stripped or not registered with the Objective-C runtime if it's not directly imported or referenced from anywhere in code? Or maybe is it somehow lazily loaded?
Question #2: what can I do with it so that I don't need to apply such workarounds?
Update: I restarted the Xcode and the issue did not persist, yet still, the interface builder doesn't suggest MyFramework as a module while typing in the Module field in view's identity properties, and the #IBInspectables of MyView don't get parsed, don't show up in the interface builder, in the view's attributes.

Added class unable to be selected for a new XIB

I added a new class to solution (iOS), together with XIB and its designer.cs.
Problem is I cannot select this newly created class (NewAccountView.cs) in the XIB NewAccountView.xib properties (see the drop down list does not include my added class NewAccountView.cs.)
I am new to Xamarin and I am sure I am missing a crucial step here. Pls help.
Did you use the View or the View Controller template?
If you used the View template, then only the .xib is created and you would have had to create the View class separately, in which case it probably did not automatically add the class inheritance and the Register attribute to your class. Make sure your NewAccountView inherits from UIView (or some subclass of UIView as appropriate) and also that you register it. E.g.:
using Foundation;
using UIKit;
...
[Register("NewAccountView")]
public class NewAccountView : UIView
{
...
}

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

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.

Using a ViewController with a dot in its name through storyboards

Is it possible to reference a UIViewController with a dot in its name through a storyboard?
Login.ViewController for example?
class Login {
class ViewController: UIViewController {}
}
Interface Builder is not aware of nested classes that are available in Swift, so you cannot set a nested view controller class as the class for a view controller scene in a storyboard.
Apple may address this in the future or you could consider posting a feature request

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)