J.Arji
J.Arji

Reputation: 661

pass data with Button in swift

I try to pass data from my ViewController to TableViewPlace.swiftUse the buttons. When I am choosing button return not some thing

viViewController

class ViewController: UIViewController {

    struct Country{
        var id :Int
        init(id:Int){

            self.id = id
        }
}
var CountrySelected = [Country]()
var countryArry = NSArray()



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

@IBAction func airAction(sender: AnyObject) {
}


@IBAction func viewPlaceAction(sender: AnyObject) {


    getParsePlaceView()
     // json viewPlace


    performSegueWithIdentifier("viewPlaceSegu", sender: sender)
}

@IBAction func tourAction(sender: AnyObject) {
}
/// Open the page
    // parse json
func getParsePlaceView(){

    let url = NSURL(string: "http://jsonplaceholder.typicode.com/posts")
    NSURLSession.sharedSession().dataTaskWithURL(url!){ [unowned self] (data , repsonse , error) in
        if error != nil {
            print(error!)
        } else {

            do {
                let posts = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! [[String:AnyObject]]

                for post in posts {

                    if let id = post["userId"] as? Int{

                     // print(id)

                        let sets = Country(id: id)
                        self.CountrySelected.append(sets)
                    }


                }
                self.countryArry = posts
                print(self.countryArry)// return data and currect

            } catch let error as NSError {
                print(error)
            }
        }
        }.resume()

   print(countryArry) // return nil why??
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    // get a reference to the second view controller
    if segue.identifier == "viewPlaceSegu" {
        if let secondViewController = segue.destinationViewController as? TableViewPlace {

            // set a variable in the second view controller with the String to pass
            print(countryArry)
            secondViewController.tnt = countryArry
        }

    }
}


}

when I print(countryArry) return nil why ??

Can someone help me or give me a better solution?

Upvotes: 1

Views: 2474

Answers (1)

AnthonyR
AnthonyR

Reputation: 3545

You are creating a Post object with this line :

let countryPlace = Post(userid: post["userId"] as! Int, title: post["title"] as! String)

And you are trying to pass to your tableview this Post object to your tnt variable which is a ViewController class, this is why you've got the first error :

secondViewController.tnt = Country 

Then, if you want to add your string country to your mutable array you have to do :

if let title = tnt.title {
   Country.append(title)
}

Upvotes: 1

Related Questions