How to call a viewcontroller in other class using KGModal - ios

I'm working on an app. The app is in the swift. Now the problem is I'm trying to represent a view controller from another view using the KGModel.
Lets say that are two view controller ViewA and ViewB. Now if i set the viewB as the initial viewcontroller then it's working fine. All the delegate of the tableview controller called successfully but if try to call the ViewB from viewA then the table view always return empty. Is any one know how to call the other view controller in the current view using KGmodel. Thanks in advance.
Code To Call ViewB from ViewA
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "MoveToReviewView") as? ReviewViewController
controller?.assignValue = self
presentController(controller!)
func presentController(_ controller: UIViewController) {
let ret = self.view.frame
controller.view.frame.size.width = ret.size.width - ret.size.width/4
controller.view.frame.size.height = ret.size.height/2
KGModal.sharedInstance().show(withContentView: controller.view, andAnimated: true)
}
The output is

Related

Pass Values from Popup VC to controller

Currently in my App I am opening a popup View Controller inside a normal View Controller, and need to pass values back from my Popup VC to the normal VC.
This is how I am making the popups.
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "IconEditPopup") as! IconEditPopup
// this is where you can set values in the view
vc.id = "12"
self.addChild(vc)
vc.view.frame = self.view.frame
self.view.addSubview(vc.view)
vc.didMove(toParent: self)
Basically just want to pass values back from the popup View controller to my normal VC.
You can use Delegate pattern or Clouser callback handler to pass a value back to the parent view.
here is a example:
Define a clouser in your popVC like this:
var clouserName: ((returnType) -> Void)?
Inside your popVC where you need to call the clouser:
clouserName?(returnValue)
in your parent controller, capture the value in this way:
vc.clouserName = { returnValue in // dont forget [weak self] if you need self
// Do your stuff here
}

Is there a way to connect or access my `scrollView`from my main class to another class?

in my MainViewController I have a scrollView and add a ChildView in it which works fine. In this example I instantiate my PlansViewController2and add it as a ChildViewto my scrollView.
After selecting a Cellit pushes a new controller. But I want to add the new Controller as a ChildView to my `scrollView.
#IBAction func planAction(_ sender: UIButton) {
let newPlan = sender.frame.origin.x
scrollView.subviews.forEach{$0.removeFromSuperview()}
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let nextController = storyBoard.instantiateViewController(withIdentifier: "PlansViewController2") as! PlansViewController2
addChildView(viewController: nextController, in: scrollView)
self.movingView.frame.origin.x = newPlan
}
Im my PlansViewController2 I push another ViewControllerbut I want to add it also in my scrollView from the previous controller.
let storyBoard = UIStoryboard(name:"Main", bundle:nil)
let planViewCtrl = storyBoard.instantiateViewController(withIdentifier: "PlanViewCtrl") as! PlanViewController
planViewCtrl.navigationItem.title = plan.title
self.show(planViewCtrl, sender: self)
_ = planViewCtrl.view // force view loading, so we can set it up properly
_ = self.updateLastUsed(plan: plan)
planViewCtrl.plan = plan
Is it possible to add - in my case here - the PlanViewController into my scrollViewfrom my MainViewController?
When you add view's PlansViewController2 to scrollview. Please add PlansViewController2 to childViewController of VC has scrollView(self.addChild(nextController)).
When you want to add PlanViewController to scrollView:
Step 1: First you need to get viewcontroller that have scrollView using parent property (self.parent).
Step 2: Final, You have access to scrollView instance though vc you get in step 1.
Good luck!

How to initialize a View Controllers outlets inside of a Second View Controller?

How would I access a second view controllers IBOutlets from with in a second view controller? In my situation I initialize a current view controller with a statement like this.
let eqController = EqualizerViewController()
But although this statement works, when referencing an outlet from eqController, it succeeds at build time but fails at run time because he outlets are nil. How would I initialize those outlets from within the current view controller? Thanks in advance.
You can't access outlets because they are nil until the Vc loads , so inside the secondVC
class SecondVc:UIViewController {
var sendedStr = ""
}
//
let vc = storyboard.instantiateViewController(withIdentifier: "secondId") as! SecondVc
vc.sendedStr = ""
// here present / push
If you're trying to initialize a view controller to present, you would want to use the storyboard instead of doing the let eqController = EqualizerViewController().
Try this instead:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let eqController = storyboard.instantiateViewController(withIdentifier: "eqController") as? EqualizerViewController
But you will also need to update the view controller's identifier in the storyboard, as shown below:

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)

Resources