我有两个UIViewController
。在第一个视图中,我有一个按钮,可以向主视图中一次添加一些视图。在第二个中,我建立了一个商店,以便在按下按钮时可以解锁应用程序的某些功能。现在,我完全知道(希望)如何处理使VC通讯并触发其他一些简单功能的部分,但我不知道如何使存储按钮增加按钮的功能。
我需要什么:
现在,该按钮最多可以添加10个视图(完整版本)。我希望在用户购买我的应用程序之前,他最多可以添加3个视图,然后在购买时,我已经拥有的功能(一个可以添加10个视图)开始工作并替换另一个。
主视图控制器
var messageArray = [UIView] ()
我从情节提要中附加了我所有的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
}
商店视图控制器
在这里,我不会发布所有代码,因为它太多了,当然也没有必要,因为重要的是要知道这里有一个按钮,如果用户按下它并继续购买,他将会使我在此处发布的功能正常运行,而不是使他只有3个视图的功能。
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
}
答案 0 :(得分:2)
在主视图控制器中:
var isLocked = true
@IBAction func addMessageViewButton(_ sender: Any) {
if isLocked {
// Do something for when is locked
} else {
// Do something for when is unlocked
}
}
然后在商店视图控制器中:
func unlock() {
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.viewController!.isLocked = false
buyButton.isEnabled = false
}