ios - 如何让一个按钮修改另一个按钮的功能

标签 ios arrays swift uiview uibutton

我有两个 UIViewController。在第一个中,我有一个按钮可以添加一些 View ,一次一个,到主视图。在第二个中,我设置了一个商店,这样当我按下一个按钮时,我就可以解锁我应用程序的一些功能。现在,我完全知道(我希望)如何处理让 VC 通信并触发其他一些简单功能的部分,我不知道如何让商店按钮增加按钮的功能。

我需要什么:
现在该按钮最多可添加 10 个 View (完整版)。我希望在用户购买我的应用程序之前,他最多可以添加 3 个 View ,然后,当他购买它时,我已经拥有的功能(添加 10 个 View 的那个)开始工作并取代另一个。

主视图 Controller

var messageArray = [UIView] ()

我从 Storyboard 中附加了我所有的 UIView 并将它们附加到 viewDid 加载中的数组,如下所示: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
    }

商店 View Controller

我不会在这里发布所有代码,因为它太多了,当然也没有必要,因为这里要知道的重要一点是有一个按钮,如果用户按下它并继续购买,他将使我在这里发布的功能正常工作,而不是让他只有 3 个 View 的功能。

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

}

最佳答案

在你的主视图 Controller 中:

var isLocked = true

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

然后在您的商店 View Controller 中:

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

关于ios - 如何让一个按钮修改另一个按钮的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56103438/

相关文章:

ios - 如何通过代码隐藏 UISearchBar?

iphone - 便利构造函数会增加应用程序的大小吗?

ios - NSDateFormatter.dateFromString for 循环,用@try处理无效格式

javascript - 从获取的 WMS GetCapability 请求返回数组

php - 使用单个表创建多维数组

xcode - 类在/Users/... 和/Applications/... 中实现。使用两个填充之一。哪个是未定义的

ios - MasterViewController更新时如何刷新DetailViewController

ios - 我的代码似乎运行得太快

ios - 在分页模式下制作中心单元格

c - 为变量赋值会存储在错误的位置?