how to link storyboard outlets and actions to custom category methods? - ios

I am writing a simple app with custom storyboards for iPhone and iPad. I have a generic viewcontroller class from which my custom iPhone and iPad viewcontrollers inherit. Outlets and IBActions in my storyboards are mostly wired up to the generic viewController class and everything works well.
Now for stylistic reasons I decided to make my iPhone's viewcontroller be a UITableViewController and it can no longer inherit from my generic viewcontroller. I've been googling and searching this site and see advice which says I should write a category and use this in my custom classes.
I have never done this, but I looked at the documentation and understand the concept, but once I move my code into a category, how would I be able to link my storyboard's outlets and actions (ctrl-drag from storyboard) to the methods which are now moved out of my custom viewcontroller classes? Usually I would for example ctrl-drag from a switch in the storyboard to an existing IBAction method in my generic viewController, but these methods will now be inherited and not showing my my custom classes to drag to.
I am just a hobbyist and newb, so I apologize if this question is too basic.

You can use UIViewController with UITableView instead of UITableViewController.
1.In storyboard, get a new UIViewController and linking with your custom viewcontroller class.After that,put an UITableView into that UIViewController and linking with that class using (ctrl-drag from storyboard) called mTableView
2.In that class you should implement <UITabBarControllerDelegate,UITableViewDataSource>.
Use mTableView instead of self.tableView just like in UITableViewController.

Related

How UIViewController interact with Storyboard under the hood

I'm new to iOS development and this could be a stupid question for some experienced guys...
When I create a new iOS project in Xcode, I get a ViewController class and a storyboard which sets its Custom Class to this ViewController. It looks like there is a "Storyboard" class holding an instance of ViewController, however, I cannot find where this "Storyboard" class is defined.
Even though I know how to create multiple subclasses of UIViewController to handle different views interaction following some tutorials, I still find it uncomfortable to associate these subclasses to the storyboard by selecting them in the storyboard panel. I would rather see something like a "Storyboard" class holding an array of UIViewController.
So my question is, how these UIViewController interact with the Storyboard under the hood?
Thanks
Roughly, it happens as follows:
App launches.
App loads storryboard.
Depending on app's navigation structure, app instantiates each view controller inside the storyboard as needed.
The storyboard contains detailed information on:
Which custom subclass of UIViewController, UINavigtionController, etc. to use for instantiating each view controller in the storyboard.
How to map (connect) each if its view controllers' subviews to the corresponding custom classes' outlets and actions.
But seriously, read Apple's docs. It's all there.
UIViewController has a property named storyboard which refers to the storyboard file associated with the viewcontroller subclass.
Also the view controller code are interecting with the storyboard with connections symboled with #IBOutlet and #IBAction.

Is it possible to subclass UITableViewController with xib or storyboard?

It is easy to subclass UITableView. Also my UITableViewController can inherit BaseTableViewController programmatically instead of xib. But the requirement is using xib or sb to subclass. Is it possible? I have tried to drag a UITableViewController in a xib. And Xcode remind me the customization only can be done in storyboard.But in storyboard , children controllerA is connected to its parent controller. And children controllerA inherit from baseController. BaseController needn't to connect anything with segue. Am I right?
Static table view cells are only available when using storyboards. Take a look at this answer: How to use static cells in UITableView without using Storyboards?

Connecting Objects with two View controllers in Swift

In my project I have two view controllers, and I am having trouble connecting objects such as an UIImageView to the view controller. When I try to create the IBOutlet, it tells me that "Could not insert new outlet collection: could not find any information for the class named UIViewController". I believe this problem stems from the fact that my original declaration of my class is as follows:
class UIViewController: UIViewController {
when in fact the view controller is named mainScene instead. However, when I change the first UIViewController to what I think it should be (mainScene), it doesn't even show me the option of connecting an IBOutlet...
class mainScene: UIViewController {
So, I have two questions.
Do I need to have a whole separate class for the second UIViewController and would that solve my issues?
Is there a better way to link objects to the UIViewController or am I just doing something horribly wrong (the likely scenario)?
Thanks so much
Short answer: 1. Yes, and yes. 2. There's no better way, and you're not doing something horribly wrong. (You probably just missed a step.)
You have two view controllers. Assuming they are different, you would subclass each one from UIViewController with a different name. E.g., mainSceneViewController and otherSceneViewController.
Your mainSceneViewController and otherSceneViewController would each have their own properties and IBOutlets.
Where you're probably stuck, is needing to change the class of your viewController within Interface Builder to match the class name in its .swift file, so IB knows what outlets it can connect for that view controller.
Each scene in your storyboard corresponds to a view controller. When the segue is performed, iOS instantiates your view controller from the storyboard.
While it is possible to only have one view controller subclass, and use the same subclass for different views, it doesn't happen too often.
Update:
Subclassing lets you add properties and methods to a class, and override their superclass.
In your comment, UIViewController is the class, and mainSceneViewController is subclassed from UIViewController. For your second view controller, it would likely be class otherSceneViewController: UIViewController {, as your other scene would likely require a different properties and methods from your main scene view controller.
You can read more about Inheritance in the Swift Programming Language guide.

Adding Outlets to View Controller via Table View Controller

I'm attempting to create an application for iOS, using Xamarin and storyboards. Within this, there shall be a button located in a TableViewController which will navigate the user.
To do this, I've used a Container, embedded to a TableViewController. However, my concern is I wish to add an Outlet from the button within the TableViewController to the ViewController header class - Yet it only allows me to try and add one to the 'UITableViewController.h' file.
What is causing this and how am I able to add the Outlet to the 'ViewController.h' file? (Extremely sorry if this makes little sense)
You should not use both the UITableViewController and the UIViewController. UITableViewController is a subclass of UITableViewController so all you need the UITableViewController and you should drag the outlet to it.

What superclass for a custom UI component?

I need to create a custom UI component that will satisfy the following:
it will contain a composition of UIViews, Labels, Button and a TableView.
it should be possible to instantiate it programatically - or - placing a UIView in nib editor and change the class in the inspector to my custom component class. (and it would the maintain/keep the main frame values from placed UIView)
I already started experimenting with it, I chose the UIView but it doesn't handle the ..cellForRow... cell method. On the other hand, if I use a UIViewController, then my class change in the inspector doesn't work and the app crashes.
Which one should I choose - UIView, UIviewController or NSObject?
I would go for UIViewController or UITableViewController, since you said you are having a tableView in it. If you do a UIViewController, you would probably have to include another UITableViewController inside it, using addChildViewController, as explained here
I believe the reason why your app is crashing is that you you changed your class superclass from UIView to UIViewController, but in the Interface Builder you still just have a UIView object. You need to change it to a UIViewController in the Interface Builder, and select your class as the File Owner.
If you intend this to be a stand-alone component, with all of the logic contained within the control, then I would base it on a UIViewController, like Apple does with the MFMailComposeViewController. If you want it to be strictly a view, with no built-in logic of its own, but instead delegates that logic, then use a UIView and delegate the table view methods to the controller that is using it.

Resources