Dropdown menu in a dropdown menu swift - swift

So I have created a dropdown menu in Swift. When I press a button a dropdown menu appears, but my problem is that I don't know how to add another dropdown menu for my options from my first dropdown.
this is my current code for my dropdown:
import UIKit
class ViewController: UIViewController {
#IBOutlet var skolButtons: [UIButton]! // buttons for different schools
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 handleSelection(_ sender: UIButton) {
skolButtons.forEach { (button) in
button.isHidden = !button.isHidden // Hides button and = ! is the oppesite
self.view.layoutIfNeeded()
}
}
#IBAction func itgTap(_ sender: UIButton) { // what happens when you tap different schools
}
}
So if anyone knows how to create a sub dropdown please help me!

Related

Swift making labels disappear when button is clicked

I would like to make a label disappear and reappear with a click of a button and then reappear with the click of the button again, to reduce clutter on screen.
The following code when it was first run made the button disappear. Which I wanted it to do, but when I closed it off and then ran it again and hit the button the code all of a sudden didn't work meaning that my label didn't disappear. I have not changed any of the code; all I did was run it again.
I am doing this as part of a group project at school. If anyone can help me with my problem that would be great. Thanks.
P.S. PrepInfo is the button I'm trying to hide and PrepButtton is the button that when pressed, I want to make the PrepInfo label disappear.
import UIKit
class BackgroundViewController: UIViewController {
#IBAction func PrepButton(sender: AnyObject) {
func hide() {
PrepInfo.hidden = false
}
hide()
}
#IBOutlet weak var PrepInfo: UILabel!
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.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Try this, if your label is called myLabel:
// In the code you write to handle button tap
myLabel.hidden = true

Reveal Passcode dots using Swift

How do I reveal the passcode dots in swift, so then the user can see their password by clicking a button.
Thanks
Tyge
TextField has a property called secureTextEntry. All you need is to connect an outlet to your textfield and toggle secureTextEntry:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var passwordField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
passwordField.secureTextEntry = true
// 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 showHide(sender: UIButton) {
passwordField.secureTextEntry = !passwordField.secureTextEntry
sender.setTitle({passwordField.secureTextEntry ? "Show":"Hide"}(), forState: .Normal)
}
}
stackoverflow provides a working search engine, but anyway here you is the answer.
Do the same, but with false instead of true
Obscure a UITextField password
By the way make sure that the user knows what he does when reveal the secure text.

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

Hide ios 8 sidebar 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.