implement login screen before swrevealcontroller - ios

I've just started learning IOS development, I've successfully implemented the SWRevealViewController by following a given tutorial online all is working as expected.
I've then decided to add a login screen, which would be the first page the user see's when the application is run. The steps I took are a follows:
Dropped a UIViewController onto the story board
Set this as my 'is initial view controller'
Added a button to this new view and created a seque for testing purposes
But when I click this button it does nothing, so after searching the web and trying to figure this one by my self unfortunately I've been unsuccessful due to my lack of knowledge on this, would someone be kind enough to give me some pointers on what I need to change if anything, or what sections need to me modified to make this flow work as expected?
Below is a screen grab of my current story board.
Update
After adding the relevant code the app delegate file I still receieve this error message:

Step-1
embed your login VC to NavigationController.
Step-2
on your login button action set the segue type as Modal and call as
#IBAction func btnLogin(sender: AnyObject) {
self.performSegueWithIdentifier("openSWL", sender: self)
}
For flow understand purpose
For sample Project you can download here

The Storyboard arrangement looks good. I have used SWRevealController like below:
After you login (performing login service or some login process) write below code.
This code will change current rootViewController (In your case it is LoginViewController) to SWRevealController. So that it will work. And when ever you do logout change rootViewController to LoginViewController.
SWRevealViewController *controller = (SWRevealViewController *)[self.mainStoryboard instantiateViewControllerWithIdentifier:#"RevealViewController"];
[self.window setRootViewController:controller];
Do not forget to assign StoryboardID = "RevealViewController" in Storyboard for SWRevealViewController.
Swift Code:
Add below function to your AppDelegate.swift file:
func changeRootViewControllerToSWRevealViewController () {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("RevealViewController")
if let window = self.window{
window.rootViewController = controller
}
}
// Call above function in your login button action method like below:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.changeRootViewControllerToSWRevealViewController()

Related

How to skip the login scene and start straight from the next if you are already authorized?

What is the best place to check if the user is already logged-in and how to realize the transition to the first app scene skipping the login viewController?
You could try something like that:
In your FirstViewController you check if the user is logged in. If so, you push the MainViewController, if not you push the LoginViewController and after login you push the MainViewController.
You can build a interface for FirstViewController cloning the Splash Interface, so the user won't note you have another viewcontroller before Login.
Like this:
I do this in my AppDelegate. In my main storyboard, my initial view controller assumes that my user is logged in. Thus, in my AppDelegate I do the following to decide whether I need to show the login screen:
// Show login view if not currently logged in
if (!currentUser){
let sb = UIStoryboard(name: MainStoryboardId, bundle: Bundle.main)
let vc = sb.instantiateViewController(withIdentifier: loginViewControllerId)
vc.modalTransitionStyle = .crossDissolve
window.rootViewController = vc
window.makeKeyAndVisible()
}
PS, just converted code from Objective-C... it's untested.

How to Push a New View Without Storyboards Swift

First of all, I don't want to use storyboards at all. I am able to "present" the targeted view controller, but I can't get it to show with the standard iOS back button at the top. My understanding is I can get this to work by pushing the controller instead of presenting it. I don't get any errors, but nothing happens.
On a button click this code is run. The commented code is what successfully presented the ForgotPasswordPage :
// Changes to Forgot Password Page
func forgotPasswordSwitch(sender: UIButton!) {
//let ForgotPassword:ForgotPasswordPage = ForgotPasswordPage()
//self.presentViewController(ForgotPassword, animated: true, completion: nil)
let ForgotPassword = ForgotPasswordPage()
self.navigationController?.pushViewController(ForgotPassword, animated: true)
}
You have to manually create a UINavigationcontrollerto get the back bar. To do this you can use the code from this answer, which achieves this by using this code:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var nav1 = UINavigationController()
var mainView = ViewController() //ViewController = Name of your controller
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()
Here just add all the ViewControllers you want to be under the Navigation controller into the array and then push between them.
Hope that helps, Julian
I know that sometimes it's much better develop apps programmatically, but there are many time that Storyboard can save you a lot of time. You just simply use it for this situations...
1) In you Storyboard, localize the view controller you want to enable for pushing
2) Select it and find the "Editor" in your top bar
3) Select Editor->Embed In->Navigation Controller
And now your view controller it's ready for pushing

