PickerView will reload by itself with wrong behavior Swift 4.2 - swift

My pickerView was going very well, but don't know why just get wrong behavior after rebuild the project folder.
The component[0] will reload itself but I didn't write any code for it!
Below are the codes:
DropdownListExtension.swift
extension UITextField: UITextFieldDelegate {
func loadAddressDropdownData(data: [String], zipField: UITextField!) {
self.inputView = AddressPickerView(pickerData: data, cityField: self, zipField: zipField)
// ToolBar
let toolBar = UIToolbar()
// Adding Button ToolBar
// ...Init ToolBar
self.inputAccessoryView = toolBar
}
#objc func doneClick() {
self.resignFirstResponder()
}
#objc func cancelClick() {
self.resignFirstResponder()
}
}
AddressPickerView.swift
class AddressPickerView : UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate {
var pickerData : [String]!
var pickerTextField : UITextField!
var zipTextField: UITextField!
var selectedRow = 0
var selectedCity = "臺北市"
init(pickerData: [String], cityField: UITextField, zipField: UITextField) {
super.init(frame: CGRect.zero)
self.pickerData = pickerData
self.pickerTextField = cityField
self.zipTextField = zipField
self.delegate = self
self.dataSource = self
DispatchQueue.main.async{
if pickerData.count > 0 {
self.pickerTextField.text = self.pickerData[0]
} else {
self.pickerTextField.text = nil
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0{
return pickerData.count
}else {
switch selectedCity{
//...
default:
return 0
}
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0{
return pickerData[row]
} else {
switch selectedCity{
//...
default:
return ""
}
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(component)
if component == 0 {
pickerTextField.text = pickerData[row]
selectedRow = row
selectedCity = pickerData[row]
pickerView.reloadComponent(1)
}else {
switch selectedCity{
case "臺北市":
zipTextField.text = taipei[row]
case "基隆市":
zipTextField.text = keelung[row]
//...
default:
return print("")
}
}
}
}
Please help me for fix it, I spent almost 3 das but still cannot figure out the problem...

Related

Using enums case as segment control to show types of data in pickerview

I am new in swift and I was thinking a way to populate data ideally with segment control which I did not attempt before.
VC has layout below
CategoryType Segment Control : to control CategoryType)
CategoryTextField with picker function : keyboard will show list of data from category
Expecting result of selected CategoryType Segment Control to show list of data based on CategoryType on pickerview
This code is in trial and error mode, as I did not have an exact idea on how to execute the result I wish to obtain.
func appendDefaultCategoryTypes() {
categories = realm.objects(Category.self)
if categories.count == 0 {
try! realm.write() {
let defaultCategories = [
Category(type: .Expense, name: "EXPENSE 1"),
Category(type: .Expense, name: "EXPENSE 2"),
Category(type: .Income, name: "INCOME 1"),
Category(type: .Income, name: "INCOME 2"),
]
realm.add(defaultCategories)
}
}
}
//MARK: - Transaction Section
class Transaction : Object {
//Child of Transaction
let parentAccount = LinkingObjects(fromType: Account.self, property: "ofTransactions")
#objc dynamic var categoryType : Category?
#objc dynamic var amount : String = ""
#objc dynamic var date : String = ""
}
//MARK: - Transaction Category Section
enum CategoryType : String, CaseIterable {
case Income = "Income"
case Expense = "Expense"
static let allValues = [Income, Expense]
init?(id : Int) {
switch id {
case 1:
self = .Income
case 2:
self = .Expense
default:
return nil
}
}
}
class Category : Object {
#objc dynamic var type : String = CategoryType.Income.rawValue
#objc dynamic var name : String = ""
convenience init(type:CategoryType, name: String) {
self.init()
self.type = type.rawValue
self.name = name
}
}
//VC
var categories : Results<Category>!
var picker = UIPickerView()
#IBAction func categoryTypeSC(_ sender: UISegmentedControl) {
guard let selectedCategoryType = CategoryType.(rawValue: sender.selectedSegmentIndex) else {
fatalError("no corresponding category type for the index selected by segment control")
}
switch selectedCategoryType {
case .income :
print("Income in SC selected")
case .expense :
print("Expense in SC selected")
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
// if categorySCoutlet.selectedSegmentIndex == 0 {
// return CategoryType.income.count
// } else if categorySCoutlet.selectedSegmentIndex == 1 {
// return CategoryType.expense.count
// }
return categories.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
// if categorySCoutlet.selectedSegmentIndex == 0 {
// return
// } else if categorySCoutlet.selectedSegmentIndex == 1 {
// return
// }
// return "None"
return categories[row].name
}
In your view controller you need to keep track of the categories that correspond to the type indicated by the segmented control. I call it currentCategories.
class ViewController: UIViewController {
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBOutlet weak var textField: UITextField!
var categories: Results<Category>!
var currentCategories: Results<Category>!
lazy var pickerView: UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
let realm = try! Realm()
categories = realm.objects(Category.self)
appendDefaultCategoryTypes()
currentCategories = categories.filter("type == %#", CategoryType.income.rawValue)
textField.text = currentCategories.first?.name
textField.inputView = pickerView
pickerView.delegate = self
pickerView.dataSource = self
segmentedControl.addTarget(self, action: #selector(onCategoryTypeChanged(_:)), for: .valueChanged)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if touches.first?.view == view {
textField.resignFirstResponder()
}
}
}
When the segmented control value changes you need to refresh the picker so that the contents reflect the selected category type.
extension ViewController {
#IBAction func onCategoryTypeChanged(_ sender: UISegmentedControl) {
guard let type = CategoryType(id: sender.selectedSegmentIndex) else {
fatalError("no corresponding category type for the index selected by segment control")
}
currentCategories = categories.filter("type == %#", type.rawValue)
textField.text = currentCategories.first?.name
pickerView.reloadAllComponents()
pickerView.selectRow(0, inComponent: 0, animated: true)
}
}
In your picker data source and delegate methods you need to reference data from the categories that reflect the current type.
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return currentCategories.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return currentCategories[row].name
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
textField.text = currentCategories[row].name
}
}
Note that I took the liberty of changing a couple of things in your CategoryType enum. Indexes should start at zero, and cases should be lowercased.
enum CategoryType : String, CaseIterable {
case income = "income"
case expense = "expense"
init?(id : Int) {
if id < CategoryType.allCases.count {
self = CategoryType.allCases[id]
} else {
return nil
}
}
}

