-1

Hi all am developing a app using swift 2.2 i declared a variable in first view controller i want to access that in second view controller.i have searched other stack overflow answers nothing supports me.

In my first view controller:

class firstViewController: UIViewController{

var timer:nstimer?
//performed some functions using nstimer
}

Now i want to stop the timer and i kept button action in the second view controller so i want to access the variable timer.how to access??

I searched before stack overflow answers that asked to use prepare for segue but am already given some connection and performed segue to other controllers.

Now am looking for the solution to access that variables.can anyone help me out from this problem??

Thanks in advance.

vaibhav
  • 4,038
  • 1
  • 21
  • 51
antonyraja
  • 13
  • 7
  • http://stackoverflow.com/questions/24222640/passing-data-between-view-controllers-in-swift – Sahil Nov 09 '16 at 11:39
  • using storyboard identifier and prepare for segue will cause any problem to the project?? – antonyraja Nov 09 '16 at 11:42
  • because am using many view controllers and uses many segues between view controllers – antonyraja Nov 09 '16 at 11:43
  • no! it won't create any problem. and in prepareforsegue you can check identifier name. but keep in mind two identifier name should not be same. – Sahil Nov 09 '16 at 11:43
  • no need to access timer var from second ctrlr. set a delegate, and the implementation in first ctrlr stop the timer. So from second ctrlr call delegate.stopTimer(). btw better to move Swift 3 – jpulikkottil Nov 09 '16 at 11:43
  • i have almost completed by application thats what i cant able to move to swift3 – antonyraja Nov 09 '16 at 11:45
  • @antonyraja check my answer. it won't create any problem. – Sahil Nov 09 '16 at 11:49
  • there are plenty of tutorials and examples available regarding your issue you can google little bit. – vaibhav Nov 09 '16 at 13:51

2 Answers2

1

In FirstController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "segueidentifier" {

     if let destVc = segue.destinationViewController as? SecondViewController {
      destVc.timer = self.timer
    }
 }
}

In SecondViewController create a timer property and use it.

var timer: NSTimer?

and if you are getting problem with connecting. try this

Create segue between FirstViewController and SecondViewController by ctrl + drag and connect to second and give name to segue identifier

enter image description here

Sahil
  • 9,096
  • 3
  • 25
  • 29
  • if segue.identifier == "segueidentifier" – antonyraja Nov 09 '16 at 11:52
  • if segue.identifier == "segueidentifier" .here what identifier i have to give..?? – antonyraja Nov 09 '16 at 11:52
  • you need to click on segue and give the identifier name on right side https://www.google.co.in/search?q=segue+identifier+swift&espv=2&biw=1229&bih=548&source=lnms&tbm=isch&sa=X&ved=0ahUKEwi9xPu7zpvQAhUKPo8KHWrnB2QQ_AUICCgD#imgrc=eKAWRN9Aycd5YM%3A – Sahil Nov 09 '16 at 11:54
  • there is no option like segue....there is show and custom only....is it possible to use storyboard id?? – antonyraja Nov 09 '16 at 11:57
  • check my updated answer. and for more http://www.codingexplorer.com/segue-swift-view-controllers/ – Sahil Nov 09 '16 at 12:06
  • it shows me three popup i.e....show custom and push....which should i have to select?? – antonyraja Nov 09 '16 at 12:08
  • it depends . if your first controller embed in navigation controller select push else select present modally. you need to read http://www.codingexplorer.com/segue-swift-view-controllers/ carefully. – Sahil Nov 09 '16 at 12:12
  • variable accessed but if i press the button action...timer.invalidate not working it keeps on calling the function nstimer....but nothing shows error....what to do???? – antonyraja Nov 09 '16 at 12:27
  • in my firstvc: override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if segue.identifier == "pushsegue" { var destVc = segue.destinationViewController as! LeftSideViewController destVc.timer = self.timer } in my secondvc:i declared again var timer:nstimer?..like what i declare in firstvc........In button action:timer?.invalidate() – antonyraja Nov 09 '16 at 12:35
  • have you checked ref in secondViewController viewDidLoad? if not please check print(self.timer) – Sahil Nov 09 '16 at 12:38
  • self.timer =??...what i have to provide? – antonyraja Nov 09 '16 at 12:40
  • sorry. i meant print the timer – Sahil Nov 09 '16 at 12:42
  • why should i have to print timer...and it is not possible to print – antonyraja Nov 09 '16 at 13:01
  • debug the code and check timer object is not nil. if it is successfully passed data in secondviewcontroller and you called timer?.invalidate(). timer should be stopped. – Sahil Nov 09 '16 at 13:03
  • we have to declare var timer:nstimer! in both view controller??or else first vc is enough – antonyraja Nov 09 '16 at 13:18
  • Yes in both view controllers. – Sahil Nov 09 '16 at 15:48
0

Your second ViewController shouldn't know anything about the first view controller, if it does then you essentially tie both VC's together.

What you need to do instead is either pass a reference through like @Sahil suggested above (although I would refrain from doing that as the first VC might not expect a change from an external source), or use a delegate pattern.

With a delegate pattern you give the second VC a weak reference to the first VC and have the first VC conform to a protocol defined by the second VC. The delegate pattern has been explained many times over so I'll just give you a link instead of explaining it again here: http://useyourloaf.com/blog/quick-guide-to-swift-delegates/

The delegate pattern is the safest option and it allows the first VC to control the timer in an expected way rather than having it modified externally, so I would recommend exploring that option instead of passing a reference to the timer through.

dlbuckley
  • 685
  • 1
  • 5
  • 14
  • @antonyraja there is an example in the link that I posted. It explains the delegate concept as a whole so you can understand whats going on. – dlbuckley Nov 09 '16 at 13:37