Frage zum erstellen eines UIPicker's

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Frage zum erstellen eines UIPicker's

    Guten Tag,
    bin gerade dabei, meine erste kleine App zu erstellen, also noch relativ am Anfang mit Swift :/

    Ich möchte einen Picker durch das klicken auf ein Textfield erstellen. Das klappt soweit auch, leider übernimmt das Textfield nur den Text aus dem Picker nicht. Wäre super wenn jemand aus dem Forum mir weiterhelfen könnte!

    Hier der Code:
    import UIKit

    class JobErstellenViewController: UIViewController, UITextFieldDelegate {


    @IBOutlet var jobAuswählenTextField: UITextField!
    var jobKategorien = ["Haus und Garten", "Betreuung", "Sonstiges"]
    var selectedJob: String?


    override func viewDidLoad() {
    super.viewDidLoad()
    createPickerView()
    createJobToolBar()
    }

    //Folgendes für die Erstellung des PickerViews
    func createPickerView() {
    let jobPicker = UIPickerView()
    jobPicker.delegate = self
    jobPicker.dataSource = self
    jobAuswählenTextField.inputView = jobPicker
    }

    //Folgendes für die Verbidung des PickerViws mit den angegebenen Daten
    extension JobErstellenViewController: UIPickerViewDelegate, UIPickerViewDataSource {

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return jobKategorien.count
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return jobKategorien[row]
    }
    func pickerView(_pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    jobAuswählenTextField.text = jobKategorien[row]
    self.view.endEditing(false)
    }

    //Folgendes für die Erstellung der Toolbar
    func createJobToolBar() {
    let jobToolBar = UIToolbar()
    jobToolBar.sizeToFit()
    //Costumisazions
    jobToolBar.barTintColor = .gray
    jobToolBar.tintColor = .white
    let jobDoneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(RegistrationViewController.dismissKeyboard))
    jobToolBar.setItems([jobDoneButton], animated: false)
    jobToolBar.isUserInteractionEnabled = true
    jobAuswählenTextField.inputAccessoryView = jobToolBar
    }
    @objc func dismissKeyboard() {
    view.endEditing(true)
    }
    }


    Vielen Dank im voraus und ein schönes Wochenende, Tom
  • Das hier wirft die Konsole mir aus wenn ich das ganze einmal im Simulator laufen lassen:
    (Es kann dabei sein, dass nicht alles für den aktuellen Fall relevant ist, leider verstehe ich selber aber nicht viel von dem was dort bemängelt wird und habe den gesamten Verlauf einmal kopiert)


    2019-08-04 00:21:15.511041+0200 Gardentesting2[8036:145426] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want.
    Try this:
    (1) look at each constraint and try to figure out which you don't expect;
    (2) find the code that added the unwanted constraint or constraints and fix it.
    (
    "<NSLayoutConstraint:0x6000003272a0 UITextField:0x7fbacf8ad600.height == 30 (active)>",
    "<NSLayoutConstraint:0x600000327840 UITextField:0x7fbacf868400.height == 30 (active)>",
    "<NSLayoutConstraint:0x600000326170 UISwitch:0x7fbaced23490.height == 31 (active)>",
    "<NSLayoutConstraint:0x60000035d950 Gardentesting2.RoundLabel:0x7fbacee2ccb0'Job erstellen'.height == 39 (active)>",
    "<NSLayoutConstraint:0x60000035cfa0 Gardentesting2.RoundTextView:0x7fbad0889600'Weitere Informationen...'.height == 116 (active)>",
    "<NSLayoutConstraint:0x600000358cd0 UITextField:0x7fbad00a2800.height == 32 (active)>",
    "<NSLayoutConstraint:0x6000003225d0 UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide'.bottom == UISwitch:0x7fbaced23490.bottom + 295 (active)>",
    "<NSLayoutConstraint:0x600000322710 Gardentesting2.RoundLabel:0x7fbacee2ccb0'Job erstellen'.top == UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide'.top + 11 (active)>",
    "<NSLayoutConstraint:0x600000322800 V:[Gardentesting2.RoundLabel:0x7fbacee2ccb0'Job erstellen']-(28)-[UITextField:0x7fbad00a2800] (active)>",
    "<NSLayoutConstraint:0x600000322940 V:[UITextField:0x7fbad00a2800]-(79)-[Gardentesting2.RoundTextView:0x7fbad0889600'Weitere Informationen...'] (active)>",
    "<NSLayoutConstraint:0x600000322b20 V:[Gardentesting2.RoundTextView:0x7fbad0889600'Weitere Informationen...']-(29)-[UITextField:0x7fbacf8ad600] (active)>",
    "<NSLayoutConstraint:0x600000325810 V:[UITextField:0x7fbacf8ad600]-(11)-[UITextField:0x7fbacf868400] (active)>",
    "<NSLayoutConstraint:0x600000325950 V:[UITextField:0x7fbacf868400]-(5)-[UISwitch:0x7fbaced23490] (active)>",
    "<NSLayoutConstraint:0x600000337a20 'UIView-Encapsulated-Layout-Height' UIView:0x7fbacee23820.height == 812 (active)>",
    "<NSLayoutConstraint:0x600000322030 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide']-(34)-| (active, names: '|':UIView:0x7fbacee23820 )>",
    "<NSLayoutConstraint:0x600000321ef0 'UIViewSafeAreaLayoutGuide-top' V:|-(44)-[UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide'] (active, names: '|':UIView:0x7fbacee23820 )>"
    )

    Will attempt to recover by breaking constraint
    <NSLayoutConstraint:0x60000035cfa0 Gardentesting2.RoundTextView:0x7fbad0889600'Weitere Informationen...'.height == 116 (active)>

    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
    2019-08-04 00:21:15.512713+0200 Gardentesting2[8036:145426] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want.
    Try this:
    (1) look at each constraint and try to figure out which you don't expect;
    (2) find the code that added the unwanted constraint or constraints and fix it.
    (
    "<NSLayoutConstraint:0x6000003272a0 UITextField:0x7fbacf8ad600.height == 30 (active)>",
    "<NSLayoutConstraint:0x600000327840 UITextField:0x7fbacf868400.height == 30 (active)>",
    "<NSLayoutConstraint:0x600000326170 UISwitch:0x7fbaced23490.height == 31 (active)>",
    "<NSLayoutConstraint:0x6000003225d0 UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide'.bottom == UISwitch:0x7fbaced23490.bottom + 295 (active)>",
    "<NSLayoutConstraint:0x600000322bc0 UITextField:0x7fbacf8ad600.top == UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide'.top + 334 (active)>",
    "<NSLayoutConstraint:0x600000325810 V:[UITextField:0x7fbacf8ad600]-(11)-[UITextField:0x7fbacf868400] (active)>",
    "<NSLayoutConstraint:0x600000325950 V:[UITextField:0x7fbacf868400]-(5)-[UISwitch:0x7fbaced23490] (active)>",
    "<NSLayoutConstraint:0x600000337a20 'UIView-Encapsulated-Layout-Height' UIView:0x7fbacee23820.height == 812 (active)>",
    "<NSLayoutConstraint:0x600000322030 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide']-(34)-| (active, names: '|':UIView:0x7fbacee23820 )>",
    "<NSLayoutConstraint:0x600000321ef0 'UIViewSafeAreaLayoutGuide-top' V:|-(44)-[UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide'] (active, names: '|':UIView:0x7fbacee23820 )>"
    )

    Will attempt to recover by breaking constraint
    <NSLayoutConstraint:0x600000326170 UISwitch:0x7fbaced23490.height == 31 (active)>

    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
    2019-08-04 00:21:15.514214+0200 Gardentesting2[8036:145426] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want.
    Try this:
    (1) look at each constraint and try to figure out which you don't expect;
    (2) find the code that added the unwanted constraint or constraints and fix it.
    (
    "<NSLayoutConstraint:0x6000003272a0 UITextField:0x7fbacf8ad600.height == 30 (active)>",
    "<NSLayoutConstraint:0x600000327840 UITextField:0x7fbacf868400.height == 30 (active)>",
    "<NSLayoutConstraint:0x600000358b90 UILabel:0x7fbaced26c90.height >= 21 (active)>",
    "<NSLayoutConstraint:0x6000003223f0 UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide'.bottom == UILabel:0x7fbaced26c90.bottom + 300 (active)>",
    "<NSLayoutConstraint:0x600000322bc0 UITextField:0x7fbacf8ad600.top == UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide'.top + 334 (active)>",
    "<NSLayoutConstraint:0x600000325810 V:[UITextField:0x7fbacf8ad600]-(11)-[UITextField:0x7fbacf868400] (active)>",
    "<NSLayoutConstraint:0x600000327610 V:[UITextField:0x7fbacf868400]-(10)-[UILabel:0x7fbaced26c90] (active)>",
    "<NSLayoutConstraint:0x600000337a20 'UIView-Encapsulated-Layout-Height' UIView:0x7fbacee23820.height == 812 (active)>",
    "<NSLayoutConstraint:0x600000322030 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide']-(34)-| (active, names: '|':UIView:0x7fbacee23820 )>",
    "<NSLayoutConstraint:0x600000321ef0 'UIViewSafeAreaLayoutGuide-top' V:|-(44)-[UILayoutGuide:0x60000194cee0'UIViewSafeAreaLayoutGuide'] (active, names: '|':UIView:0x7fbacee23820 )>"
    )

    Will attempt to recover by breaking constraint
    <NSLayoutConstraint:0x600000358b90 UILabel:0x7fbaced26c90.height >= 21 (active)>

    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
    2019-08-04 00:21:21.494257+0200 Gardentesting2[8036:145426] [WindowServer] display_timer_callback: unexpected state (now:1ac57256da57 < expected:1ac5733d252e)
    2019-08-04 00:21:23.174496+0200 Gardentesting2[8036:145426] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/Tom/Library/Developer/CoreSimulator/Devices/C9FB15B1-FC29-4B2E-9D15-09F02721F26E/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
    2019-08-04 00:21:23.175042+0200 Gardentesting2[8036:145426] [MC] Reading from private effective user settings.
    2019-08-04 00:21:26.363609+0200 Gardentesting2[8036:145426] [WindowServer] display_timer_callback: unexpected state (now:1ac6949299ce < expected:1ac69563ab44)


    Edit: Es kann nicht sein, dass man in den Einstellungen des TextFields etwas anpassen muss? Es gibt ja teilweise die Möglichkeit z.B. das Element für die Benutzer zu deaktivieren (das ist in meinem Fall aktiv)...gib es da eventuell eine Einstellung?
  • Die Ausgaben haben alle mit Layout-Contraints zu tun, das hilft nicht weiter (wäre mit Code-Tags aber wesentlich einfacher zu lesen).

    Ich würde einen Breakpoint in der „didSelectRow“-Methode setzen und dann einmal nachschauen, ob alle Variablen wie erwartet gefüllt sind. Vielleicht auch einmal das UITextField explizit füllen bzw. prüfen, ob es überhaupt das erwartete Objekt referenziert. Vielleicht ist Dein IBOutlet gar nicht richtig verbunden...

    Mattes
    Diese Seite bleibt aus technischen Gründen unbedruckt.