在swift中调用viewWillAppear方法时,我的textFields不会消失

时间:2016-01-08 15:01:47

标签: swift

import UIKit
import Alamofire
import SystemConfiguration

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var simpleLabel: UILabel!
    @IBOutlet weak var uiNameSearch: UITextField!
    @IBOutlet weak var uiGivenName: UITextField!
    var patient1 = Patient!()

    override func viewDidLoad() {
        super.viewDidLoad()
        uiNameSearch.delegate = self
        uiGivenName.delegate = self

        print("ViewController viewDidLoad")
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.simpleLabel.center.x -= self.view.bounds.width
        self.uiGivenName.center.x -= self.view.bounds.width
        print("Call viewWillAppear")
    }

    override func viewDidAppear(animated: Bool){
        super.viewDidAppear(animated)
        UIView.animateWithDuration(0.9 , animations: {
           // self.simpleLabel.center.x += self.view.bounds.width
            self.uiGivenName.center.x += self.view.bounds.width
        })

        UIView.animateWithDuration(0.7 , delay: 0.7, options: [], animations: {
            self.uiNameSearch.center.x += self.view.bounds.width
        }, completion: nil )
    }

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



    @IBAction func searchForName(sender: UIButton) {
        let header = ["Accept" : "application/json"]
        let name = uiNameSearch.text!
        let given = uiGivenName.text!
        if name.characters.count == 0 {
            self.view.makeToast( message: "Please insert the family name of the patient!")
        } else  if given.characters.count == 0 {
            self.view.makeToast(message: "Please insert the patient given name!")
        } else if checkInternetConnection() == false {
            self.view.makeToast(message: "Please connect to the internet!")
        } else {
            Alamofire.request(.GET, "https://open-ic.epic.com/FHIR/api/FHIR/DSTU2/Patient?family=\(name)&given=\(given)", headers: header).responseJSON { response in
                self.patient1 = Patient(response: response.result.value!)
                self.performSegueWithIdentifier("showSegue", sender: sender)

            }
        }
    }

    func checkInternetConnection() -> Bool {
        var zeroAddress = sockaddr_in()
        zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
        zeroAddress.sin_family = sa_family_t(AF_INET)
        let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
            SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
        }
        var flags = SCNetworkReachabilityFlags()
        if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
            return false
        }
        let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
        let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
        return (isReachable && !needsConnection)
    }


    func textFieldShouldReturn(textField: UITextField) -> Bool {
        switch textField {
        case uiNameSearch:
            uiNameSearch.resignFirstResponder()
            uiGivenName.becomeFirstResponder()

        case uiGivenName:
            uiGivenName.resignFirstResponder()

        default:
            print("")
        }
        return true
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject?){
        if segue.identifier == "showSegue" {
            if let displayViewController = segue.destinationViewController as? DisplayViewController {
                displayViewController.patient1 = patient1
            }

        }

    }
}

我想让uiGivenName和simpleLabel消失,直到创建视图,以及调用viewDidAppear从左侧出现之后。

1 个答案:

答案 0 :(得分:0)

您的视图不会消失,因为您正在更改viewWillAppear中的视图位置,并且当调用viewWillAppear时,视图即将添加到视图层次结构中(即视图仍未添加到视图层次结构中),因此它不反映在你的用户界面。

所以你可以在viewDidLayoutSubviews中做你的东西,因为如果你想在屏幕实际出现之前修改你的UI,这是最好的地方。

编辑 -

替换此 -

    override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.simpleLabel.center.x -= self.view.bounds.width
    self.uiGivenName.center.x -= self.view.bounds.width
    print("Call viewWillAppear")
}

    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.simpleLabel.center.x -= self.view.bounds.width
    self.uiGivenName.center.x -= self.view.bounds.width
}