Manual segue with SWRevealViewController in Swift - ios

There is a great blog post over at http://www.appcoda.com/tag/swrevealviewcontroller/ that goes in to setting up SWRevealViewController which is a great component for slide out side menus (https://github.com/John-Lluch/SWRevealViewController)
Unfortunately, there are no swift examples of how to perform a manual segue.
Took a cleaner approach to storyboard support. SWRevealViewControllerSegue is now deprecated and you should use SWRevealViewControllerSegueSetController and SWRevealViewControllerSeguePushController instead.
I've tried something along the lines of:
let navigationController = self.window?.rootViewController as! SWRevealViewController;
let viewController = navigationController.storyboard?.instantiateViewControllerWithIdentifier("ImportFileSelect") as! ImportFileSelect
navigationController.showViewController(viewController, sender: self)
This doesn't work though. Any ideas? I've trawled the web for swift examples, my next step is to learn objective c!

In order to work you'll need to following steps:
You need to instantiate the SWRevealViewController and then attach it
to the root controller.
Then instantiate the destination controller.
Then create a navigation controller and set the destination
controller as the rootViewController
Finally push the navigation controller with SWReveal
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sw = storyboard.instantiateViewControllerWithIdentifier("SWRevealViewController") as! SWRevealViewController
self.view.window?.rootViewController = sw
let destinationController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardID") as! NameOfViewController
let navigationController = UINavigationController(rootViewController: destinationController)
sw.pushFrontViewController(navigationController, animated: true)

I've kind of made some progress. I can load in a new view controller, but it doesn't animate in anyway. The code to do this, on a button click is:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("TARGET_VIEW_CONTROLLER") as! UIViewController
var rvc:SWRevealViewController = self.revealViewController() as SWRevealViewController
rvc.pushFrontViewController(vc, animated: true)

Download SWRevealViewController project's zip form Github.
Drag SWRevealViewController.m and SWRevealViewController.h files from zip (SWRevealViewController folder) to your project, and Click “Yes” to the prompted message "Would you like to configure an Objective-C bridging header?"
Take a look at storyboard in RevealControllerStoryboardExample2 project. Design your storyboard with this example.

This is not how it should be done, but at least i found a way to do it.
In the TableViewController where u have the slideOut menu do something like:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(indexPath.row == <some position> ){
let vc: AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("YOUR_DESTINATION_IDENTIFIER")
self.showViewController(vc as! SWRevealViewController, sender: vc)
}
The one that satisfy the condition will segue with default animation, not like the slide out menu normally does.
After, in storyboard, do a normal Reveal View Controller Push controller, and as long as it doesn't satisfy the condition it will exit the slide out menu normally

Related

How to open specific ViewController from TabBarController

Sorry, if it is an essential question.
The scheme is the following:
enter image description here
I want the following:
User taps last Tab
User goes to some ViewController (different from ViewController, actually connected with last Tab)
How to do this (without using segue)?
Thanks a lot in advance!
You can go with instantiateViewController with push when load your tab and push one viewController to another like below:
let next = self.storyboard?.instantiateViewController(withIdentifier: "nextVC")as! nextVC
self.navigationController?.pushViewController(next, animated: true)
You can assign ViewController in you UITabBarController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create view controllers from storyboard
// Make sure you set Storyboard ID for both the viewcontrollers in
// Interface Builder -> Identitiy Inspector -> Storyboard ID
let clockViewController = storyboard.instantiateViewControllerWithIdentifier("ClockViewController")
let stopWatchViewController = storyboard.instantiateViewControllerWithIdentifier("StopWatchViewController")
// Set up the Tab Bar Controller to have two tabs
let tabBarController = UITabBarController()
tabBarController.viewControllers = [clockViewController, stopWatchViewController]
// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController

Problems using self.navigationController?.pushViewController(newViewController, animated: true)

