Adding a dependencies to a child view controller - ios

I want to present a custom view controller within a navigation controller within modal view. To do this I made a view in interface builder, added a container view, and embedded the contained view in a navigation controller. The contained view is my custom view controller, DetailViewController.
I have to add dependencies to the DetailViewController object to at runtime. Here’s the method where I’ll be presenting the DetailViewController:
override func tableView(_: UITableView, didSelectRowAt: IndexPath) {
guard let record = records[didSelectRowAt.row] else { return }
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let containerView = storyBoard.instantiateViewController(withIdentifier: "DetailContainer")
containerView.modalPresentationStyle = .overFullScreen
self.present(detailContainer, animated: true, completion: nil)
}
How can I add the record to DetailViewController? I tried to access children of the container view, but the array is empty. This Apple document says
The children must be instantiated at the same time as the parent so that the appropriate parent-child relationships can be created.
I'm not sure how to achieve that.

containerView.children is empty because viewcontroller's view isn't loaded.
You can force to load view by calling containerView.view thereafter containerView.children will exist.

Related

iOS TabBarController shows only black screen

I have created a TabBarController with 4 UIViewControllers. However, when I want to show one of the ViewControllers in the TabBarController with:
func gotoMainAppVC()
{
let mainVC:SearchVC = SearchVC()
self.present(mainVC, animated: true, completion: nil)
}
I only get a plain black screen. What am I doing wrong?
This is not how any of this works.
I am not sure what self is in your context but I expect it is either a tab bar controller or one of the view controllers on the tab bar.
By calling present it means you want to present a view controller on a current one which by default brings a new fullscreen view controller from bottom upwards.
Since you have created a new instance of view controller by calling SearchVC() this is not the view controller that you see in your storyboard. You may create any instances of any of your view controllers but in your case if you designed SearchVC in a storyboard you want to actually use that version of it. To do so you need to set storyboard id inside your storyboard. After you have done that you should initialise it as using:
UIStoryboard(name: <#T##String#>, bundle: nil).instantiateViewController(withIdentifier: <#T##String#>)
as in
let mainVC:SearchVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TheIDIHaveSet") as! SearchVC
But again this will not give you the view controller that is inside your tab bar but create a new instance. And it will not instert it as one of the tabs but present a new full screen view controller.

Switching containerView content from within viewController

I have three viewControllers Main, A, B. Main ViewController holds ContainerView and other content as well does all transactions in containerView. ViewControllerA has ButtonA when pressing it content of container has to change to ViewControllerB
how can I do that? I cannot find any similar examples.
You will need to create create delegate for that.
First create a protocol
protocol ViewControllerADelagate {
func didPressOnButtonA()
}
In ViewControllerA add following delegate variable
class ViewControllerA {
....
var delegate : ViewControllerADelagate?
....
}
In ViewControllerA add following on button press
#IBAction buttonAPressed(sender : UIButton) {
self.delegate?.didPressOnButtonA()
}
In MainViewController assign the delegate of ViewControllerA to self
like
vcA.delegate = self
Implement the delegate method in MainViewController like
func didPressOnButtonA {
let storyboard : UIStoryboard = UIStoryboard(name: storyboard, bundle: nil)
let vcB : UIViewController = storyboard.instantiateViewController(withIdentifier: viewControllerIdentifier) as! ViewControllerB
self.addChildViewController(vcB)
self.containerView.addSubview(vcB.view)
vcB.didMove(toParentViewController: self)
vcB.view.frame = CGRect.init(x: 0, y: 0, width: self.containerView.frame.size.width, height: self.containerView.frame.size.height)
}
While click on ButtonA. Post a notification to mainView. There remove viewController A from Container and add View Controller B .
I have created a storybaord with sample. you can download it from here.
You need to change the embed view to a navigation controller and then you can use segue to show second view on button press. also hide/show navigation bar depends on requirement.
I am not fond of using these but you can get the child view controller from parent controller by accessing an array childViewControllers. On view did load you would need to go through all of these and find the one that can be typecast into your view controller type like
childViewControllers.flatMap { $0 as? AViewController }.first.
Now that you found the correct view controller I suggest you to either assign yourself as a delegate
childViewControllers.flatMap { $0 as? AViewController }.first.delegate = self
or simply add a button target
childViewControllers.flatMap { $0 as? AViewController }.first.button.addTarget...
Now this can easily be done if you simply embed the 2 view controllers at the same time (have 2 content views) and hide one or the other depending on which you show. At least this way you can assign connection straight away. When this is not the case then you will need to iterate again when setting a new controller or assign connections where you initialize a new view controller.
In this case it then seems better to turn the system around: When child view controller is loaded rather call
self.delegate = parentViewController as? AViewControllerDelegate
Although this will work it seems wrong that a child view controller will control who its listener is so I advise you to avoid such coding.
I in general use a custom implementation for container view which you could do the same or maybe at least subclass the native one and target the methods so that your code will look something like:
private onBPressed() {
containerView.setViewController(viewController: BViewController(delegate: self), animation: .fade)
}
When you pressing Button A from View A:
#IBAction func BtnAPress(_ sender: Any)
{
//Moving Storyboard
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let MainVC : UIViewController = Storyboard.instantiateViewController(withIdentifier: "ViewControllerB")
self.present(MainVC, animated: true, completion: nil)
}

