// 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.
Related
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...
I have a class called PickerFilterClass that access the YNDropDownView library and in there I create a pickerview in PickerFilterClass's extension. I want to the pickerview help sort a collectionview in another class called RestaurantDetails. I am trying to use the pickerview didselect function to access the collectionview called productCollection and reload the data when a user pickerview selects an row. I here is how I am trying to do this:
PickerFilter Class
class PickerFilterClass: YNDropDownView {
#IBOutlet var pickerView: UIPickerView!
var rotationAngle: CGFloat!
var filteredProducts = [Product]()
var products = [Product]()
let prodCollection: RestaurantDetails
let sections = [["Burgers", "Fries", "Cola", "Chicken", "Sides"],["Breakfast", "Lunch", "Dinner"]]
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.white
self.initViews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initViews()
}
#IBAction func confirmButtonClicked(_ sender: Any) {
self.normalSelected(at: 1)
self.hideMenu()
}
#IBAction func cancelButtonClicked(_ sender: Any) {
// self.changeMenu(title: "Changed", at: 1)
// self.changeMenu(title: "Changed", status: .selected, at: 0)
self.alwaysSelected(at: 1)
// self.alwaysSelected(at: 2)
// self.alwaysSelected(at: 3)
self.hideMenu()
}
override func dropDownViewOpened() {
print("Open Menu")
}
override func dropDownViewClosed() {
print("Close Menu")
}
func initViews() {
}
}
extension PickerFilterClass: UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return sections[component].count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
pickerView.sizeToFit()
return sections[component][row]
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let view = UIView()
view.frame = CGRect(x: 10, y: 20, width: 300, height: 52)
let label = UILabel()
label.frame = CGRect(x: 0, y: 0, width: 300, height: 52)
label.textAlignment = .center
label.font = UIFont(name: "Tiny.Toon", size: 11)
label.textColor = UIColor.purple
label.text = sections[component][row]
view.addSubview(label)
// view rotation
// view.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180))
return view
}
I am trying to use the pickerview to access this collection view here:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(sections[row])
if sections.count > row {
let selectedUsage = sections[component][row].lowercased()
if (selectedUsage == RestaurantDetails.allProductIdentifier.lowercased()) {
filteredProducts = products
} else {
filteredProducts = products.filter { ($0.usage?.lowercased() ?? "") == selectedUsage }
}
self.prodCollection.productCollection.reloadData()
}
}
}
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
}
I would like to add a function in my app where the user has to choose a category from a UIPickerView and upon clicking a button, the program would open the view selected from the categories in the UIPickerView. For example, if one of the chosen options on the UIPickerView is "About Page", I would like to have a program that would open the "About Page" upon a click of the button.
This is what the UI would look like.
But instead of the states - I would have the categories, I'm not sure how to set the values in the UIPickerView and open specific views.
Implement UIPickerViewDelegate method and give each VC a stroryboard Identifier
var lastId:String?
var ids = ["secondVC","thirdVC"] // picker data source and identifier names
func pickerView(_ pickerView: UIPickerView,
didSelectRow row: Int,
inComponent component: Int) {
lastId = ids[row]
}
#IBAction func goClicked(_ sender:UIButton) {
if let id = lastId {
let vc = storyboard.instantiateViewController(withIdentifier:id)!
self.present(vc, animated: true, completion: nil) // or push
}
else {
// select a vc alert
}
}
If you want to create the picker view programmatically - which I recommend the most - this solution should work for you:
class MyViewController : UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let pickerView = UIPickerView()
let titles = ["view1", "view2"]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
pickerView.delegate = self
pickerView.dataSource = self
view.addSubview(pickerView)
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return titles[row]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row == 0 {
let view1 = viewController1()
self.present(view1, animated: true, completion: nil)
}
if row == 1 {
let view2 = viewController2()
self.present(view2, animated: true, completion: nil)
}
}
}
class viewController1 : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
}
}
class viewController2 : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
}
}
hope that this works for you too ;)
This is what I'm trying to accomplish:
I want to take the value that the user selected from 2 different pickerViews and display that value in a textField
let drivingGear = ["1", "12", "36", "60", "84"]
let boxVal = chooseDrivingGear
let boxVal2 = chooseDrivenGear1
let newVal = boxVal / boxVal2
resultBox1.text = newVal
However I couldnt get that to work so I'm now trying this:
#IBAction func chooseBox1Pressed(_sender: UIButton) {
sender.isHidden = true
if chooseDrivingGear.isHidden {
chooseDrivingGear.isHidden = false
}
if chooseDrivingGear[row] == "1" {
let v1 = 1
}
but chooseDrivingGear[row] shows this error: use of unresolved identifier 'row'
(My other code for pickerview) UPDATE:
//first picker view
chooseDrivingGear.isHidden = true
chooseDrivingGear.delegate = self
chooseDrivingGear.dataSource = self
chooseDrivingGear.tag = 0
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
chooseBox1.setTitle(drivingGear[row], for: .normal)
chooseDrivingGear.isHidden = true
chooseBox1.isHidden = false
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return drivingGear[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return drivingGear.count
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
chooseDrivingGear.isHidden = false
chooseDrivingGear2.isHidden = false
chooseDrivingGear3.isHidden = false
chooseDrivenGear1.isHidden = false
chooseDrivenGear2.isHidden = false
chooseDrivenGear3.isHidden = false
return false
}
You can simply try getting the selectedRow from both the pickerViews, get the values for the selected rows and the perform calculation on the values as required to show in the textField.
I have provided a sample code for that below.
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
{
#IBOutlet weak var pickerView1: UIPickerView!
#IBOutlet weak var pickerView2: UIPickerView!
#IBOutlet weak var textField: UITextField!
let drivingGear = ["1", "12", "36", "60", "84"]
override func viewDidLoad()
{
super.viewDidLoad()
}
#IBAction func showResult(_ sender: UIButton)
{
let val1 = self.drivingGear[self.pickerView1.selectedRow(inComponent: 0)]
let val2 = self.drivingGear[self.pickerView2.selectedRow(inComponent: 0)]
if let intVal1 = Int(val1), let intVal2 = Int(val2)
{
let result = intVal1 / intVal2
self.textField.text = String(result)
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return self.drivingGear.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return self.drivingGear[row]
}
}
Let me know if you still face any issues.