Code kürzen?

  • Code kürzen?

    Hallo liebe Gemeinde,

    könnte man den Code noch kürzen?

    Quellcode

    1. func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    2. myTextField.text = pickerDataSource[row]
    3. if(row == 0)
    4. {
    5. // Traufhö-Vordach-Neigung
    6. grdmaßTextField.enabled = true
    7. grdmaßTextField.backgroundColor = UIColor.whiteColor();
    8. frsthöTextField.enabled = true
    9. frsthöTextField.backgroundColor = UIColor.whiteColor();
    10. trfhöTextField.enabled = true
    11. trfhöTextField.backgroundColor = UIColor.whiteColor();
    12. vrdaläTextField.enabled = true
    13. vrdaläTextField.backgroundColor = UIColor.whiteColor();
    14. dchngTextField.enabled = true
    15. dchngTextField.backgroundColor = UIColor.whiteColor();
    16. okfpfTextField.enabled = false
    17. okfpfTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    18. okfipfTextField.enabled = false
    19. okfipfTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    20. }
    21. else if(row == 1)
    22. {
    23. // Traufhö-Vordach-OKFußpf
    24. grdmaßTextField.enabled = true
    25. grdmaßTextField.backgroundColor = UIColor.whiteColor();
    26. frsthöTextField.enabled = true
    27. frsthöTextField.backgroundColor = UIColor.whiteColor();
    28. trfhöTextField.enabled = true
    29. trfhöTextField.backgroundColor = UIColor.whiteColor();
    30. vrdaläTextField.enabled = true
    31. vrdaläTextField.backgroundColor = UIColor.whiteColor();
    32. dchngTextField.enabled = false
    33. dchngTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    34. okfpfTextField.enabled = true
    35. okfpfTextField.backgroundColor = UIColor.whiteColor();
    36. okfipfTextField.enabled = false
    37. okfipfTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    38. }
    Alles anzeigen
    lg matze
  • Quellcode

    1. func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    2. myTextField.text = pickerDataSource[row]
    3. grdmaßTextField.enabled = true;
    4. grdmaßTextField.backgroundColor = UIColor.whiteColor();
    5. frsthöTextField.enabled = true
    6. frsthöTextField.backgroundColor = UIColor.whiteColor();
    7. trfhöTextField.enabled = true
    8. trfhöTextField.backgroundColor = UIColor.whiteColor();
    9. vrdaläTextField.enabled = true
    10. vrdaläTextField.backgroundColor = UIColor.whiteColor();
    11. okfipfTextField.enabled = false
    12. okfipfTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    13. if(row == 0)
    14. {
    15. // Traufhö-Vordach-Neigung
    16. dchngTextField.enabled = true
    17. dchngTextField.backgroundColor = UIColor.whiteColor();
    18. okfpfTextField.enabled = false
    19. okfpfTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    20. }
    21. else if(row == 1)
    22. {
    23. // Traufhö-Vordach-OKFußpf
    24. dchngTextField.enabled = false
    25. dchngTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    26. okfpfTextField.enabled = true
    27. okfpfTextField.backgroundColor = UIColor.whiteColor();
    28. }
    Alles anzeigen
  • Quellcode

    1. func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    2. myTextField.text = pickerDataSource[row]
    3. if(row == 0)
    4. {
    5. // Traufhö-Vordach-Neigung
    6. for textField in [grdmaßTextField, frsthöTextField, ...] {
    7. textField.enabled = true
    8. textField.backgroundColor = UIColor.whiteColor()
    9. }
    10. okfpfTextField.enabled = false
    11. okfpfTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    12. okfipfTextField.enabled = false
    13. okfipfTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    14. }
    15. else if(row == 1)
    16. {
    17. // Traufhö-Vordach-OKFußpf
    18. for textField in [grdmaßTextField, frsthöTextField, ...] {
    19. textField.enabled = true
    20. textField.backgroundColor = UIColor.whiteColor()
    21. }
    22. dchngTextField.enabled = false
    23. dchngTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    24. okfpfTextField.enabled = true
    25. okfpfTextField.backgroundColor = UIColor.whiteColor();
    26. okfipfTextField.enabled = false
    27. okfipfTextField.backgroundColor = UIColor.groupTableViewBackgroundColor();
    28. }
    Alles anzeigen
  • Quellcode

    1. - (void)pickerView:(UIPickerView*)pickerView didSelectRow:(int)row inComponent:(int)component
    2. {
    3. grdmaßTextField.enabled = true;
    4. grdmaßTextField.backgroundColor = UIColor.whiteColor();
    5. frsthöTextField.enabled = true;
    6. frsthöTextField.backgroundColor = UIColor.whiteColor();
    7. trfhöTextField.enabled = true;
    8. trfhöTextField.backgroundColor = UIColor.whiteColor();
    9. vrdaläTextField.enabled = true;
    10. vrdaläTextField.backgroundColor = UIColor.whiteColor();
    11. dchngTextField.enabled = row==0;
    12. dchngTextField.backgroundColor = row==0?UIColor.whiteColor():UIColor.groupTableViewBackgroundColor();
    13. okfpfTextField.enabled = row==1;
    14. okfpfTextField.backgroundColor = row==0?UIColor.groupTableViewBackgroundColor():UIColor.whiteColor();
    15. okfipfTextField.enabled = false
    16. okfipfTextField.backgroundColor = row==0?UIColor.groupTableViewBackgroundColor():UIColor.groupTableViewBackgroundColor();
    17. myTextField.text = pickerDataSource[row]
    18. }
    Alles anzeigen
    Es hat noch nie etwas gefunzt. To tear down the Wall would be a Werror!
    25.06.2016: [Swift] gehört zu meinen *Favorite Tags* auf SO. In welcher Bedeutung von "favorite"?
  • Das hier machts zwar nicht schneller, dafür aber noch ein paar zeilen kürzen und meiner meinung nach auch etwas überichtlicher:

    Quellcode

    1. - (void)pickerView:(UIPickerView*)pickerView didSelectRow:(int)row inComponent:(int)component
    2. {
    3. for tmpTextField in [grdmaßTextField, frsthöTextField, trfhöTextField, vrdaläTextField]
    4. {
    5. tmpTextField.enabled = true
    6. tmpTextField.backgroundColor = UIColor.whiteColor()
    7. }
    8. dchngTextField.enabled = (row==0);
    9. dchngTextField.backgroundColor = (row==0= ? UIColor.whiteColor() : UIColor.groupTableViewBackgroundColor();
    10. okfpfTextField.enabled = (row==1);
    11. okfpfTextField.backgroundColor = (row==0) ? UIColor.groupTableViewBackgroundColor() : UIColor.whiteColor();
    12. okfipfTextField.enabled = false
    13. okfipfTextField.backgroundColor = (row==0) ? UIColor.groupTableViewBackgroundColor() : UIColor.groupTableViewBackgroundColor();
    14. myTextField.text = pickerDataSource[row]
    15. }
    Alles anzeigen
  • gritsch schrieb:

    Das hier machts zwar nicht schneller, dafür aber noch ein paar zeilen kürzen und meiner meinung nach auch etwas überichtlicher:

    Quellcode

    1. - (void)pickerView:(UIPickerView*)pickerView didSelectRow:(int)row inComponent:(int)component
    2. {
    3. for tmpTextField in [grdmaßTextField, frsthöTextField, trfhöTextField, vrdaläTextField]
    4. {
    5. tmpTextField.enabled = true
    6. tmpTextField.backgroundColor = UIColor.whiteColor()
    7. }
    8. dchngTextField.enabled = (row==0);
    9. dchngTextField.backgroundColor = (row==0= ? UIColor.whiteColor() : UIColor.groupTableViewBackgroundColor();
    10. okfpfTextField.enabled = (row==1);
    11. okfpfTextField.backgroundColor = (row==0) ? UIColor.groupTableViewBackgroundColor() : UIColor.whiteColor();
    12. okfipfTextField.enabled = false
    13. okfipfTextField.backgroundColor = (row==0) ? UIColor.groupTableViewBackgroundColor() : UIColor.groupTableViewBackgroundColor();
    14. myTextField.text = pickerDataSource[row]
    15. }
    Alles anzeigen
    Finde ich nicht, da ist Objective-C mit Swift gemischt ;)
  • matz schrieb:

    gritsch schrieb:

    Das hier machts zwar nicht schneller, dafür aber noch ein paar zeilen kürzen und meiner meinung nach auch etwas überichtlicher:

    Quellcode

    1. - (void)pickerView:(UIPickerView*)pickerView didSelectRow:(int)row inComponent:(int)component
    2. {
    3. for tmpTextField in [grdmaßTextField, frsthöTextField, trfhöTextField, vrdaläTextField]
    4. {
    5. tmpTextField.enabled = true
    6. tmpTextField.backgroundColor = UIColor.whiteColor()
    7. }
    8. dchngTextField.enabled = (row==0);
    9. dchngTextField.backgroundColor = (row==0= ? UIColor.whiteColor() : UIColor.groupTableViewBackgroundColor();
    10. okfpfTextField.enabled = (row==1);
    11. okfpfTextField.backgroundColor = (row==0) ? UIColor.groupTableViewBackgroundColor() : UIColor.whiteColor();
    12. okfipfTextField.enabled = false
    13. okfipfTextField.backgroundColor = (row==0) ? UIColor.groupTableViewBackgroundColor() : UIColor.groupTableViewBackgroundColor();
    14. myTextField.text = pickerDataSource[row]
    15. }
    Alles anzeigen
    Finde ich nicht, da ist Objective-C mit Swift gemischt ;)
    die func kann er sich ja selbst draus machen. ich hab nur das von Amin "optimiert" ;)