LoginViewController -> OtherviewController Two-way Segue

I am new to iOS development and XCode and am trying to figure out how to implement the following logic:
1) User starts app - user is directed to the LoginViewController that has a login form
2) Upon successful user login - go to the next view controller - let's call it LoginViewController
How do I programatically do that in the action logic for the login button in the view of LoginViewController e.g.
#IBAction func loginButtonPressed(button: UIButton) {
//do login logic
//what to put here to segue to OtherViewController
}
And once the user is in OtherViewController, it is possible their auth token will expire. If that happens I would need to segue back to LoginViewController. I come from an Android background where you would use Intents to make something like this happen - what is the equivalent in iOS?
An answer in Swift is preferred since I am more familiar with that, but I am decent at deciphering Objective C code as well.
As a side note- should I be controlling segues in code like this or should I be using storyboards? I tried creating segues in the storyboard but I do not understand how to hook those up to application code.
One way:
self.performSegueWithIdentifier("name2", sender: self)
As an example:
#IBAction func button(sender: AnyObject) {
self.performSegueWithIdentifier("newView", sender: self)
}
Here i created a modal segue from viewController1 to ViewController2 in the storyboard. I did this by control-click on the yellow ViewController icon at the top of the view and drag the blue line to the second view. Then release. in the popup menu select modal segue. Then in the utilities panel on the right - after highlighting the segue in the storyboard, click on the attributes inspector and give the identifier a name. Then paste that name in the code example where I have "newView".
Actually you can init you next controller from storyboard:
UIStoryboard * storyboard = self.storyboard;
OtherViewController * otherVC = [storyboard instantiateViewControllerWithIdentifier: # "xxx * Name Desire"];
If you use navigation controller:
[self.navigationController pushViewController: otherVC animated: YES];
Else:
[self presentViewController:otherVC animated:YES completion:nil];

Move to another ViewController

Im using StoryBoard and i am not so sure how to instantiate a ViewController or how to reference one.
The thing is i have two view controllers and i have one with a button. I want to go to the other view controller when i pressed the button from the first view controller.
I have tried something like that:
let secondViewController:UIViewController = UIViewController()
self.presentViewController(secondViewController, animated: true, completion: nil)
Does anyone knows how to explain it to me? Thanks!
There are couple of ways of navigating between view controllers. Here's how you do it in code without segues if you're going that way.
Presenting a view controller modally.
Say you have 2 view controller scenes in the storyboard, FirstViewController and SecondViewController. You want to transit from FirstViewController to SecondViewController and there is no segue between them.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("SecondVC") as UIViewController
presentViewController(secondViewController, animated: true, completion: nil)
The important part is the that you have to assign an identifier to the view controller you want to go. In this case its the SecondViewController. The way you assign an identifier is you select the view controller, open up the right panel and in it, go to the Identity Inspector (the third one from the left) and under the Identity, assign an identifier to the Storyboard ID field. I put mine as SecondVC as you can see from the code snippet above.
Push to another view controller.
If you want to push on to another view controller instead of presenting it, all you have to do is embed the FirstViewController in a UINavigationController and change the code to this.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("SecondVC") as UIViewController
navigationController?.pushViewController(secondViewController, animated: true)
In a comment of yours I saw you want to go to the next view controller based on a condition. Well all you have to do is check your condition in a if else statement and execute either of those above code.
Using Segues.
If you're going to use segues instead of code, here's how you do it.
In the storyboard first you select the button and Ctrl drag to the other view controller. You'll be prompted to choose between different segues. Select show for push or show detail for modal transition (I've explained what these are below). And that's it! If you run it and tap the button, you'd be taken to the other view controller.
But if you want more control over this, you have to do a little more work.. Instead of creating a segue directly from the button, select your view controller and select that little yellow icon on top (This is in Xcode 6. In older Xcode versions its under the view controller scene). Ctrl drag from that icon to the other view controller you want to transit to. You can see a connection appears between those two controllers. Select the segue and open up the right panel and go to the Attributes Inspector (The forth one from the left). Give a name to the field identifier. I gave ToSecond.
Now create a normal action from the button. And you have to call a method called performSegueWithIdentifier passing that identifier. What it does is basically execute a segue with the identifier we give.
#IBAction func segueButtonPressed(sender: UIButton) {
performSegueWithIdentifier("ToSecond", sender: nil)
}
And this would work. You can do the conditions checking here inside a if else and if the conditions are met, call performSegueWithIdentifier.
One other thing you're likely to face is having multiple buttons in a view controller and segueing to different view controller when you tap each of them. A method called prepareForSegue fires each time a segue happens. And inside it, you can check for current segue identifier and execute it. The below code snippet will make this clearer.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ToSecond" {
let secondViewController = segue.destinationViewController as SecondViewController
}
}
Another thing I'd like to mention is that presentViewController and pushViewController are deprecated from iOS8. Instead you can use showDetailViewController for modal and showViewController to push.
showDetailViewController(secondViewController, sender: nil)
navigationController?.showViewController(secondViewController, sender: nil)
I'm not sure if these are backwards compatible. Meaning using show and show detail will work if you're developing for iOS 7 as well. If you are then just stick with the older methods.
First set seques of uistoryboardviewcontroller and try this
self.performSegueWithIdentifier("push", sender: self)
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "push" {
}
In the storyboard, select VC1 and then select the button and press control and while holding down the left button or touchpad drag across to your VC2. Then a menu should pop up. Select modal.
Run and Test. It should now perform a transition.
To transition back, the easiest way is to embed VC1 in a Navigation Controller. To do this, zoom out, select VC1 and go to the top of the screen and select:
Editor > Embed > Navigation View Controller.
Now test and run. You should have the option to go back.
If you are binding view controller programmatically you need to follow this step if you are creating storyboard based application.
It is similar what we don in Objective-c just the syntax has changed.
According to your question what you need to do is go to Main.storyboard and need to select identity inspector.
There you will be able to view identity which contains two fields
1.) Storyboard ID
2.) Restoration ID
Give them the name of view controller you have binded with class in storyboard id and select check box below restoration id. It will automatically copy storyboard ID in restoration ID.
You need to do same for all your view controllers.
let secondView = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.presentViewController(secondView, animated: true, completion: nil)
you need to write the name you have entered in Storyboard ID for self.storyboard?.instantiateViewControllerWithIdentifier("/* Storyboard ID */")
You can also visit the link:
Instantiate and Present a viewController in Swift
okay,
in interface builder control click on your button and drag the blue line that appears to the second view controller. The second view controller will highlight blue. You can release and the button is connected. In the popup menu select "modal segue". No code necessary. XCODE handles it.
Watch this video for a demo.

