2017-11-20 11 views
-3

私はいくつかの異なる場所でUIPickerViewを持っています。現在、ユーザーが場所を選択すると、ボタンが表示され、背景イメージが変更されます。ボタンが接続されたら、次のビューをその場所から表示されたユーザーのリストにしたいと思います(ユーザーはFirebaseからフェッチされます)。私はこれに取り組む方法についていくつかのアドバイスをしたいと思います。プロトコル、コンテナビュー、またはセグを使用するのが最善の方法であるかどうかはわかりません。違いがあれば、3つのタブを持つタブ付きアプリケーションでもあります。他のタブは設定とプロファイルタブです。 location picker location picked私のアプリケーションの構造化に関するアドバイス

class LocationPickerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { 


@IBAction func locationAction(_ sender: Any) { 
} 

@IBOutlet weak var locationButton: UIButton! 


let locationsArray = ["Please Select your location","San Francisco", "Los Angeles", "Las Vegas", "Chicago","New York","Miami"] 

func numberOfComponents(in pickerView: UIPickerView) -> Int { 
    return 1 
} 

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    return locationsArray[row] 
} 

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    return locationsArray.count 
} 

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { 
    let label = (view as? UILabel) ?? UILabel() 

    label.textColor = .white 
    label.textAlignment = .center 
// label.font = UIFont(name: "SanFranciscoText-Light", size: 50) 
    label.font = UIFont.systemFont(ofSize: 30) 
    // where data is an Array of String 
    label.text = locationsArray[row] 

    return label 
} 
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { 
    return 50.0 
} 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

    if(row == 0) 
    { 
     locationLabel.text = "Select Location" 

     locationButton.isHidden = true 
     self.view.backgroundColor = UIColor.black 

    } 
    else if(row == 1) 
    { 
     locationLabel.text = locationsArray[row] 
     print(locationsArray[row]) 
     locationButton.isHidden = false 
     locationButton.setTitle("SF", for: .normal) 
     self.view.backgroundColor = UIColor(patternImage: UIImage(named: "SF")!) 


    } 
    else if(row == 2) 
    { 
     locationLabel.text = locationsArray[row] 
     print(locationsArray[row]) 
     locationButton.isHidden = false 
     locationButton.setTitle("LA", for: .normal) 
     self.view.backgroundColor = UIColor(patternImage: UIImage(named: "LA")!) 
    } 
    else if(row == 3) 
    { 
     locationLabel.text = locationsArray[row] 
     print(locationsArray[row]) 
     locationButton.isHidden = false 
     locationButton.setTitle("Las Vegas", for: .normal) 
     self.view.backgroundColor = UIColor(patternImage: UIImage(named: "LV")!) 
    } 
    else if(row == 4) 
    { 
     locationLabel.text = locationsArray[row] 
     print(locationsArray[row]) 
     locationButton.isHidden = false 
     locationButton.setTitle("Chicago", for: .normal) 
     self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Chicago")!) 
    } 
    else if(row == 5) 
    { 
     locationLabel.text = locationsArray[row] 
     print(locationsArray[row]) 
     locationButton.isHidden = false 
     locationButton.setTitle("New York", for: .normal) 
     self.view.backgroundColor = UIColor(patternImage: UIImage(named: "NY")!) 
    } 
    else if(row == 6) 
    { 
     locationLabel.text = locationsArray[row] 
     print(locationsArray[row]) 
     locationButton.isHidden = false 
     locationButton.setTitle("MIAMI", for: .normal) 
     self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Miami")!) 
    } 


} 

@IBOutlet weak var locationLabel: UILabel! 


@IBOutlet weak var locationPicker: UIPickerView! 

override func viewDidLoad() { 
    locationPicker.backgroundColor = UIColor.black 
    locationPicker.alpha = 0.6 
    locationButton.isHidden = true 
    super.viewDidLoad() 
    self.locationPicker.selectRow(0, inComponent: 0, animated: true) 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

}

答えて

0

セグエとしてボタンを使用すると、単にストーリーボードに新しいコントローラを作成し、右、そのビューにボタンをドラッグしてクリックし、これに取り組むための最も効率的な方法だろう簡単なセグを作成します。

関連する問題