![](/img/trans.png)
[英]How to pass data to another ViewController without using a button in swift
[英]Hiding an imageView using a button in another ViewController Swift XCode
我需要在 Swift 中使用 ViewControllerOne 中的按鈕隱藏 ViewControllerTwo 中的 ImageView。
我如何在另一個 ViewController 中使用“imageView.hidden = true”?
請問有什么幫助嗎? 謝謝
在你的按鈕函數中試試這個(在 viewController1 中):
@IBAction func yourButtonFunction(_ sender: AnyObject){
let defaults = UserDefaults.standard
defaults.set(true, forKey: "isImageHidden")
}
在 viewController2 中,添加以下內容:
override func viewDidAppear(){
super.viewDidAppear(true)
if UserDefaults.standard.value(forKey: "isImageHidden") != nil{
let condition = UserDefaults.standard.value(forKey: "isImageHidden") as! Bool
if condition{
//should be hidden
self.imageView.isHidden = true
UserDefaults.standard.set(false, forKey: "isImageHidden")
}
}
}
讓我知道這是否有幫助!
您不應該嘗試從另一個視圖控制器更改一個視圖控制器的視圖。 那是糟糕的設計。 相反,您應該使要隱藏其圖像的視圖控制器具有委托屬性。 創建一個委托協議來定義其他視圖控制器調用的消息。 這是一種非常常見的設計模式,您應該學習。
網上有很多教程展示了委托設計模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.