Hide ios 8 sidebar swift? - swift

im currently using the amazing open source library https://github.com/evnaz/ENSwiftSideMenu for my application navigation sidebar. However, I created an login page for the project, but couldn't figure out an way to hide the sidebar in my login page, can anyone help me?
my view controller (where will guide the user to login page if login does not login )
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var currentUser = PFUser.currentUser()
if currentUser != nil {
NSLog("CurrentUser:", currentUser.username)
// Do stuff with the user
} else {
// Show the signup or login screen
self.performSegueWithIdentifier("showLogin", sender: self)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func logout(sender: AnyObject) {
PFUser.logOut()
self.performSegueWithIdentifier("showLogin", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showLogin") {
// pass data to next view
segue.destinationViewController.sideMenuController()?.sideMenu?.hideSideMenu()
//sideMenuController()?.sideMenu?.hideSideMenu()
}
}
}
my login page:
import UIKit
class LoginViewController: UIViewController {
#IBOutlet weak var usernameField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBAction func loginButton(sender: AnyObject) {
var username = String (self.usernameField.text .stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()))
var password = String (self.passwordField.text .stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()))
if username.isEmpty || password.isEmpty{
let alert = UIAlertView()
alert.title = "Oops"
alert.message = "Please make sure you enter all the field"
alert.addButtonWithTitle("Okay")
alert.show()
}
else {
PFUser.logInWithUsernameInBackground(username, password:password) {
(user: PFUser!, error: NSError!) -> Void in
if user != nil {
// Do stuff after successful login.
self.navigationController?.popToRootViewControllerAnimated(true)
} else {
// The login failed. Check error to see why.
let alert = UIAlertView()
alert.title = "Oops"
alert.message = error.userInfo!["error"] as NSString
alert.addButtonWithTitle("Okay")
alert.show()
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
//self.sideMenuController()?.sideMenu?.hideSideMenu()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

You need to call hideSideMenuView() function in your view controller. It is view controller's extension public function. Also there are showSideMenuView() and toggleSideMenuView() functions.

Related

Error : "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee0234ff0)"

I have this code and when I am running the App on the line with "super.viewDidLoad()", I have the "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee0234ff0" error...
Can someone help me please?
On internet they say me that I have to connect my Developer Account, but I don't have one.
This is the code :
import Foundation
import UIKit
import FirebaseAuth
import FirebaseDatabase
class SignUpController : UIViewController {
//Fonction pour scroller
private let scrollView: UIScrollView = {
let scrollView = UIScrollView.self()
scrollView.clipsToBounds = true
return scrollView
}()
//MARK : Outlets
#IBOutlet weak var artistnameTextField: UITextField!
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var confirmationPasswordTextField: UITextField!
#IBOutlet weak var signupButton: UIButton!
#IBOutlet weak var loginButton: UIButton!
//MARK : Properties
override func viewDidLoad() {
super.viewDidLoad()
viewDidLoad()
//Add subviews
view.addSubview(scrollView)
scrollView.addSubview(artistnameTextField)
scrollView.addSubview(emailTextField)
scrollView.addSubview(passwordTextField)
scrollView.addSubview(confirmationPasswordTextField)
scrollView.addSubview(signupButton)
scrollView.addSubview(loginButton)
//Design
setupButtons()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showLogin" {
let VCDestination = segue.destination as! LoginController
VCDestination.myMail = emailTextField.text!
}
if segue.identifier == "showLogin" {
let VCDestination = segue.destination as! LoginController
VCDestination.myPassword = passwordTextField.text!
}
}
//MARK : Private Methods
private func setupButtons() {
signupButton.layer.cornerRadius = 20
loginButton.layer.cornerRadius = 20
loginButton.layer.borderWidth = 3
loginButton.layer.borderColor = UIColor.lightGray.cgColor
}
private func setupTextFieldManager() {
artistnameTextField.delegate = self
emailTextField.delegate = self
passwordTextField.delegate = self
let tapGesture = UITapGestureRecognizer (target: self, action: #selector(hideKeyboard))
view.addGestureRecognizer(tapGesture)
}
//MARK : Actions
#IBAction func signUpButton(_ sender: UIButton) {
performSegue(withIdentifier: "showLogin", sender: nil)
}
#objc private func hideKeyboard() {
artistnameTextField.resignFirstResponder()
emailTextField.resignFirstResponder()
passwordTextField.resignFirstResponder()
}
#IBAction func signupButtonWasPressed(_ sender: UIButton) {
if artistnameTextField !== "" as AnyObject && emailTextField !== "" as AnyObject && passwordTextField !== "" as AnyObject && confirmationPasswordTextField.text == passwordTextField.text {
Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (authResult, error) in
if error != nil {
print(error.debugDescription)
self.alertUserLoginError()
} else {
print ("Inscription en cours...")
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).setValue(["artistName": self.artistnameTextField.text])
self.performSegue(withIdentifier: "goToHome", sender: self)
}
}
} else {
print("Veuillez remplir tous les champs.")
}
}
#IBAction func loginButtonWasPressed(_ sender: UIButton) {
print("Redirection vers l'écran de connexion...")
}
func alertUserLoginError() {
let alert = UIAlertController(title: "Erreur", message: "Veuillez remplir tous les champs", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
present(alert, animated: true)
}
}
extension SignUpController: UITextFieldDelegate{
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
Thanks
Ok so for the sake of brevity, here's a concept called "App Lifecycle" which by its nature is the methods that are called by the iOS system and in a specific order. Remember that computers can only run one thing at a time, per CPU. So it must go in an order of some sort. The iOS lifecycle events are as follows.
loadView()
viewDidLoad()
viewWillAppear()
viewDidAppear()
didReceiveMemoryWarning()
viewWillDisappear()
viewDidDisappear()
Now bear in mind there are a few others depending on what you're doing but these are the most common ones.
With 90% of these lifecycle methods, there's something more that is going on in the background that you don't see. This is where the super.methodName() call comes into play. It allows you to add additional functionality at a given lifecycle event without losing any of the other functionality that is being provided by that method. Otherwise, you might lose something that is required to load the view. Eg, super.methodName retains all previous functionality.
Now, to your issue, you have the following lines of code.
override func viewDidLoad() {
super.viewDidLoad()
viewDidLoad()
//A bunch more code after this.
}
Looking at the Lifecycle methods, that are required in many cases, notice that you are calling viewDidLoad() inside of your super.viewDidLoad which means that your main thread gets stuck. It loops back through that method that called it, and then there it is again, another viewDidLoad() call. In turn, it loops, again, and again, and again, until it crashes. There is a time and a place for recursion, however, this is not one of them, and that's for a completely separate topic.
Ultimately your solution is to remove the viewDidLoad() method call after your super.viewDidLoad() and that will resolve the error that you're having.

Unable to send an int value between view controller and table view controller in swift

the class PreferencesViewController sends nil through prepare for segue regardless if IBActions are taken or not. It seems that something is wrong in my prepare for segue but I'm not sure. I'm not trying to assign to any label or anything I'm just trying to send the int value to another variable in the class Pick2_1. Thanks in adavance
import UIKit
class PreferencesViewController: UIViewController {
var preference = 0
#IBAction func nImportant(sender: UIButton) {
preference = -1
print(preference)
}
#IBAction func N(sender: UIButton) {
preference = 0
print(preference)
}
#IBAction func mImportant(sender: UIButton) {
preference = 1
print(preference)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "preferenceSegue" {
let preferenceVC = segue.destinationViewController as! Pick2_1
preferenceVC.preferenceSent = preference
print(preference)
}
}
}
Ok segue is directly connected to the button. You have to set the segue between your 2 controllers set the identifier and in the #IBAction you should call performSegueWithIdentifier func

