Create base class of UIViewController with tableview and buttons - ios

I want to create base class of UIViewController with tableview for my App sharing functionality between two almost similar ViewControllers.
When i Extend UIViewController with tableview did the child class get access to all views of its superViewController and all the (IBAction)undoAction:(id) sender methods?

Yes that should work. If you subclass a UIViewController (or any other NSObject) you will be able to access all of its (public) methods. You can create a public method by defining them in the header file and adding an implementation of that method in the implementation file. These files will both be created while creating a cocoa (touch) class. The same thing applies to properties, the one you define in the header file will be accessible.
Let me know if you have any further questions

Related

Cannot assign Custom Class to UITableView in IB

I have a custom class that derives from (a base class that derives from) UIViewController and UITableViewDelegate that I want to assign to my table view.
I know it's assignable, because I've used this pattern elsewhere in my project, but entering the name in Custom Class doesn't work (it get's erased when I hit Enter). Any ideas?
I don't know why it works, but if I temporarily make my class derive from UITableViewController instead of my BaseViewController, I can assign the custom class in interface builder. Then, I can switch back to deriving from my BaseViewController and everything will still function correctly.
Could be a separate question, but any idea why this works?

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.

Common base class for ios view controllers using storyboards

I'm trying to use a base class MyBaseViewController that's a subclass of UIViewController for every view controller that I'll use in my app, so that I could have some things set up in that base controllers for convenience.
I created a storyboard with a UITableViewController object in it, and I'm writing a custom controller class MyTableViewController for it.
I wanted MyTableViewController to subclass MyBaseViewController and implement UITableViewDelegate and UITableViewDataSource for it to work properly with a table view, but when I tried to set the class for the UITableViewController object in my storyboard, it doesn't allow me to select the custom class I wrote.
I assume this is because the storyboard wants me to use a class that's a sub class of UITableViewController?
Is there a way to get this to work without changing MyTableViewController's inheritance?
Unfortunately not. Objective C doesn't support multiple inheritance.
I don't know what kind of defaults you're setting up in MyBaseViewController but if you moved them to an extension of UIViewController you could then have MyTableViewController and MyBaseViewController both call the same extension methods.
In a word, no. UITableViewController's ancestry is already defined. It inherits directly from UIViewController. You can't insert a new ancestor after the fact.
Objective-C doesn't have multiple inheritance, which would make this easy.
You could define your own subclass of UIViewController that implements the table view delegate and data source methods, and use that instead of a UITableViewController as the base class for your table view, but some things won't work unless the host of your table view is a UITableViewController.
If you don't need to add instance variables to your view controllers or override existing methods you might be able to define a category of UIController (UIViewController+MyMethods).
You might also create a set of methods that you add to both a custom subclass of UIViewController and UITableViewController. If you put your extra source code into a separate source file you could #include that source file in the body of your MyViewController.m. and your MyTableViewController.m files:
You might have a file ExtraVCMethods.m:
- (void) methodOne
{
//code
}
- (void) methodTwo
{
//code
}
And then in your MyViewController.m:
#implementation MyViewController
#include "ExtraVCMethods.m"
#end
And in your MyTableViewController.m:
#implementation MyTableViewController
#include "ExtraVCMethods.m"
#end
(Note that I am #including a .m file, not a .h file, and the #include goes inside the implementation, which is unusual.)
The #include would cause the compiler to insert the same source in the body of both classes. That way if you change the source for your extra methods you don't have to change it in 2 places.
Note that with the above approach you turn off the target membership checkbox for your ExtraVCMethods.m file. You don't want that compiled separately - just pasted into your other source files by the precompiler.

Reuse identical delegate methods in multiple controllers?

I have a UITextFieldDelegate method shouldChangeCharactersInRange that is exactly the same across several view controllers, so I had to just copy-paste it into every view controller that conforms to the UITextFieldDelegate. I like sticking to DRY, and here I have identical code in lots of view controllers. Is it possible to reuse delegate methods and what are good ways to do it, or this is the only/optimal way and why?
Create a sub class of UIViewController which implements just the delegate methods you wish. Set this as the parent class of all the view controllers in your project which want the delegate methods to be used.
Instead of that, you can also try looking into Categories. Create a category on UIViewController named whatever you wish. Add the delegate methods you wish to implement to this category. Now, all view controllers in which you #import this category will implement the delegate methods.
You can make a parent object class from which all your view controllers inherit. Place the delegate method code in the parent object implementation.
You can create an object that conforms to UITextFieldDelegate and inherits from NSObject.
Then, on each view controller you can create an instance of this object, storing it on a property and assigning your UITextField's delegate to this object.

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