implementing Replace and Insert Buttons in Xcode/Swift - swift

I am new to xCode and confused about implementing the following two things to the replace button and insert button. Can someone please helpWhen you press the
“replace” button, the item selected in the picker should be replaced by the contents of the text field (blank if
nothing in the text field). When you press “insert”, the contents of the text field should be added to the data for
the picker, after the item currently selected, and the new, added, item should be shown in the picker as the
selected item (so the previously selected item would be immediately above it on the picker’s display at that
point).
class ViewController: UIViewController {
#IBOutlet weak var countryTextField: UITextField!
#IBOutlet weak var selectButton: UIButton!
#IBOutlet weak var replaceButton: UIButton!
#IBOutlet weak var insertButton: UIButton!
let countries = ["USA", "MEXICO", "CANADA"]
var pickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
countryTextField.inputView = pickerView
countryTextField.textAlignment = .center
}
}
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countries.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return countries[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
countryTextField.text = countries[row]
countryTextField.resignFirstResponder()
}
}

Create #IBAction for your Buttons from xib and add conditions accordingly. It will look like these-
#IBAction func insertAction(sender: UIButton){
// write code for insert
}
#IBAction func replaceAction(sender: UIButton){
// write code for replace
}

Related

Swift UIPickerView defaults to last option regardless of selected option

I have a UIPickerView that when tapping the done button, keeps selecting the last option of the Array. I can tap on either the UITextField or a button I'm using that assigns the UITextField as the first responder. My code is as follows:
Edit. I should also mention that I'm using Xcode 11 GM Seed if that was to make a difference. I tested a clean version of the code shown below on a separate project with nothing else and it is displaying the same behavior.
#IBOutlet weak var sortTypeTextField: UITextField!
var selectedSortOption = String()
let sortOptions = [
USER_REVIEWS_SORT_OPTION_DATE,
USER_REVIEWS_SORT_OPTION_LIKES,
USER_REVIEWS_SORT_OPTION_DISLIKES
]
sortOptions above is an array of Strings located in a different file but for the purpose of this question I’ll add here:
let USER_REVIEWS_SORT_OPTION_DATE = "Date"
let USER_REVIEWS_SORT_OPTION_LIKES = "Number of Likes"
let USER_REVIEWS_SORT_OPTION_DISLIKES = "Number of Dislikes"
let DONE_TOOLBAR_BUTTON_TITLE = "Done"
I have a function setupUI which I call in viewDidLoad along with the value for the sortTypeTextField. The value is the first String in the array when the viewController first loads.
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI() {
sortTypeTextField.text = sortOptions[0]
createSortPicker()
createPickerToolbar()
}
func createSortPicker() {
let sortPicker = UIPickerView()
sortPicker.delegate = self
sortTypeTextField.inputView = sortPicker
}
func createPickerToolbar() {
let toolBar = UIToolbar()
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: DONE_TOOLBAR_BUTTON_TITLE, style: .plain, target: self, action: #selector(dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
sortTypeTextField.inputAccessoryView = toolBar
}
#objc override func dismissKeyboard() {
view.endEditing(true)
}
I’m also using a button to display the UIPicker as shown here:
#IBAction func sortRatingsButtonWasTapped(_ sender: Any) {
sortTypeTextField?.becomeFirstResponder()
}
As shown below, I want for the sortTypeTextField value to change to the selected option but for some reason when tapping the Done button in the toolbar, the text shown is always the third String in the array regardless if I have chosen the first, the second or the third.
extension ReviewsViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
sortOptions.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return sortOptions[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedSortOption = sortOptions[row]
sortTypeTextField.text = selectedSortOption
}
}
It seems to be a bug in the simulator. I tested the my code above and the code provided by Abdul Karim Khan on my device and the work as they should. The label updates according to the option selected in the picker view.
This is how I created pickerView,
class ViewController: UIViewController {
#IBOutlet weak var selectaday: UITextField!
let days = ["monday","wednesday"]
var selectedDay: String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createDayPicker()
}
func createDayPicker(){
let dayPicker = UIPickerView()
dayPicker.delegate = self
selectaday.inputView = dayPicker
}
}
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource{
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return days.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return days[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedDay = days[row]
selectaday.text = selectedDay
}
}

Using picker view with UIButton to open a new TableView