add subview over UITableView

First of, I am noob to Swift programming.
I am trying to show a custom view over a UITableView.
I am facing two major issues :
a) The subview does not appear correctly firstly. ref image below :
and after few moments actual view is loaded :
b) After the UITableView is scrolled, the view is not coming over the current top scroll position of UITableView
Following is the code to add subview :
let vaAdPopupViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AdPopupWithCrossViewController") as! AdPopupWithCrossViewController
vaAdPopupViewController.adID = adID
vaAdPopupViewController.tableView = self.tableView
if let message = parseJSON["adv_text"] as? String{
vaAdPopupViewController.message = message
}
let navigationBarFrame: CGRect = (self.navigationController?.view.frame)!
let frameSize = CGRect(x: navigationBarFrame.minX, y: navigationBarFrame.minY, width: navigationBarFrame.width, height: (navigationBarFrame.height - (self.navigationController?.navigationBar.frame.size.height)!))
vaAdPopupViewController.view.frame = frameSize
self.tableView.addSubview(vaAdPopupViewController.view)
vaAdPopupViewController.view.tag = 1000
self.addChildViewController(vaAdPopupViewController)
vaAdPopupViewController.didMove(toParentViewController: self)
also in AdPopupWithCrossViewController :
override func viewDidLoad() {
self.view.superview?.layoutIfNeeded()
self.view.layoutIfNeeded()
super.viewDidLoad()
}
Please guide me how to resolve this issue.
Thank You.
You probably want to either create a Present Modally Segue or use present() via code.
You've already created your AdPopupWithCrossViewController. Set its background color to be translucent. Then you can present it like this:
let vaAdPopupViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AdPopupWithCrossViewController") as! ModalOverTableViewController
vaAdPopupViewController.modalPresentationStyle = .overCurrentContext
self.present(vaAdPopupViewController, animated: true, completion: nil)
Then, your little "X" button can remove the ad with:
#IBAction func closeTapped(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
You can try out the various options on .modalPresentationStyle and .modalTransitionStyle, as well as setting animated to true or false, to get the appearance / behavior you desire.
As Dan says, you should add your popup as a subview of the view controller's view, not of the table view. A Table view's view hierarchy is private and you should not be trying to mess with it.
Note that you can't do this with a table view controller. This has always bugged me, but it's a fact: A UITableViewController manages a single table view and nothing else. If you want a more complex view hierarchy you need to embed your table view controller in another view controller. I do this with a container view and an embed segue, which is very easy.
If you do that then you can add your new view controller's content to the parent view controller's content view, not to the table view controller's view (or to the table view itself)

viewDidload and viewDidAppear not being called?

