Passing object from Parse query to UIContainerView from Parent View - ios

I have the following navigation stack: Navigation Controller -> View Controller -> UIContainer (embedded into the View Controller).
On the viewDidLoad() of the View Controller I am querying Parse to get back a User.
The query returns a var userFound = User(), how can I pass this User object to the UIContainerView?
Thanks

I managed to get this working for anyone that might need it.
With the following:
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("theViewController") as! TheViewController
controller.userDetails = userFound
self.addChildViewController(controller)
self.containerView.addSubview(controller.view)
self.didMoveToParentViewController(controller)
Thanks

Related

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:

How to call a viewcontroller in other class using KGModal

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

Initial view controller and Tab View Controller

I'm working with a single view app but also I'm using Tab View controller at one case , but the Tab view Controller must be put as initial view controller to work so how I can use Tab view controller without make it as initial view Controller?
#SamahAhmed
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let diallerTBC = storyboard.instantiateViewControllerWithIdentifier("tabBarStoryboardId") as! MyTabbarController
//create a variable for e.g : data at MyTabbarController and set like
diallerTBC.data = "testing"
When you are clicking on button to move tabbar controller
Provide a storyboard id to tabbar and use below code
Write this as a class method in appdelegate and call it on button click
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let diallerTBC = storyboard.instantiateViewControllerWithIdentifier("tabBarStoryboardId") as! UITabBarController
self.window?.rootViewController = diallerTBC
self.window?.makeKeyAndVisible()
}
I hope this will work
Thanks

Get ViewController from Appdelegate

I have four views in my storyboard
ViewOne
ViewTwo
ViewThree
ViewFour
In my ViewTwo, I make a call and open an new App and when the URL opening is done I call the following function in my Appdelegate
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool
What I do here is that I iterate through all my viewControllers because I want to find my ViewTwo to be able to call a segue that I have created. I want to do it this way because I donĀ“t want to create a new instance of the viewController.
It works great with this function below to find ViewTwo:
if let viewControllers = window?.rootViewController?.childViewControllers {
for viewController in viewControllers {
if viewController.isKindOfClass(ViewTwo) {
viewController.performSegueWithIdentifier("sw", sender: self)
}
}
}
But now I have added a NavigationController to ViewTwo, ViewThree and ViewFour so when I run the snippet above I only get the following result for viewController (I made a simple print(viewController))
<ViewOne: 0x...>
<UINavigationController: 0x...>
<UINavigationController: 0x...>
<UINavigationController: 0x...>
So my question is, how do I check for ViewTwo now that I also have a NavigationController?
I solved this by
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("ViewThree") as! WebViewController
self.window?.rootViewController!.presentViewController(controller, animated: true, completion: { () -> Void in
})
You can get the view controller from storyboard without creating a new instance. You don't need to iterate through all the view controllers. Also why do you not set the view controller as the root of your nav controller instead of performing a segue?
You can get the view controller from storyboard like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("ViewTwo") as! ViewTwoViewController
Because now you are all getting UINavigationViewController. And it's just simply a controller of controller. You can access the root view controller from UINavigationViewController by accessing the first element of viewControllers property. Then, you are able to make the interpolation.
The root view controller is at index 0 in the array, the back view
controller is at index n-2, and the top controller is at index n-1,
where n is the number of items in the array.

swift ios cant remove old modal viewcontrollers from stack

In my app I am making the account page the new root VC when a user logs in.
It looks like this:
Navigation controller -> table view -> menu(modal segue) -> login screen(modal segue) -> account page
When transitioning from login to account I am using:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc = storyboard.instantiateViewControllerWithIdentifier("testVc")
let navigationController = self.view.window?.rootViewController as! UINavigationController
navigationController.setViewControllers([vc], animated: true)
This makes the account page the new root VC. But the only problem is that once is shows up both the menu and login form is still visible ontop of the screen.
So how do I clear two old VC's shown as modal?
Update got it to work using:
#IBAction func loginButtonDidTouch(sender: AnyObject) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("testVc")
let navigationController = self.view.window?.rootViewController as! UINavigationController
self.presentingViewController!.presentingViewController!.dismissViewControllerAnimated(false) { () -> Void in
navigationController.setViewControllers([vc], animated: true)
}
}
But I dont know if this is the right way to do it?
You need to get the reference of those controllers, and then dismissing them.
Try this:
let loginScreen = self.window.rootViewController.presentedViewController
loginScreen.dismissViewControllerAnimated(false) { () -> Void in
let menuScreen = self.window.rootViewController.presentedViewController
menuScreen.dismissViewControllerAnimated(false, completion: nil)
}
When you're calling the original navigation stack and modifying it:
let navigationController = self.view.window?.rootViewController as! UINavigationController
You are setting the new view controller (#testVc) by replacing the only other view controller, "tableview", in that navigation stack.
The modally presented views are not a part of that particular navigation stack and instead are presented above the current navigation stack as new stacks (this gives you a pointer to the new Navigation Controller on top in the form of self.navigationController to push new views)
You can explicitly dismiss the two modally presented views by calling dismissViewControllerAnimated(_:completion:) on each, most likely by propagating the communication through a delegate response or through the completion handler.

Resources