2017年3月13日 星期一

從這個viewcontroll 傳值到另外一個viewcontroller



利用   prepare 這個function


call 了  performSegue(withIdentifier: "gotoview2", sender: inputtext)



  @IBAction func changeView(_ sender: UIButton) {
        
        //check if user input text or not
        
        if let inputtext = myTextInput.text{
            if inputtext == ""{
                // no input, pop up an alert
                let myalert = UIAlertController(title: "No input", message: "Please enter something", preferredStyle: .alert)
                let okaction = UIAlertAction(title: "OK", style: .default, handler: nil)
                myalert.addAction(okaction)
                present(myalert, animated: true, completion: nil)
            }else{
                performSegue(withIdentifier: "gotoview2", sender: inputtext)
            }
            
        }
        

    }

然後系統就會自動呼叫   prepare()...  在這裡面判斷要去的   viewcontroll

在這裡我們要去    gotoview2 這個  viewcontroller

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if segue.identifier == "gotoview2"{
            if let lightred = segue.destination as? lightRedViewController{
                lightred.infoFromView1 = sender as? String
            }
          }
        
    }

    
把數值存到    gotoview2 這個  viewcontroller 的變數   infoFromView1


-------------------------------

class lightRedViewController: UIViewController {

    var infoFromView1:String?
    
    @IBOutlet weak var myLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        myLabel.text = infoFromView1

        // Do any additional setup after loading the view.
    }

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

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

-------------

回到剛剛的viewcontroll...


 let _ = navigationController?.popViewController(animated: true)

前提是有embed navigatorcontroller

--------------

 myTextInput.resignFirstResponder() // release the focus






ios 買的線上課程

Ref : https://code4startup.com/projects/uber-app-for-food-with-python-django-and-swift/tasks/creating-your-very-first-xcode-project

Ref : https://www.udemy.com/swift3-level1/learn/v4/t/lecture/5934516?start=15

Ref : https://www.udemy.com/kencens-swift3/learn/v4/overview

Ref : https://www.udemy.com/the-essential-ios-developer-course/learn/v4/overview

Ref : https://www.udemy.com/ios-10-swift-3-hands-on-features-siri-kit-messages/learn/v4/overview

ios connect api 網站練習


雖然已經很詳細地解說如何解析 JSON 格式的資料,不過如果做更多的練習會更熟悉如何連結其他網路服務的 API。我推薦下面網頁的教學,供大家繼續做練習的參考:
1 AppCode 連接台北市政府的 Open Data:很好的範例,可惜是 Swift 2 的程式碼,遇到有錯請大家自己改一下吧。
網址:http://www.appcoda.com.tw/json-data-taipei-tutorial/

2 FanZhao 連結天氣API:連結我們課堂上提到的 Weather API,可惜也不是 Swift 3 的程式碼,遇到有錯請大家自己改一下吧。
網址:https://youtu.be/r-LZs0De7_U