I have a picker view and tableviews for each picker row programmed and I'd like to connect them via a UIButton. Any suggestions for a swift function to connect the Pickerview row with the botton to open that picker's table? (code below)
Thanks!
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var Picker1: UIPickerView!
var Array = ["G: Classroom friendly", "PG-13: Swear words OK(no sexual innuendo)", "R: Anything goes!"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Picker1.delegate = self
Picker1.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return Array[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Array.count
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
#IBAction func Submit(_ sender: Any) {
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
#IBAction func ContinueBotton(_ sender: UIButton) {
}
}
class ViewController1: UITableViewController {
#IBOutlet var ViewController1: UITableView!
var GRating = [GN]()

problems at nested hierarchy view using storyboard in Swift

I'm beginner at Swift, so currently i organize most part of my app using storyboard.
I want to construct some view scrollable that contains textField and pickerView.
and each of them is stacked View, and then it is child view of view A.
and view A is child view of scrollView.
help you to understand more, i'll attach my app structure.
Anyway, when i run my apps, it represent well i want to achieve, however, coudn't get any touch reaction.
keyboard isn't popup when i touch textField, pickerView scroll isn't working. I think it's because of nested view, but have no idea how to solve.
plz help! And also glad give some advice learn Swift.
Thx in advance.
* edit 1) attach my code *
import UIKit
class ViewController:
UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
{
#IBOutlet var outerView: UIView!
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var statePicker: UIPickerView!
#IBOutlet weak var innerView: UIView!
#IBOutlet weak var nameInputer: UITextField!
let states = ["Alaska", "Arkansas", "Alabama", "California", "Maine", "New York"]
override func viewDidLoad() {
super.viewDidLoad()
var scrollViewHeight:CGFloat = 0.0
for view in self.view.subviews as [UIView] {
scrollViewHeight += view.frame.size.height;
}
scrollView.contentSize = CGSize(width: view.frame.width, height: scrollViewHeight)
scrollView.contentInset = UIEdgeInsetsMake(20, 0, 20, 0)
statePicker.dataSource = self
statePicker.delegate = self
statePicker.isExclusiveTouch = true
scrollView.canCancelContentTouches = false
innerView.sendSubview(toBack: outerView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return states.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return states[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
statePicker.isHidden = true
}
}
* EDIT 2 : attach my story board *

making a drop-down list in Swift 3

I'm trying to make drop-down list where picker view is not visible until user clicks on textfield. Then picker view disappears after user makes selection from pickerView. Currently, the picker View does not show up at all when I click on textfield.
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var textBox: UITextField!
#IBOutlet weak var pickerView: UIPickerView!
var choices = ["SortBy:", "Date(ascending)", "Date(descending)","Calories Burned(descending)","Duration(descending)"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return choices[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return choices.count
}
fun pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.textBox.text = self.choices[row]
self.pickerView.isHidden = true
print("hello")
}
func textFieldDidBeginEditing(TextField: UITextField){
if TextField == self.textBox {
self.pickerView.isHidden = false
TextField.endEditing(true)
}
}
override func viewDidLoad() {
self.pickerView.isHidden = true
super.viewDidLoad()
}
}
try this :
override func viewDidLoad() {
self.pickerView.isHidden = true
super.viewDidLoad()
self.textBox.delegate = self
self.pickerView.delegate = self
}

How to connect UIPickerView + Button + call dictionary to display in label

I have a label, a picker (years), and a button. I want to be able to select the year on the picker, hit the button, and then display data associated with the year (a dictionary). Here's my code so far
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
#IBOutlet weak var picker: UIPickerView!
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent
component: Int) -> Int {
return years.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int,
forComponent component: Int) -> String! {
return years[row]
}
/* func pickerView(pickerView: UIPickerView!, didSelectRow row: Int,
inComponent component: Int) {
label.text = "\(years[row])"
}
*/
#IBAction func button(sender: UIButton) {
label.text =
}
here is my Model:
let years = ["2015", "2014", "2013", "2012"]
let winners = [
2015: "Patriots",
2014: "Seahawks",
2013: "Ravens",
2012: "Giants"
]
You need to do a few things to make this work. First, make your viewController conform to the UIPickerViewDataSource and UIPickerViewDelegate and set your picker's data source and delegate in viewDidLoad. Then you'll want to use the delegate method pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) to respond to changes to the pickerView. Store the selection in an instance variable and you should be good to go:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var label: UILabel!
#IBOutlet weak var picker: UIPickerView!
let years = [2015, 2014, 2013, 2012]
let winners = [
2015: "Patriots",
2014: "Seahawks",
2013: "Ravens",
2012: "Giants"
]
var selectedYear: Int?
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return years.count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedYear = years[row]
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return "\(years[row])"
}
#IBAction func button(sender: UIButton) {
if let year = selectedYear {
if let winner = winners[year] {
label.text = winner
}
}
}
}