-2

I made a popupviewcontroller using this tutorial: https://www.youtube.com/watch?v=FgCIRMz_3dE

And I would like when I press down the popup (with a Retry-button instead), that it should go back to the original Viewcontroller, as it does, but also restarts the "game" on the original viewcontroller automatically. So basically I want to start the game when I press "Retry" on the popup. But I only get errors when trying to reach that function..

Original Viewcontroller:

var time : Float = 0.0
    var timer: NSTimer?


    @IBOutlet weak var progressBar: UIProgressView!


    override func viewDidLoad() {
        super.viewDidLoad()
        startGame()
    }
    func startGame() {
        startTimer()

    }
    func startTimer() {
        timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector:#selector(GameViewController.setProgress1), userInfo: nil, repeats: true)

    }
    func setProgress1() {
        time += 0.001
        progressBar.setProgress(time / 2, animated: true)
        if time >= 1.9 {
            endPopUp()
            timer!.invalidate()
            progressBar.setProgress(0.0, animated: false)
            setProgress1(time = 0.0)

        }
    }


    func endPopUp() {
        let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("endPopUpID") as! PopUpViewController
        self.addChildViewController(popOverVC)
        popOverVC.view.frame = self.view.frame
        self.view.addSubview(popOverVC.view)
        popOverVC.didMoveToParentViewController(self)



        }



}

As you can see, when the time reaches 1.9, the popOverVC should appear, but when the popup closes with the button in the popOverVC, I want to type: GameViewController().startGame()

But it's just giving me errors... how do I do this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
moseby
  • 21
  • 2
  • 7
  • http://stackoverflow.com/questions/24099230/delegates-in-swift – Shades Sep 11 '16 at 10:21
  • I know what a delegate is, kind of. But I need help how to use it for this scenario. So a bit more detailed help would mean a lot. – moseby Sep 11 '16 at 10:27
  • Thanks, but Im going to need a little more help than that :/ Guess I will need to look somewhere else – moseby Sep 11 '16 at 10:36
  • If you post a question about errors in your code, you need to include those errors in your question. – rmaddy Sep 11 '16 at 16:14

1 Answers1

0

I'm not the swift guy, but the underneath concept is same for objective-c and swift. Code examples are in objective-c.

TODO: Declare a protocol in your popOverViewContorller header file. As your state, you need to protocol method as following

@protocol PopOverViewControllerDelegate
    -(void)popOverRetryBtnPressed;
    -(void)popOverCloseBtnPressed;
@end

In your header file declare a property as follows

@interface PopOverViewController:UIIViewController
    ...//other staff
    @property id<PopOverViewControllerDelegate> myDelegate;
@end

Now in your retry button action should look something like this

-(IBAction)onPressRetry:(id)sender
{
    if(self.myDelegate && [self.myDelegate respondToSelector:@selector(popOverRetryBtnPressed)])
    {
         [self.myDelegate popOverRetryBtnPressed];
    }
    else
    {
         NSLog(@"delegate not implemented");
    }
}

simillarly complete the close button function. Now implement the delegates in your view controller as any other delegate. Don't forget to set the popOverViewController's myDelegate property.

Under the hood: Here is what happens when a delegate pattern is used. When a protocol is declared, it's contain the information that, what the object is capable of providing while implement this protocol. Say in the above example, our protocol is saying that, the object that will conform this protocol will contain two function named popOverRetryBtnPressed & popOverCloseBtnPressed. Then we declare an property of type id<PopOverViewControllerDelegate> which denote any class that conform to the protocol declaration PopOverViewControllerDelegate. Setting this property will contain the reference to the object which conform and implement the delegate method. Now time to call these protocol methods in appropriate places. In retry button action call the popOverRetryBtnPressed, and call popOverCloseBtnPressed as in same manner. Now before calling check is that self.myDelegate object actually implements any method which you are calling, Otherwise it will end up crashing.

Thats all, hope you enjoy it.

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44