Federica Benacquista
Federica Benacquista

Reputation: 843

How to make a button modify another button functions

I have two UIViewController. In the first one, I have a button that adds some views, one at a time, to the main view. In the second one, I set up a store, so that when I press on a button I unlock some features of my app. Now, I perfectly know (I hope) how to handle the part where I make the VCs comunicate and I trigger some other easy functions, what I don't know is how to make the store button increase the button's functions.

WHAT I NEED:
Right now the button adds a maximum of 10 views (complete version). I want that before the user buys my app, he gets to add a maximum of 3 views and then, when he buys it, the function I already have (the one to add 10 views)starts to work and replaces the other one.

MAIN VIEW CONTROLLER

var messageArray = [UIView] ()

I attached all of my UIView from the storyboard and I appended them to my array in the viewDid load like this: messageArray.append(View1)

@IBAction func addMessageViewButton(_ sender: Any) { 
    let hiddenViews = messageArray.filter { $0.isHidden }
    guard !hiddenViews.isEmpty else {

        let sheet = UIAlertController(title: "max reached", message: nil, preferredStyle: .actionSheet)
        let ok = UIAlertAction(title: "OK", style: .cancel, handler: nil)

        let closeAll = UIAlertAction(title: "Close all", style: .destructive) { (addMessage) in

      view1.isHidden = true
      view2.isHidden = true
      view3.isHidden = true
      view4.isHidden = true
      view5.isHidden = true
      view6.isHidden = true
      view7.isHidden = true
      view8.isHidden = true
      view9.isHidden = true
      view10.isHidden = true
    }
            sheet.addAction(ok)
            sheet.addAction(closeAll)

            present(sheet, animated: true, completion: nil)

            return

}
        let randomHiddenView = hiddenViews.randomElement()
        randomHiddenView?.isHidden = false
    }

SHOP VIEW CONTROLLER

Here I won't post all of the code because it would be too much and of course unnecessary, since the important thing to know here is that there's a button and if the user presses it and he proceeds with the purchase, he will get the function I posted up here working instead of the one that allows him to have just 3 views.

func unlock() {

        let appdelegate = UIApplication.shared.delegate
            as! AppDelegate
        appdelegate.viewController!.functionToHave10Views()
//viewControlled is declared in the app delegate like this ` var viewController: ViewController?`
//I know I don't physically have `functionToHave10Views()`, but I guess I'll turn the code of my button into a function, so just to be clear, I'm referring to that function.


    buyButton.isEnabled = false

}

Upvotes: 0

Views: 53

Answers (1)

alanpaivaa
alanpaivaa

Reputation: 2089

In your main view controller:

var isLocked = true

@IBAction func addMessageViewButton(_ sender: Any) {
  if isLocked {
    // Do something for when is locked
  } else {
    // Do something for when is unlocked
  }
}

Then in your shop view controller:

func unlock() {
    let appdelegate = UIApplication.shared.delegate as! AppDelegate
    appdelegate.viewController!.isLocked = false
    buyButton.isEnabled = false
}

Upvotes: 2

Related Questions