如何在解除视图后手动调用ViewDidAppear?

时间:2016-01-31 13:53:04

标签: ios swift uiviewcontroller lifecycle

我在调用来自不同视图控制器的数据时遇到问题。

Illustrate

当应用程序启动时,它会输入1,并立即输入2,因为2是主视图。 2然后会立即检查是否有人注册,结果如下:

A - 如果结果为负,则进入登录视图控制器

B - 像往常一样调用一个函数来下载viewDidAppear中的数据(这对我来说不是问题)

我的问题是当A发生时,用户将输入他的患者ID并与服务器通信,如果它成功通过验证,它将自行解散并返回到查看控制器2.

当它返回到视图控制器2时,我希望它像往常一样运行viewDidAppear和DOWNLOAD DATA。

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    print("First view appeared")
    let prefs = NSUserDefaults.standardUserDefaults()

    //if an user has logged in
    let isLoggedIn = prefs.integerForKey("ISLOGGEDIN") as Int
    if (isLoggedIn != 1){
        print("No user currently, so heading to login screen")
        self.performSegueWithIdentifier("gotoLogin", sender: self)
    }else{
        print("ViewDidAppear: An user has been logged in")
        let permissionToLoadData = prefs.integerForKey("ISLOGGEDIN")

        if (permissionToLoadData != 1) {
            print("Please grant permission to get data")
        }else{
            print("First view: connecting to database")

            reportStatus()
            socket.connect()

            readDataFromSocket(){ data in
                let json = JSON(data)
                let nou = json[0].count
                if nou > 0 {
                    print("Test(1st VC): grabbing data from database")
                    for var i=0; i<nou; ++i{
                        numberOfUsersExisting = nou
                        pSample += [Patient(id: json[0][i]["ID"].intValue, name: json[0][i]["Name"].stringValue, gender: json[0][i]["Gender"].stringValue, mileage: json[0][i]["Mileage"].doubleValue)]
                        pSample.sortInPlace({$0.globalPatientMileAge < $1.globalPatientMileAge})
                    }
                    print("Successfully grabbed data")
                }else{
                    print("No user in the database")
                    numberOfUsersExisting = 0
                }
            }//readDataFromSocket
        }//permission to load data
    }
}//end of viewDidAppear

然而它并没有按照我的预期行事,嗯,那么无论如何我可以在解除登录视图控制器动作后立即调用viewDidAppear(来自视图控制器2)?

1 个答案:

答案 0 :(得分:0)

您可以将下载数据逻辑与其他方法分开,并在需要时进行呼叫。所以声明一个正在下载数据的函数。

let prefs = NSUserDefaults.standardUserDefaults()

func downloadingData() {
    print("ViewDidAppear: An user has been logged in")
    let permissionToLoadData = prefs.integerForKey("ISLOGGEDIN")

    if (permissionToLoadData != 1) {
        print("Please grant permission to get data")
    }else{
        print("First view: connecting to database")

        reportStatus()
        socket.connect()

        readDataFromSocket(){ data in
            let json = JSON(data)
            let nou = json[0].count
            if nou > 0 {
                print("Test(1st VC): grabbing data from database")
                for var i=0; i<nou; ++i{
                    numberOfUsersExisting = nou
                    pSample += [Patient(id: json[0][i]["ID"].intValue, name: json[0][i]["Name"].stringValue, gender: json[0][i]["Gender"].stringValue, mileage: json[0][i]["Mileage"].doubleValue)]
                    pSample.sortInPlace({$0.globalPatientMileAge < $1.globalPatientMileAge})
                }
                print("Successfully grabbed data")
            }else{
                print("No user in the database")
                numberOfUsersExisting = 0
            }
        }//readDataFromSocket
    }//permission to load data
}

然后在viewDidLoad()上调用它。

override func viewDidLoad() {
    super.viewDidLoad()
    //if an user has logged in
    let isLoggedIn = prefs.integerForKey("ISLOGGEDIN") as Int
    if (isLoggedIn != 1){
        print("No user currently, so heading to login screen")
        self.performSegueWithIdentifier("gotoLogin", sender: self)
    }else{
        downloadingData()
    }
}

同时关闭视图控制器。

dismissViewControllerAnimated(true) {
    viewController.downloadingData()
}