How to call segue/ unwind right after login with Facebook

I created a log-in screen with 3 text fields (username, email, password). with 3 action buttons (Register, Login, Facebook login).
I was able to log in with facebook account, but I can't seem to present the details of the user, such name, and profile pic.. I tried to displayed it on the currentviewcontroller or the destinationVC but I guess I'm doing something wrong with the segue and Facebook
import UIKit
import Parse
import FBSDKCoreKit
import ParseFacebookUtilsV4
var myAlert = ""
var user = PFObject(className:"User")
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var userName: UITextField!
#IBOutlet var email: UITextField!
#IBOutlet var passWord: UITextField!
#IBOutlet weak var userlab: UILabel!
let permissions = ["public_profile", "user_friends", "email"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.userName.delegate = self
self.email.delegate = self
self.passWord.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func LogInToFacebook(sender: AnyObject) {
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
print("User signed up and logged in through Facebook!")
self.userlab.text = PFUser.currentUser()?.username
self.performSegueWithIdentifier("CSegue", sender: self)
} else {
print("User logged in through Facebook!")
//self.image = UIImage(named: "logout_FB.png")!
self.userlab.text = PFUser.currentUser()?.username
//self.fbOutlet.setImage(self.image, forState: UIControlState.Normal)
self.performSegueWithIdentifier("CSegue", sender: self)
}
} else {
print("Uh oh. The user cancelled the Facebook login.")
}
}
}
at the destinationVC (HomeVC) I just created 3 labels
import UIKit
import Parse
import FBSDKCoreKit
import ParseFacebookUtilsV4
class HomeVC: UIViewController {
#IBOutlet weak var userNamelabel: UILabel!
var user: PFUser!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "LogInToFacebook" {
user = PFUser.currentUser()
userNamelabel.text = user.username
}
}
That's the segue I am trying to use:
override func perform() {
let sourceViewController: UIViewController = self.sourceViewController
let destinationViewController: UIViewController = self.destinationViewController
user = PFUser.currentUser()!
sourceViewController.view.addSubview(destinationViewController.view)
destinationViewController.view.transform = CGAffineTransformMakeScale(0.05, 0.05)
UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
destinationViewController.view.transform = CGAffineTransformMakeScale(1.0, 1.0)
}) { (finished) -> Void in
destinationViewController.view.removeFromSuperview()
sourceViewController.presentViewController(destinationViewController, animated: false, completion: nil)
}
}