How to change UIPickerViewData when I select the different UITextField?

I want to show a UIPickerView when tapping a UITextField.
And I made a 'place' UITextField with a UIPickerView.
But, I don't know how to make another UITextField with a UIPickerView.
I tried to use 'switch', but UITextField was not a value, so I couldn't do it with 'switch'.
I want to change UIPickerViewData when I select the different UITextField.
How can I do?
Here's the codes.
Thank you!
#IBOutlet weak var place: UITextField!
#IBOutlet weak var product: UITextField!
#IBOutlet weak var number: UITextField!
let placeArray = ["A", "B", "C", "D", "E", "F"]
let productArray = ["Apple", "Banana", "Grape"]
let numberArray = ["1", "2", "3", "4", "5", "6"]
var pickerView = UIPickerView()
var textField: UITextField!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return placeArray.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
place.text = placeArray[row]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return placeArray[row]
}
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(displayPickerView))
tapGesture.numberOfTapsRequired = 1
place.addGestureRecognizer(tapGesture)
}
#objc private func displayPickerView() {
if textField == nil {
self.textField = UITextField(frame: .zero)
textField.inputView = self.pickerView
self.view.addSubview(textField)
}
textField.becomeFirstResponder()
}
Very simple way to do this.
var placePickerView = UIPickerView()
var productPickerView = UIPickerView()
var numberPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
placePickerView.delegate = self
placePickerView.dataSource = self
place.inputView = placePickerView
productPickerView.delegate = self
productPickerView.dataSource = self
product.inputView = productPickerView
numberPickerView.delegate = self
numberPickerView.dataSource = self
number.inputView = numberPickerView
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var numberOfRows: Int = 1
if pickerView == placePickerView {
numberOfRows = placeArray.count
} else if pickerView == productPickerView {
numberOfRows = productArray.count
} else if pickerView == numberPickerView {
numberOfRows = numberArray.count
}
return numberOfRows
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == placePickerView {
place.text = placeArray[row]
} else if pickerView == productPickerView {
productType.text = productArray[row]
} else if pickerView == numberPickerView {
transactionType.text = numberArray[row]
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
var title: String?
if pickerView == placePickerView {
title = placeArray[row]
} else if pickerView == productPickerView {
title = productArray[row]
} else if pickerView == numberPickerView {
title = numberArray[row]
}
return title
}

result from multiple UIPickers

