how to move from one view Controller to another without using a button - swift3

I am using the following code snippet:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
I want to execute this code in the viewDidLoad. I will be using something like this in a series of if statements.
I used some code I found here that utilized a label. It did not work. Can a program move from one viewController without using a button? Almost every example, youtube video uses a button. I will be using a button when it is appropriate

You can not push view controllers before your current view controller loads. You should call your code in your viewDidAppear.
If you plan on directing to a particular view when the app launches I suggest you moving your logic to the AppDelegate in the method: didFinishLaunching. This way you don't need to unnecessarily load views.
Example of a ViewController being pushed in the viewDidAppear:
///
/// FUNCTION - VIEW DID APPEAR
///
override func viewDidAppear(_ animated: Bool) {
// GET VIEW CONTROLLER
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
// PUSH VIEW ONTOP OF CURRENT NAVIGATION CONTROLLER
self.navigationController.pushViewController(secondViewController, animated: true)
} // END - FUNCTION

Related

Pass variable to next View Controller

I want to pass a variable from one view controller to another.
First View Controller looks like this and the segue gets triggered correctly and the new view controller gets displayed.
#IBAction func testButton(_ sender: Any) {
let timerVC = TimerViewController()
timerVC.secondsPassed = textfield_seconds.text!
navigationController?.pushViewController(timerVC, animated: true)
}
On the next View Controller (TimerViewController) I declared a variable in the class header
var secondsPassed:String!
and in viewDidLoad I just want to print the value to the console and all I receive is a 'nil'.
I looked through various tutorials but I seem not to find the correct answer for this to get it work.
Anyone around with a clue?
Thanks in advance...
If you already are using a segue, you can remove this line that presents the controller (you don't seem to have a navigationController anyway):
navigationController?.pushViewController(timerVC, animated: true)
Now the correct way to pass data to another VC using segues is this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? TimerViewController {
vc.secondsPassed = textfield_seconds.text!
}
}

Snapshotting a view that has not been rendered results in an empty snapshot when view presented modally - Swift 3

I have a Table View Controller as a tab. In it, there's a button that takes you to a Facebook profile:
func didTapFacebook() {
let url = URL(string: "http://www.facebook.com/" + myFacebookId)
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
Works fine every time you press the button.
From another tab, there's a button that presents modally a navigation controller with Table View Controller as its root:
func segueToTable(_ sender: UIViewController, tvc: MyTableViewController, completion: #escaping ((_ done: Bool) -> Void)) {
let nc = MyNavigationController(rootViewController: tvc)
sender.present(nc, animated: true, completion: {
completion(true)
})
}
…
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tvc = storyboard.instantiateViewController(withIdentifier: "TVC") as! MyTableViewController
segueToTable(self, tvc: tvc, completion: { done in
print(“segue complete”)
})
Now once you tap the same button to go to Facebook profile (or Twitter button, basically anything that causes the app to go to background), this warning occurs (there is no keyboard displayed on the screen at this time):
Cannot snapshot view (>) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES.
Returning to the app and pressing the button again causes this warning to appear (and every attempt after that):
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
I realize this is related to snapshotting a view that automatically occurs prior to entering the background. It appears for some reason it doesn't like doing that within a view that was presented modally. I've tried a variety of things based on other posts but cannot get the warnings to go away.
Any help is appreciated.

How to change the way in which the root controller of a navigation controller is appearing on screen?

The navigation controller is not the initial view controller. It is being instantiated in the initial VC and supplied with a root controller, the second VC. When I call present view controller function I cannot change the way in which this new VC in being presented, it always appears from the bottom. I did not find how to make it come from right to left. I tried to change who gets the call to modalPresentaionStyle from the second VC to the nav con and back but no change.
Code listing below. Thank you for your help.
#objc fileprivate func showRegisterScreen() {
let registerAccountVC = RegisterAccountVC()
let navigationController = UINavigationController(rootViewController: registerAccountVC)
navigationController.modalPresentationStyle = .pageSheet
present(navigationController, animated: true, completion: nil)
}
This will add a custom animation from right to left.
let transition = CATransition.init()
transition.duration = 0.4
transition.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
view.window?.layer.add(transition, forKey: nil)
present(navigationController, animated: false, completion: nil)

Using SideMenu Pod for navigation causes bugs when changing screens

I am using this pod: https://github.com/jonkykong/SideMenu for my Side bar in my app. I set the side bar navigation through Storyboard. My app design is that I have a main screen, and a side bar with buttons that take me to different screens. The different screens also have the same side bar. My bug is in this situation: I go into the side bar from main screen -> Then I go into screen2 through the side bar -> I click on the side bar from the screen 2 -> Side bar appears from the bottom and takes the whole screen.
For fixing this bug I found a workaround that does NOT satisfy me. I understood I need to dismiss the side bar but then if I want to go into another screen I first see the screen I came from (because I dismiss) and then it only moves to the second screen. I hope this makes sense. I want to achieve switching to the second screen without first seeing the main screen. This is the code that helps me dismiss and then change screens:
dismiss(animated: true, completion: {
goIncident()
})
func goIncident(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let window = appDelegate.window
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = mainStoryboard.instantiateViewController(withIdentifier: "log") as! UINavigationController
window?.rootViewController = rootController
}
Edit: The added photo shows the problem when I only use the function goIncident() without dismissing. If I do dissmiss, I see the home screen and only after the home screen is presented when it calls the new screen to appear.
So I used a very ugly method to solve this, but I believe this is the only possible way, since this Pod is not very flexible, unless I missed something. What I did is this:
In SideBarViewController:
func goLog(){
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeLog"), object: self)
}
In MainViewController:
override func viewDidLoad(){
NotificationCenter.default.addObserver(self, selector: #selector(goLog), name: NSNotification.Name(rawValue: "changeLog"), object: nil)
}
func goLog(){
let logViewController = self.storyboard?.instantiateViewController(withIdentifier: "IncidentLogViewController") as! IncidentLogViewController
self.navigationController?.viewControllers = [logViewController]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dismissSideBar"), object: self)
}
So the Side bar tells the navigation controller to replace its view controller, and then after its replaced the navigation controller send back a notification to dismiss the the side bar. Pretty ugly but actually works smoothly.

UIPageViewController transition (passing through viewcontroller before the target viewcontroller)

Let`s say i have 3 or more view controllers in pageViewController.
If i go to third view controller from my first view controller, the transition is directly to Third view controller without passing through second View controller.
I`m using this code to make the transition:
let startVC = self.viewControllerAtIndex(indexPath.row)
let viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as?
[UIViewController], direction: .forward, animated: true, completion: nil)
Is it possible to getting through the second view controller like this animation?
https://github.com/calvinsug/SlidingContainerViewController