Swift - passing text and int from vc1 to vc 2 (uitextfield -> label in vc2)

Passing only input text from textfield to label in second VC works!! But i want when user type number 10 in uitextfield, ( 1 ticket is 2 euros so 10 tickets * 2 euro is 20) and when i click PAY button, so that SUM can be displayed in label in second VC, i think that viewdidload in VC2 is happening before prepareForSegue, i don't know. It works when i click second time on PAY button, but not when i first click PAY button where label displays zero, help :) Embedded in navigation controller for navigation.
VC1
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var howManyTickets: UITextField!
var sumTicketsAndPriceOfTickets = Int()
var priceOfTicket = 2 // euros
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func platiTeKarte(sender: AnyObject) {
sumTicketsAndPriceOfTickets = howManyTickets.text.toInt()! * priceOfTicket
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let driver = segue.destinationViewController as! primaocViewController
var whatToPass = sumTicketsAndPriceOfTickets
driver.receiver = whatToPass
}
}
VC2
import UIKit
class primaocViewController: UIViewController {
#IBOutlet weak var displaySum: UILabel!
var receiver:Int!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.displaySum.text = String(receiver)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
If your button is wired directly to a segue, then you don't need an #IBAction as well. As you are seeing, the prepareForSegue is happening before the #IBAction for your Pay button. Just compute your value in prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let driver = segue.destinationViewController as! primaocViewController
driver.receiver = (howManyTickets.text.toInt() ?? 0) * priceOfTicket
}
I changed the calculation of the pay to use the nil coalescing operator ??. This is generally a safer approach because if the toInt() returns nil for any reason, it will in this case just use 0 instead of crashing.

Linking View Controllers through button

I'm a beginner at all of this...Having said that I've come across a point in my app where I've stalled and don't know what to do or fix next. So any answers would be appreciated!
So in my Home View Controller, I have four buttons with four different categories.
Each of these categories has its own question list, but they have a common "General Question" list. The general question list has its own view controller.
When you click on any of the four buttons, it brings you to the General Question view. At the bottom of this view, I have a "Next" button.
Goal: Configure the Next button to continue to one of the category's question list based on what is initially pressed in the Home View Controller.
I've connected the buttons via outlet and action in the View Controller.
However, the Next button will not connect when I control + drag into the View Controller. I'm not sure where I need to put the code for this...
I was thinking that the code for the Next button might need to have some kind of conditional statement, but since it won't connect I can't even get that far.
Help!
(This is what I have) Sample Code:
import UIKit
import AddressBookUI
import AddressBook
import Foundation
import CoreData
import CoreGraphics
import EventKit
import EventKitUI
import CoreFoundation
class ViewController: UIViewController {
#IBOutlet var ColorButton: UIButton!
#IBOutlet var StyleButton: UIButton!
#IBOutlet var CutButton: UIButton!
#IBOutlet var MakeupButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var eventstore: EKEventStore!
var event: EKEvent!
weak var editViewDelegate: EKEventEditViewDelegate!
#IBAction func ColorButtonPressed(sender: UIButton) {
}
#IBAction func StyleButtonPressed(sender: UIButton) {
}
#IBAction func HaircutButtonPressed(sender: UIButton) {
}
#IBAction func MakeupButtonPressed(sender: UIButton) {
}
}
Here is a suggested approach as shown in the code below for 2 controllers (instead of 4) for brevity. Use appropriate named segues to each of the "next processing" controllers from the common processing controller and set up a chain. Here is a link to the project file: Project file
import UIKit
class ViewController: UIViewController {
var nextVcId = 0 // defines the button that is pressed
#IBAction func unwindFromOtherControllers(segue: UIStoryboardSegue) {
// In case you want to get back to the main VC
}
#IBAction func btn2Action(sender: UIButton) {
nextVcId = 0
self.performSegueWithIdentifier("commonSegue", sender: sender)
}
#IBAction func btn1Action(sender: UIButton) {
nextVcId = 1
self.performSegueWithIdentifier("commonSegue", sender: sender)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let vc = segue.destinationViewController as! CommonViewController
vc.nextControllerId = nextVcId
}
}
import UIKit
class CommonViewController: UIViewController {
var nextControllerId = 0
#IBOutlet weak var StatusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.StatusLabel.text = "Common"
commonProcessing()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func commonProcessing() {
// do your common processing
if nextControllerId == 0 {
performSegueWithIdentifier("next1Segue", sender: self)
} else {
performSegueWithIdentifier("next2Segue", sender: self)
}
}
}
import UIKit
class Next1ViewController: UIViewController {
#IBOutlet weak var statusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.statusLabel.text = "Next1"
next1Processing()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func next1Processing() {
println("Next 1 Processing")
}
}
import UIKit
class Next2ViewController: UIViewController {
#IBOutlet weak var statusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
statusLabel.text = "Next 2"
next2Processing()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func next2Processing() {
println("Next 2 Processing")
}
}
processing