I work in a project that has two pickers and one button.
When the user chooses from the first and second pickers
he click on the button .. and show the result in the textfield ..
for example:
if he chooses from the first picker "Jed" and from the second picker "Ahmed"..
now if he clicks the button the result will be in the textfield shown "OK" ..
this is what I did so far .. I don't know why its crashing when I click on the button ..
import UIKit
class ViewController: UIViewController , UIPickerViewDelegate , UIPickerViewDataSource {
#IBOutlet weak var theResult: UITextField!
#IBOutlet weak var cityTxt2: UITextField!
#IBOutlet weak var cityTxt: UITextField!
var city = ["Jed" , "Med" , "Ruh"]
var city2 = ["ahmed" , "mohammed" , "mustafa"]
let picker = UIPickerView()
let picker2 = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
picker2.delegate = self
picker2.dataSource = self
cityTxt.inputView = picker
cityTxt2.inputView = picker2
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if (pickerView == self.picker) {
return city.count
}
else if (pickerView == self.picker2) {
return city2.count
}
return city.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView == self.picker) {
return city[row]
}
else if (pickerView == self.picker2) {
return city2[row]
}
return city[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == picker) {
self.cityTxt.text = self.city[row]
}
else if (pickerView == picker2) {
self.cityTxt2.text = self.city2[row]
}
self.view.endEditing(false)
}
#IBAction func getTheResult(_ sender: Any) {
let R1 = cityTxt.text
let R2 = cityTxt2.text
if (R1 == "Jed") && (R2 == "ahmed"){
theResult.text = "OK"
}else{
theResult.text = ""
}
}
}
I think the problem is in the Button
because when I click on it .. the program will crash ..

How Click Button close alertController and get data

// call class in UIViewController
label = TimePickerView()
alertController = UIAlertController(title: " \n\n\n\n\n\n\n\n\n\n", message: "", preferredStyle: UIAlertControllerStyle.alert)
alertController.view.addSubview(label)
// alertController.view.addSubview(samplePicker)
self.present(alertController, animated: true, completion: nil)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
import UIKit
class TimePickerView: UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate {
var hour:Int = 0
var minute:Int = 0
var samplePicker: UIPickerView = UIPickerView()
var sampleSegment:UISegmentedControl = UISegmentedControl ()
var alertController:UIAlertController = UIAlertController()
required internal init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.setup()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
func setup(){
self.delegate = self
self.dataSource = self
samplePicker = UIPickerView(frame: CGRect(x:10.0, y:40.0, width:10, height:150))
samplePicker.delegate = self
samplePicker.dataSource = self
samplePicker.showsSelectionIndicator = true
samplePicker.tintColor = UIColor.red
samplePicker.reloadAllComponents()
sampleSegment = UISegmentedControl(items: NSArray(object: "تایید") as [AnyObject])
sampleSegment.isMomentary = true
sampleSegment.frame = CGRect(x:0, y:0,width:270.0, height:30.0)
sampleSegment.tintColor = UIColor.black
sampleSegment.backgroundColor = UIColor.gray
sampleSegment.addTarget(self, action: #selector(TimePickerView.dismissAlert), for: UIControlEvents.valueChanged)
self.addSubview(sampleSegment)
self.addSubview(samplePicker)
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch component {
case 0:
self.hour = row
print(row)
case 1:
self.minute = row
default:
print("No component with number \(component)")
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return 24
}
return 60
}
private func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> Int {
return 30
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return String(row)
}else {
return String(row)
}
}
func dismissAlert(){
alertController.dismiss(animated: true, completion: nil)
}
}
Use AddAction, an instance property of UIAlertController.

Update UIPickerView rows when selecting new UITextField

I have created a pickerView that uses 3 different arrays to populate 3 textfields but when I tap on one textfield, select a row, and then directly tap another textfield, my pickerView does not update to the information in the array for that textfield.
If I tap on a textfield then tap away to dismiss the pickerview and then tap another textfield it updates and works fine.
var teams = [String]()
var schedules = ["A","B"]
var services = ["9AM","12PM","5PM"]
var pickerView:UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
teamTextField.inputView = self.pickerView
serviceTextField.inputView = self.pickerView
scheduleTextField.inputView = self.pickerView
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if teamTextField.isFirstResponder() {
return teams.count
}
else if scheduleTextField.isFirstResponder() {
return schedules.count
}
else {
return services.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if teamTextField.isFirstResponder() {
return teams[row]
}
else if scheduleTextField.isFirstResponder() {
return schedules[row]
}
else {
return services[row]
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if teamTextField.isFirstResponder() {
let itemSelected = teams[row]
teamTextField.text = itemSelected
}
else if scheduleTextField.isFirstResponder() {
let itemSelected = schedules[row]
scheduleTextField.text = itemSelected
}
else if serviceTextField.isFirstResponder() {
let itemSelected = services[row]
serviceTextField.text = itemSelected
}
}
func textFieldDidBeginEditing(textField: UITextField) {
pickerView.reloadAllComponents()
}
That should be all the necessary code you need. If you need anything else, let me know.
Try to add this:
#IBAction func textFieldDidBeginEditing(sender: AnyObject) {
pickerView.reloadAllComponents()
}
and connect it to the 'Editing Did Begin' sent events of your UITextField's