I have 3 view controllers and a container view controller. I add those 3 view controllers as child view controllers in the container. When I launch the app I have print statements in viewDidload and viewDidAppear in all the view controllers which get executed.
The problem is: When I "scroll" back on those views and it "appears" again the print statements do not execute nor any code inside viewDidAppear nor viewDidLoad. Why is this happening?
Here is my code where I instantiate my view controllers. Thanks for the help!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
let storyboard = UIStoryboard(name: "Main", bundle: nil)
page1 = storyboard.instantiateViewController(withIdentifier: StoryboardIdentifiers.feedViewController.rawValue) as! FeedViewController
page1.view.translatesAutoresizingMaskIntoConstraints = false
page1.delegate = self
scrollView.addSubview(page1.view)
addChildViewController(page1)
page1.didMove(toParentViewController: self)
page2 = storyboard.instantiateViewController(withIdentifier: StoryboardIdentifiers.favoritesViewController.rawValue) as! FavoritesViewController
page2.view.translatesAutoresizingMaskIntoConstraints = false
page2.delegate = self
scrollView.addSubview(page2.view)
addChildViewController(page2)
page2.didMove(toParentViewController: self)
page3 = storyboard.instantiateViewController(withIdentifier: StoryboardIdentifiers.settingsViewController.rawValue) as! SettingsViewController
page3.view.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(page3.view)
addChildViewController(page3)
page3.didMove(toParentViewController: self)
}
What would trigger those view controllers to cycle through their life cycles?
The parent view controller, whatever it's named will run through it's load cycle. All views will also run through their. (I bet if you subclass a view and, using it, put a breakpoint inside draw(rect:) it would hit it.) But there's nothing to trigger the child view controllers to do their thing.
From what I see, your view hierarchy is:
Main view
Scroll view inside main view
Three page views inside scroll view
When the main view (the afore-mentioned parent view controller) loads, it goes through the usual viewDidLoad, viewWillAppear, viewWillLoadSubviews.... and it's there that all of the above views get loaded, laid out, and drawn.
Nothing is happening to trigger those three page controllers to do any loading.

Load ViewController as child view in main view from other child view

I´m coding an application using swift3/xcode 8. My application have a central view controller, called MenuViewController, which is responsible for the side menu.
Inside this one, i load the other views as child views, so in this way I can use the side menu in all other views.
But I´m in doubt now: In a specific view controller I have a table view, which have a method to perform an action on selecting an item. This action calls other
view and pass a parameter.
So, how do I load this view in the BaseViewController with this parameter? Is it possible?
Please let me know if it is not clear....
Thank you!
Update 1:
I´m using a solution I found interesting:
Cocoacasts
Basically I have this code on MenuViewController:
Declare the view to be loaded in MenuViewController:
lazy var filamentoViewController: FilamentoViewController = {
let storyboard = storyboard.instantiateViewController(withIdentifier: "FilamentoViewController") as! FilamentoViewController
self.addViewControllerAsChildViewController(childViewController: viewController)
return viewController
}()
Adds the viewcontroller as child in MenuViewController:
private fun addViewControllerAsChildViewController(childViewController: UIViewController)
{
addChildViewController(childViewController)
view.addSubView(childViewController.view)
childViewController.view.frame = view.bounds
childViewController.view.autoResizingMask = [.flexibleWidth, .flexibleHeight]
childViewController.didMove(toParentViewController: self)
}
Code in FilamentoViewController which calls other viewcontroller:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
{
let editFilamentoViewController:EditFilamentoViewController = EditFilamentoViewController()
editFilamentoViewController.cod = indexPath.row
self.presentViewController(editFilamentoViewController, animated: true, completion: nil)
}
The code
self.presentViewController(editFilamentoViewController, animated: true, completion: nil)
is the one I want do load into MenuViewController.
Update 2:
MenuViewController is a CentreViewController. I load the other views in the center and the menu comes from left.
There is a built-in controller for your kind of requirement(i.e UISplitViewController) , Have you considered this?
Apple documentation
Sample Tutorial
And there are also slide-out menu libraries.
slide out menu

Resources