Reputation: 1338
I having problem with appending or adding numbers to my Integer variable!
I have to view controllers and in the first one I have this codes:
var checkSahafi = Int()
if sahafi.isSelected == true {
print("sahafi Selected")
self.checkSahafi += 1
performSegue(withIdentifier: "secondStep", sender: self)
} else if safheArayi.isSelected == true {
print("safheArayi Selected")
self.checkSahafi += 2
performSegue(withIdentifier: "secondStep", sender: self)
} else if laminet.isSelected == true {
print("laminet Selected")
self.checkSahafi += 3
performSegue(withIdentifier: "secondStep", sender: self)
} else if simpichi.isSelected == true {
print("simpichi Selected")
self.checkSahafi += 4
performSegue(withIdentifier: "secondStep", sender: self)
}
and in the second View Controller I have this method :
let checkNum = firstViewController().checkSahafi
override func viewDidLoad() {
super.viewDidLoad()
print("\(checkNum)")
print("\(checkNum)")
print("\(checkNum)")
print("\(checkNum)")
}
But The app Just Print 0 how can I append those numbers?!
Remember that I used checksahafi.append method too and didn't get the result!
Upvotes: 0
Views: 55
Reputation: 72410
The issue you are getting value from completely new instance of FirstViewController
what you need to do is override prepareForSegue
with your controller and pass value.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? SecondViewController {
vc.checkNum = self.checkSahafi
}
}
Now simply change the declaration of checkNum
in SecondViewController
like this.
var checkNum = Int()
override func viewDidLoad() {
super.viewDidLoad()
print("\(checkNum)")
}
Instead of calling performSegue(withIdentifier:sender:)
in more than once in every if condition simply call it at last after all your if conditions
if sahafi.isSelected == true {
print("sahafi Selected")
self.checkSahafi += 1
}
else if safheArayi.isSelected == true {
print("safheArayi Selected")
self.checkSahafi += 2
}
else if laminet.isSelected == true {
print("laminet Selected")
self.checkSahafi += 3
} else if simpichi.isSelected == true {
print("simpichi Selected")
self.checkSahafi += 4
}
performSegue(withIdentifier: "secondStep", sender: self)
Upvotes: 1