the thing is im new to ios development this is my second application while working on the first one i mainly used a theme and then worked over it but this one im working from scratch.
My main problem is going from one screen to another
When i use this code it works
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "servicesScreen") as! ServiceViewController
nextViewController.modalPresentationStyle = .fullScreen
self.present(nextViewController, animated:true, completion:nil)
But i need to use this one so i can go back too but this wont do anything
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "servicesScreen") as! ServiceViewController
self.navigationController?.pushViewController(newViewController, animated: true)
My question is, am i doing anything wrong here or do i need to add something in scenedeligate?
try
select Main.storyboard
select initial view Controller
select editor -> Embed in -> navigation controller.
For this, you need to add navigation controller to initial storyboards.
If you are using navigationController you need to add a navigation controller first on storyboard or programmatically.
Programmatically add navigation controller check this link:-
Creating a navigationController programmatically (Swift)
or
To add navigation controller on storyboard you should follow this link:-
https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementNavigation.html
If you have added navigation controller to your app then make sure your intialViewController is set to navigation controller or your navigation controller is added to the stack when you run your application.
Also check for "Is the identifier correct? Did you reference the correct storyboard?".

Segue to another storyboard

I dont know how create segue from storyboard 2 to storyboard 3(image: https://i.stack.imgur.com/NLhJY.png). Afterwards user tap save button(storyboard 2), i need save item and open saved item on storyboard 3. I try use Unwind Segue, but this doesnt work.
I try use instantiateViewController. I set on storyboard 3 Storyboard ID("Item") and then add this code to save button:
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Item") as! ItemViewController
self.present(viewController, animated: false, completion: nil)
But when Storyboard 3 open, after tap Save Button on Storyboard2, navigationbar on Storyboard 3 doesnt showed. Anyone can help find me solution to this problem?
You need to specify which storyboard you want to use exactly along with your ViewController identifier.
Try this:
let storyboard: UIStoryboard = UIStoryboard(name: "YOUR_OTHER_STORYBOARD_NAME", bundle: nil)
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Item") as! ItemViewController
self.show(viewController, sender: self)

Programatically creating Segues in ios swift

In my app I use side bar as in facebook. when the user slides out the side bar a uiimageview is displayed. when user taps on the image it takes hm to a different viewcontroller. the problem i am facing is that I have created sidebar programatically and the other view to which I want to navigate the user is created using storyboard. So my source view is created programatically and destination view is created using storyboard. So can someone explain me if there is any way of using "Segue" in this scenario. Since i can not create segue using storyboard I need to do it programatically but even after a lot of googling i could not find the answer.
Well, to get another instance of another storyboard programmatically you can use something like:
let newController = UIStoryboard(name: "MyStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("MyIdentifier") as! MyViewController
and then you push to your navigation controller, or add as a child view controller or something...
If you don't wanna bother with identifiers you can just use the instantiateInitialViewController instead of instantiateViewControllerWithIdentifier
Might help
"userSB" is the viewcontroller storyboard identifier
#IBAction func tapSearchCriteria(_ sender: Any?) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let aVC = storyboard.instantiateViewController(withIdentifier: "userSB") as? AViewController
aVC?.modalPresentationStyle = UIModalPresentationStyle.custom
aVC?.transitioningDelegate = self
aVC?.udelegate = self
self.present(aVC!, animated: true, completion: nil)
}

Show Taps in TapBarViewController

I have a TapBarViewController with three Taps in my project.
Now I am trying to put a normal FirstViewController in front of the TapBar ViewController when the application launches.
The FirstViewController should have three buttons for one of the individual Taps of the TapBarViewController.
How can I show the individual Taps of the TapBarViewController by pressing one of the three buttons?
Screenshot
This is the exact answer you are looking for.
Steps:
Add a UIViewcontroller and embed with UINavigationViewController (Editor -> EmbedIn -> NavigationController).
Add Three button + 1 Button (Navigate to TabViewController).
Drag and drop UITabBarController to the storyboard it comes with one default ViewController and place a UILabel named as 'ButtonOneClicked'.
Create Two more ViewControllers and named as 'ButtonTwoClicked' and 'ButtonThreeClicked'.
Connect 'ButtonOne' with FirstViewController and set segue as Show and repeat the same for rest two.
Click on each segue and provide an unique Identifier name.
Then connect the UITabBarController with each UIViewController and choose viewcontrollers
Create a ButtonAction to the Tab Button.
Copy paste the below code in that action.
#IBAction func tabClicked(_ sender: Any) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "Tab") as! UITabBarController
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()}
Hope this helps you!!

Resources