简体   繁体   中英

NavigationController does not reload tableView data after popToRootViewController

Sorry for my english

In my app, i have a login form. If the username and password are good, the username is stored in a keyChain and it goes back to the main viewController using: self.navigationController.popToRootViewControllerAnimated(true)

The data is loaded by a php file that use the username to get data from that username

And in the main viewController their is a tableView that contain the data

But the problem is that after the connection, the tableView does not change. It only change if i restart the app

My code to get the data:

@IBOutlet var tblTask: UITableView!

let bodyData = "username=" + username!

        let URL: NSURL = NSURL(string: "link to php file")
        let request:NSMutableURLRequest = NSMutableURLRequest(URL:URL)
        request.HTTPMethod = "POST"
        request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
        {
            (response, data, error) in
            var output = NSString(data: data, encoding: NSUTF8StringEncoding)

            var array = self.JSONParseArray(output)

            for var i = 0, a = 1 ; i < array.count ; i += 2, a += 2 {

                if(a > 0 && i > 0)
                {
                    self.arrayDate.append(array[i])
                    self.arrayPoid.append(array[a])
                }
            }
            for var i = 0; i < self.arrayDate.count ; i++ {
                self.arrayPoid[i] += " livres"
            }


            self.tblTask.reloadData()
        }

I think Unwind Segues are what you need. You can set them with only a few steps. Add this method in your rootViewController:

@IBAction func reset(segue: UIStoryboardSegue) {
    tableView.reloadData()
}

In Interface Builder, create UIButtons in any viewController that would need to go back to rootViewController. Then link each of these UIButtons to the red "Exit" button of their respective viewController and select reset: in the pop-up menu that appears (see the images below).

在此处输入图片说明

在此处输入图片说明

When you will click on those buttons, you will be brought back to your rooViewController and reset: will be performed (with your tableView reloaded).

See WWDC 2012 session video "Adopting Storyboards in Your App" for more details about unwind segues [starts at 38 minutes].

Try the call self.tableView.reloadData() in the UITableViewController 's viewWillAppear method. Does that solve it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM