//
// SecondViewController.swift
// Assignment 3
//
// Created by Tee Wen Seng on 7/14/19.
// Copyright © 2019 Tee Wen Seng. All rights reserved.
//
import UIKit
class SecondViewController: UIViewController, UIPickerViewDelegate,
UIPickerViewDataSource {
@IBOutlet weak var thePicker: UIPickerView!
@IBOutlet weak var selectWordButton: UIButton!
var filteredWords:Array<String> = []
var selectedWord:String!
override func viewDidLoad() {
super.viewDidLoad()
//Initialize the selected word to handle the case where user didn't move the
picker
selectedWord = filteredWords[0]
thePicker.dataSource = self
thePicker.delegate = self
//Styling the button
selectWordButton.layer.cornerRadius = 5.0
selectWordButton.layer.borderWidth = 1.0
selectWordButton.layer.borderColor = self.view.tintColor.cgColor
//Function to setup the picker
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component:
Int) -> Int {
return filteredWords.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent
component: Int) -> String? {
return filteredWords[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent
component: Int) {
selectedWord = filteredWords[row]
}
//Function when the button is pressed
@IBAction func donePressed(_ sender: UIButton) {
//Copy the selected word to clipboard
UIPasteboard.general.string = selectedWord
//Preparing for first view
let firstVC = self.presentingViewController as! ViewController
firstVC.wordsLabel.isHidden = false
firstVC.selectedTextLabel.isHidden = false
firstVC.selectedTextLabel.text = selectedWord
//Notify user the word has been copied
let alertController = UIAlertController(
title: "Copied",
message: "The selected word has been copied to clipboard",
preferredStyle: .alert
)
let okAction = UIAlertAction(
title: "OK",
style: .default,
handler: {(alert: UIAlertAction!) in self.dismiss(animated: true,
completion: nil)}
)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}