Return to initial view controller when user logs out

I'm working on an app which uses Facebook integration, and the log in system works fine now. However, I can't seem to return to my initial view controller when the user clicks log out.
Here's an overview of my storyboard:
I would like to return to the start when the user clicks the blue button (on the top). What would I do to achieve that? As you can see I have multiple Navigation Controllers, and I only use Push-seguesto get there. However, I do use the SWRevealViewController, which you can see in the middle.
I've tried [self.navigationController popToRootViewControllerAnimated:YES]; which doesn't do anything.
Any advice? Anyone familiar with the SWRevealViewController and what it might have done to my Navigation stack? Any help will be appreciated!
Thanks.
Try this,
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"NameOfYourStoryBoard"
bundle:nil];
LoginViewController *add =
[storyboard instantiateViewControllerWithIdentifier:#"viewControllerIdentifier"];
[self presentViewController:add
animated:YES
completion:nil];
Write Below Method in root viewcontroller
- (IBAction)returnToDashboard:(UIStoryboardSegue *)segue;
Give segue connection to destination view controller like below
Give identifier to segue and assign method to that segue
use below method in destination view controller
[self performSegueWithIdentifier:#"pushtoDashboard" sender:self];
First of all, I don't think you need that many UINavigationControllers. Using only one in your application should be enough.
The reason popToRootViewController is not working in your case is because it will go to the first view controller withing a UINavigationController. You have nested many UINavigationControllers, thus when you click the blue button in the settings view controller it will go to the sidebar view controller (can't read it properly, the image is small).
You can do the following to get to the root view controller of your app:
UINavigationController *rootController =[(UINavigationController*)[(AppDelegate*)
[[UIApplication sharedApplication]delegate] window] rootViewController]];
Replace AppDelegate with however it's called in your app.
But my advice is to remove all intermediate UINavigationControllers. Then just doing popToRootViewController should do the trick.
Problem
You'd like to return from a view controller (source) to the start (a
destination view controller) when the user clicks the blue button (on the
top)
Recommendation
I recommend you take a quick look at the highly rated SO answer which demonstrates how to use the unwind segue (that's the little green exit box on your view controller in the storyboard). Unwind segues are the modern way of accomplishing your goal, but you can also call dismissViewController:animated from the source controller. You should also take a quick read of a very small Apple note (TN2298) on using the unwind segue.
Essentially you will want to add the following method to your destination view controller:
- (IBAction)unwindToMainMenu:(UIStoryboardSegue*)sender
{
}
Then use ctrl+drag and click from the blue button down to the green exit icon on the source view controller. This will popup a menu and you can select unwindToMainMenu from the list. You will need to give the new segue an identifier in the Identity Inspector (e.g. segueToMain).
Manual Unwind
The technical note above (TN2298) will also show you how you can create a manual unwind segues that may be called programmatically (similar to how one might say performSegueWithIdentifier...).
I was working on a very similar problem. I am using a storyboard with a navigation controller & implemented the highly recommended SWRevealViewController, with iOS 7 & Xcode 5.1. I tried unsuccessfully to implement some of the solutions mentions. Either the screen didn't change or I got blank table. I used a hybrid version of the programatic examples provided in SWRevealController & other answers here to get a working solution. I added this as apart of my login button action
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
InboxTableViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:#"inbox"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.revealViewController pushFrontViewController:nav animated:YES];
I retrieved my storyboard & initiated the view controller I wanted from the storyboard & then added to a navigation controller. Finally I used the SWRevealViewController method to Push the view I desired to the front.
I'm using swift and what worked for me was this:
var loginVC: UIViewController? = self.storyboard?.instantiateViewControllerWithIdentifier("UILogin") as? UIViewController
self.presentViewController(loginVC!, animated: true, completion: nil)
When you have different storyboards simply "presenting" the required VC from the initial storyboard does the trick:
Swift 3
if let loginVC = UIStoryboard(name: "Login", bundle: nil).instantiateInitialViewController() {
present(loginVC, animated: true, completion: nil)
}
In some cases there might be leaking UITransitionView's. You might remove them right after the "presenting" code, but not before it and not in it's completion:
if let subviews = UIApplication.shared.keyWindow?.subviews,
let transitionViewClass = NSClassFromString("UITransitionView") {
for subview in subviews where subview.isKind(of: transitionViewClass) {
subview.removeFromSuperview()
}
}
<– This works as of Xcode 8.2.1 and iOS 10.2 but no guarantee if will work forever. Be careful with it.

Resources