I'm trying populate a pickerview using the text from labels.
I've tried searching online and in books for how to do this but no luck yet.
what i have so far shows no errors but still won't run
thanks
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate{
#IBOutlet weak var label3: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label: UILabel!
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var textField2: UITextField!
#IBOutlet weak var textField3: UITextField!
#IBAction func button(sender: UIButton) {
label.text = textField.text
label2.text = textField2.text
label3.text = textField3.text
self.textField.resignFirstResponder()
self.textField2.resignFirstResponder()
self.textField3.resignFirstResponder()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
var pickerLabels: [UILabel!] {
return [label,label2,label3]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component:Int) -> Int{
return pickerLabels.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> NSArray{
return pickerLabels
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You are sending UILabels to your array.
I believe what you are trying to achieve needs you to get the text from the UILabels and also to format the array as [String] on your computed property..
var pickerLabels: [String!] {
return [label.text, label2.text, label3.text]
}
Related
I'm testing the my add button in the view controller file. It should go to validateName function in my model file and which returns true and then enables my addButton. But when I click on "Add" it doesn't change the text of the UITextView.
In viewcontroller.swift:
import UIKit
class ViewController: UIViewController,UITextFieldDelegate{
#IBOutlet weak var nameInputText: UITextField!
#IBOutlet weak var usernameInputText: UITextField!
#IBOutlet weak var passwordInputText: UITextField!
#IBOutlet weak var phoneInputText: UITextField!
#IBOutlet weak var emailInputText: UITextField!
#IBOutlet weak var addButton: UIButton!
#IBOutlet weak var textviewText: UITextView!
let model = Model()
#IBAction func addButton(_ sender: UIButton) {
textviewText.text = "hi"
}
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String)
-> Bool
{
addButton.isEnabled = false
let text: String = nameInputText.text!
var test_name: Bool
var name_array = [String]()
test_name = model.validateName(nametext: text)
if test_name == true{
addButton.isEnabled = true
name_array.append(text)
return true
}
return (true)
}
override func viewDidLoad() {
super.viewDidLoad()
usernameInputText.delegate = self
nameInputText.delegate = self
passwordInputText.isSecureTextEntry = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool{
usernameInputText.resignFirstResponder()
nameInputText.resignFirstResponder()
return (true)
}
}
//In model.swift:
import Foundation
class Model {
func validateName(nametext: String) -> Bool{
return true
}
}
I tried to program a sign up view controller and use a UIPickerView for country choice. The issue is that when I tried to pass the picker country to Parse to save the input, Xcode give me an error. Why Xcode can't allow me to use the selected data to Parse? The error is in the last line.
Any help guys?
#IBOutlet var usernameTextField: UITextField!
#IBOutlet var fullnameTextField: UITextField!
#IBOutlet var emailTextField: UITextField!
#IBOutlet var passwordTextField: UITextField!
#IBOutlet var phonenumberTextField: UITextField!
#IBOutlet var countryPicker: UIPickerView!
let countryData: [String] = ["Saudi Arabia", "Turkey"]
#available(iOS 2.0, *)
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countryData.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let countrySelect = countryData[row]
print(countrySelect)
}
override func viewDidLoad() {
super.viewDidLoad()
countryPicker.dataSource = self
countryPicker.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func signUpAction(sender: AnyObject) {
// Declare user object
let newUser = PFUser()
let fullName = fullnameTextField.text!
let username = usernameTextField.text!
let email:String = emailTextField.text!
let password:String = passwordTextField.text!
let phoneNumber:Int? = Int(phonenumberTextField.text!)
// Passing arguments
newUser.username = username
newUser["fullName"] = fullName
newUser.email = email
newUser.password = password
newUser["phoneNumber"] = phoneNumber! as Int
newUser["country"] = countryData [row]
Based on your comment that the error is "unresolved identifier row":
You are never declaring a variable named row, yet you're using it. You can either replace the use of this undefined variable with the correct array index that will give you the country from the countryData variable OR you make sure that row is actually declared and accessible in your function.
According to everything I've read here, I should override touchesBegan() to dismiss the keyboard (in my case DatePicker). Unfortunately touching the screen does not dismiss the DatePicker. Touching other UI elements like the UISteppers dismisses the keyboard just fine, and it is using the same function of CloseKeyboard()
class RecordWorkoutTableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var dateTextField: UITextField!
#IBOutlet weak var weightLabel: UILabel!
#IBOutlet weak var setOneLabel: UILabel!
#IBOutlet weak var setTwoLabel: UILabel!
#IBOutlet weak var weightStepper: UIStepper!
#IBOutlet weak var setOneStepper: UIStepper!
#IBOutlet weak var setTwoStepper: UIStepper!
var newDate: NSDate? {
didSet {
dateTextField.text = NSDateToPrettyString(newDate!)
}
}
var newWeight: Int? {
didSet {
weightLabel.text = "\(newWeight!) lbs"
}
}
var newSetOne: Int? {
didSet {
setOneLabel.text = "Set 1: \(newSetOne!) reps"
}
}
var newSetTwo: Int? {
didSet {
setTwoLabel.text = "Set 2: \(newSetTwo!) reps"
}
}
var workout: Workout?
// MARK: UITextFieldDelegate
func textFieldDidBeginEditing(textField: UITextField) {
let datePicker = UIDatePicker()
textField.inputView = datePicker
datePicker.addTarget(self, action: #selector(RecordWorkoutTableViewController.datePickerChanged(_:)), forControlEvents: .ValueChanged)
}
func datePickerChanged(sender: UIDatePicker) {
newDate = sender.date
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
// disable editing of date text. datepicker input only
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
return false
}
// MARK: Helper Functions
func closeKeyboard() {
self.view.endEditing(true)
}
// MARK: Touch Events
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
closeKeyboard()
}
override func viewDidLoad() {
super.viewDidLoad()
dateTextField.delegate = self
newDate = NSDate()
if let lastWorkout = workout {
newWeight = lastWorkout.sets[0].weight
newSetOne = lastWorkout.sets[0].repCount
newSetTwo = lastWorkout.sets[1].repCount
} else {
newWeight = 0
newSetOne = 9
newSetTwo = 8
}
weightStepper.stepValue = 5
weightStepper.maximumValue = 995
weightStepper.value = Double(newWeight!)
setOneStepper.stepValue = 1
setOneStepper.maximumValue = 20
setOneStepper.value = Double(newSetOne!)
setTwoStepper.stepValue = 1
setTwoStepper.maximumValue = 20
setTwoStepper.value = Double(newSetTwo!)
}
#IBAction func weightStepperChanged(sender: UIStepper) {
newWeight = Int(sender.value)
closeKeyboard()
}
#IBAction func setOneStepperChanged(sender: UIStepper) {
newSetOne = Int(sender.value)
closeKeyboard()
}
#IBAction func setTwoStepperChanged(sender: UIStepper) {
newSetTwo = Int(sender.value)
closeKeyboard()
}
}
You said in the comments that you have another class in your app named Set. Since this class is in the same module as your table view controller, Swift is prioritizing it over it's built in class.
You can fix this by either renaming your Set class, or explicitly specifying the Swift module in the function declaration:
override func touchesBegan(touches: Swift.Set<UITouch>, withEvent event: UIEvent?) {
closeKeyboard()
}
My problem is that when I run the project and click the first textfield ( the others are empty for now ) the app crashes with this error :
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x12dd4c600> should have parent view controller:<Sai_Service.ServiceAppointment: 0x12dd151a0> but requested parent is:<UIInputWindowController: 0x12e00b600>'.
This is my code:
import UIKit
class ServiceAppointment: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var ScrollView: UIScrollView!
#IBOutlet weak var pickerView: UIPickerView!
#IBOutlet weak var StateField: UITextField!
#IBOutlet weak var CityField: UITextField!
#IBOutlet weak var VehRegNumberField: UITextField!
#IBOutlet weak var LocationField: UITextField!
#IBOutlet weak var CurrentKmsField: UITextField!
#IBOutlet weak var apptDateFIeld: UITextField!
#IBOutlet weak var apptTypeField: UITextField!
#IBOutlet weak var pickupField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
ScrollView.contentSize.height = 1000
pickerView.hidden = true;
StateField.text = States[0]
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
self.StateField.inputView = pickerView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var States = ["Italy","United States","UK","France"]
//MARK: - Delegates and data sources
//MARK: Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return States.count
}
//MARK: Delegates
func pickerView2(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return States[row]
}
func pickerView3(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
StateField.text = States[row]
pickerView.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
pickerView.hidden = false
return false
}
}
I think it's a problem of multiple view, so I wrote in the viewDidLoad:
self.StateField.removeFromSuperView()
but then all the ScrollViews disappeared obviously.
How I can make this work so that when I press the textfield, the pickerview appears?
I've been trying to segue some labels across to another view controller but i can't seem to work it out.
my first view controller:
import UIKit
class firstViewController: UIViewController,UITextFieldDelegate {
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
#IBOutlet weak var label4: UILabel!
#IBOutlet weak var label5: UILabel!
#IBOutlet weak var label6: UILabel!
var pickerLabels: [String!] {
return [label1.text,label2.text,label3.text,label4.text,label5.text,label6.text]
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let controller = segue.destinationViewController as thirdViewController
controller.vc2Labels = pickerLabels //pickerLabels shows error
}
}
the labels in here are what I'm trying to segue but "pickerLabels" keeps coming up with an error
my second view controller:
import UIKit
class secondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var vc2Labels: [String]!
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component:Int) -> Int{
return vc2Labels.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
return vc2Labels[row]
}
I think I know your problem. You seem to be passing in an array of String into a string object.
Maybe you can alter your second view controller's variable to be:
var vc2Labels: [String]!
but I wouldn't recommend using implicit unwrapping of optional, since it allows a passing in of nil. You can declare an empty array like this.
var vc2Labels = [String]()
Hope this helps. More context would be helpful.