0% found this document useful (0 votes)
29 views2 pages

Second View Controller

This document contains the code for a SecondViewController class written in Swift. The class is a view controller that contains a UIPickerView for selecting a word from a filteredWords array. It implements the UIPickerViewDelegate and UIPickerViewDataSource protocols to populate and manage the picker. When the "Done" button is pressed, it copies the selected word to the clipboard, updates labels on the previous view controller, and displays an alert to notify the user.

Uploaded by

wensengtee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Second View Controller

This document contains the code for a SecondViewController class written in Swift. The class is a view controller that contains a UIPickerView for selecting a word from a filteredWords array. It implements the UIPickerViewDelegate and UIPickerViewDataSource protocols to populate and manage the picker. When the "Done" button is pressed, it copies the selected word to the clipboard, updates labels on the previous view controller, and displays an alert to notify the user.

Uploaded by

wensengtee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//

// 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)
}

You might also like