Inheriting ViewController class into another ViewController class - ios

I wanted to use a library however it required me to change MyTabBarViewController class to its own class (its own TabBarController - 138) installed using cocoapod. I couldn't figure out how to do it (or if it's possible) but that would be nice for me to be able to use MyTabBarViewController as well so I can have my stuff separated, such as viewDidLoad and my functions.
I was using MyTabBarViewController instead of this but the library requires me to change it to this.
Is there a way to make my TabBarVC getting called with a line inside the library's TabBarVC? Or should I write inside the library's TabViewController?

Related

Calling functions from separate view controllers in swift

I think the solution to this is going to need to use delegation, but I'm unfamiliar with how to use them.
So in my project, I have my main viewcontroller/storyboard that contains a UIScrollView. That UIScrollview calls another storyboard (xib file) as a subview. The other storyboard (which is an xib file) is controlled with another swift file.
My question is, when I call an action inside of my other storyboard, how can I call a function from the main viewcontroller. Like say the viewdidload from the first viewcontroller.
I can't make the whole thing a global function, it needs to stay inside its class. So if I try to do ViewController.viewDidLoad() it needs (I think) an instance variable or something.
Thanks.
You can try:
Using weak variable (property) in the other class with type UIViewController
Assign the parent view controller to that property after the other view is initialized
Good reads about weak, strong, unowned references Here And Here
Firstly, if you want to call it with class name as you said above declare your method with "class". So its just like static in Java. It makes it generic to call it anywhere in your project. Make a separate extension.
class func myfunc(){
}
if you want to send data from B to A controller. You use what is called delegation. You give the work of B to A. Make a protocol above B for functions that you want to do or send with them. Call them in B. And then in A write code for those functions. So that you have the data from B to A
Else you demand something like common data. Create a singleton class and initialize properties methods there. You can use objects for that and call it in other controller to modify or make different instances.
You dont call viewDidLoad(). As the name says it loads once. If you want something that modify everytime you screen appears, use viewWillAppear

Xcode 7 Swift 2 impossible to instantiate UIViewController subclass of generic UITableViewController

I have a generic class:
class PaginatedTableViewController
<GenericElement, Source: PaginationDataSource
where Source.PaginatedGenericElement == GenericElement>:
UITableViewController
and another that I try to instantiate from storyboard:
class CandidatesTableViewController:
PaginatedTableViewController<Match, MatchPaginationDataSource>
I can't find CandidatesTableViewController in the storyboard Custom Class dropdown menu. If I force it then cast my controller in code, app crashes at runtime complaining my controller (that should be a CandidatesTableViewController instance) is in fact a UITableViewController instance.
Unknown class _TtC21MyProjectName29CandidatesTableViewController in
Interface Builder file.
Could not cast value of type
'UITableViewController' (0x1040917f8) to
'MyProjectName.CandidatesTableViewController' (0x1013a9890).
In my project this controller is embedded in another one that's why I cast it :
tableViewController = (segue.destinationViewController as! CandidatesTableViewController)
Does any one knows how to resolve this issue ?
Unfortunately, generic Swift classes are not visible to Objective-C code and also are not supported in Interface Builder (in storyboards and xibs). I find these two points closely related.
As a solution I would suggest you to use aggregation: do not make you view controller generic, but extract some logic to another (generic) class and use it inside your view controller.
It is possible if you manually load your generic VC into Objective-C runtime manually via the load() method i.e. call. PaginatedTableViewController.load() in your app delegate's init method. Idea from https://stackoverflow.com/a/43896830/671580

Cannot select Custom Class for Table View Controller (UITableViewController)

Let me preface this by saying that I've already tried quitting and restarting Xcode, as well as deleting derived data for the project, to no avail. I'm at wits end right now.
Basically, I've created a custom ViewController class I want to use, but I cannot select it. In fact, using the Custom Class dropdown, I can only select the default UITableViewController class for my controller. The weird part is, it looks like the full list appears for a split second sometimes, before being replaced by the incorrect list with a single option. Here's what it looks like:
I would really appreciate any ideas here. I'm not really sure what I could have done wrong, since I was following this Apple tutorial pretty much to the T.
For another scene in my project, which is just a plain view controller, I can select custom classes with no problems.
Ideas?
You have to inherit from UITableViewController class so basically the interface file (.h) should looks like that:
#interface YOURCLASSNAME : UITableViewController
Make sure you replace YOURCLASSNAME with your class name and you add UITableViewController and that should do the job.
Make sure the swift file is defined as a UITableViewController and not as a UIViewController.

Objective-C Modifying Class Instance Superclass

I was wondering if there was a way of dynamically taking an Instance of a class, so lets say I have a UIViewController called menu.
I could take menu's superclass which in this case would be UIViewController and create a subclass of it. I would then assign this new subclass to menu, I could also then dynamically override the methods as well.
So that when menu calls a method such as "ButtonClicked:" my code in the new Class I created fires followed by the original code when I call super :).
This all has to be done at runtime for security reasons.
Runtime subclassing is totally possible. Here's an introduction: http://www.mikeash.com/pyblog/friday-qa-2010-11-19-creating-classes-at-runtime-for-fun-and-profit.html
Although I'm curious... what "security" do you think you're getting by subclassing at runtime?

Can I create a custom nib for UnknownPersonViewController without using an "undocumented api"?

I'd like to add some custom buttons to an ABUnknownPersonView. Can I use initWithNibName:bundle: in my ABUnknownPersonViewController to load a custom view that I've created in IB, while not using an "undocumented api?" And if I do, how can I make sure that it follows all the properties and responds to all the hooks that the controller expects?
I typically create all my views programmatically and I generally like the view that ABUnknownPersonViewController creates. I'd rather just start from there. And so I accessed the view and dropped in a button, but with later versions of iOS that broke since that isn't a hook that Apple created.
initWithNibName is also not mentioned in the ABUnknownPersonViewController documentation but it is a method of it's parent class UIViewController. Does that make it safe to use?
You cannot provide a replacement XIB. While I understand you want to avoid it, you will have to use the underlying AddressBook framework and building your own version out of it.
You can, of course, look at open source stuff such as this for a good starting point.

Resources