Accessing UITabBarController variable in Swift - ios

I want to be able to subclass UITabBarController so I can add a variable and access it from the view controllers that it manages. Is this possible or should I move the var to a separate class/ app delegate? Thanks!

Yes definitely possible. For example, I'll use the class name CustomTabBarController so create a new Swift file using the template iOS / Cocoa Touch Class named CustomTabBarController.swift and in the new file dialog have it subclass UITabBarController.
You now have your custom class that inherits UITabBarController. You can add your properties and methods.
Now in your Storyboard add a UITabBarController (or select your existing one). In the inspector pane, click the Identity tab and set the custom class to CustomTabBarController.
Now to access your property or method in a ViewController that's in your CustomTabBarController, you could use something like this within that ViewController:
if let customTabBarController = self.tabBarController as? CustomTabBarController {
print(customTabBarController.variableName)
}

Related

What is the relationship between the view controller on the storyboard and view controller I define through code?

When I create a view controller in the Interface Builder, I associate it with my code version of the class through selecting the appropriate name from the Identity Inspector. Is the view controller from IB a subclass of the class I coded?
As far as I can tell the view controller in IB is not an instance because you still have to instantiate it:
if let vc = storyboard?.instantiateViewController(identifier: "Detail") as? DetailViewController {
// use vc
}
I don't think it's a property of the code version of the view controller, DetailViewController in the above example. It's being instantiated through storyboard, which in turn is from UIStoryBoard.
Think of the storyboard as a resource file. instantiateViewController just reads that file and creates a certain UIViewController subclass, by calling its init(coder:) initialiser. After that, it creates all the views found on the storyboard, and adds it into the VC's view. How does instantiateViewController know which UIViewController subclass to create? The subclass's name is actually stored in the storyboard, exactly when you type "DetailViewController" in the identity inspector!
The View Controller you see in IB is only as much of an instance as this JSON...
{
"username": "Sweeper",
"id": 5133585
}
is an instance of this struct:
struct User {
let username: String
let id: Int
}
It's not a subclass of DetailViewController either. It's just data in a resource file.
A storyboard is a collection of scenes / vcs related to each others by a segue if exists , when you create a vc you have the option to create it completely programmatically in terms of it's layout or create it's layout inside a storyboard and then assign the vc name in identity inspector so that you use it to create instance of that vc with instantiateViewController which is linked to the layout specified in storyboard there is no super-subclass relation. the code in vc acts as the series of the vc's life cycle . think of the sotyboard part as an easy way to add the layout components like label / button with constraints to the vc's view instead of creating them programmatically that is the main difference
A view controller in the Interface Builder (IB) is not a subclass of the class you define in your .swift file. The storyboard really just helps you visually define the layout and constraints of the subviews that a UIViewController controls.
Basically what your code snippet is doing is "find the storyboard object with 'Detail' as its identifier, make sure its companion class is of type DetailViewController, and then create an instance of DetailViewController with the layout and constraints that are defined in that storyboard object".

Subclass from the subclass of UIViewController with Storyboard doesn't work as except

I have two view controllers, one subclass UIViewController
class MGStatusViewController: UIViewController
with storyboard identifier "MGStatusViewController".(With some UIButtons)
And the other subclass **MGStatusViewController**
class MGStatusDetailViewController: MGStatusViewController
with storyboard identifier MGStatusDetailViewController
When i use it like this:
self.storyboard?.instantiateViewControllerWithIdentifier("MGStatusDetailViewController") as? MGStatusDetailViewController
There's no controllers(the UIButtons) from MGStatusViewController's storyboad file.
How should i achieve this? Thanks!
When you inherit your custom view controller - in this example MGStatusViewController you inherit code behind and logic. You can connect your outlets for MGStatusDetailViewController with MGStatusViewController. And that is it. You can't inherit UIView controls from the storyboard and expect them to show.
What you can do is to create MGStatusDetailViewController take the view from it and add it to the MGStatusViewController. Or, you can create embed control and embed MGStatusDetailViewController in MGStatusViewController. This way you will have all the controls from storyboard and will present your MGStatusDetailViewController
In short, inheritance does not take Views from storyboard, only code from that view controller.
Furthermore, when you inherit from view controllers, you use them as an abstract. You create some logic for that will be used on others view controllers, but you don't create actual UI.
Why you use "MGStatusDetailViewController" story board identifier if you have set identifier as "MGStatusViewController"
Storybord ID and class are diferent thing.
You may try with this -
self.storyboard?.instantiateViewControllerWithIdentifier("MGStatusViewController") as? MGStatusDetailViewController

Can't use the drag function in .h when using storyboard with more than one view controller

I can't use the control drag function to hook up a button to action in .h when using storyboard with more than one view controller
Please help me...
Are you sure you've assigned the view controller a custom class? and dragging to that specific class? Each view controller should have its own Obj-c class. To create one:
press cmd + n and create a new Objective-C class and make it a subclass of UITableViewController (or the view controller you used in your Storyboard). after doing so, go to you Storyboard and select the view controller, on the right menu choose "Identity Inspector" and set the custom class name to the name of the class you just created.
Set the class file name for the UIViewController of the storyboard file at the identity inspector .

iOS : Load uiview of another view controller

I have a UIViewcontroller on which I am showing UIview which contains a registration form.
now I need this same registration form on another UIViewcontroller without recreating the same for second UIViewcontroller.
I am using UInavigationcontroller.
Is there any way to achieve this? I am stuck since last few hours and tried everything.
First method & the one that I will prefer is to create the registration form as a separate UIView class.
Then you can use it in various view controllers as subview.
Second method, you can create a parent class & inherit both the classes from this parent class. This parent class will contain all the common functionalities.(as mentioned by #alinoz).
You can add viewController in another view controller. Create instance of registretionViewController and add its view in current viewcontroller vew. [self.view addSubview:regViewController.view]. And if you want to push this you can simple create instance of it and push in navigation stack.
Let the class of your registration form view be RegisterFormView.
In appDelegate create a class public property of RegisterFormView.
#property (nonatomic,strong) RegisterFormView *formView;
When you first create form view in viewController A, assign the same in appDelegate also.
[UIApplication sharedApplication].delegate.formView = registrationFormViewInstance;
In another view controller(B) where you want to use the same formView,
if([UIApplication sharedApplication].delegate.formView != nil){
//use the form view
}

Can't find NewViewController subclass in Storyboard view

I want to change a TableViewController to a new UIViewController (PlayersViewController) from a UIViewController subclass, so I added two files (.h, .m), and then went back to the Identity Inspector/Custom Class.
Expected to find the PlayersViewController in the pulldown menu in Custom Class but could not find it, so I typed it in and hit "Return", but that didn't work either.
Am I doing something wrong?
Your new class should inherit from UITableViewController, not UIViewController. Only subclasses of the base class are available to select in the custom class field.

Resources