簡體   English   中英

如何通過單擊按鈕在 swift 中顯示來自其他 viewController Class 的視圖

[英]How to show a view from other viewController Class in swift with button click

我有兩個 viewController 類,如這里 1)搜索購物車 viewController 2)在我的搜索購物車 controller 中搜索醫學 Viewcontroller 有一個在 storyboard 中設計的 UiView(名稱:lineItemView)。 在搜索葯物 controller 中,我有一個添加葯物的按鈕。 我想如果我點擊搜索葯物 controller 中的添加葯物按鈕,它應該會從搜索車 controller 中彈出 lineItemView 並且搜索車 controller 本身應該被隱藏。 我嘗試使用 UserDefalts,但它不起作用。 添加按鈕點擊

@IBAction func addMedicineBtnTapped(_ sender: Any) {
  
    let defaults = UserDefaults.standard
    defaults.set(true, forKey: "showLineItemView")
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyBoard.instantiateViewController(withIdentifier: "searchCart") as!  searchCart
    vc.finalcart = cart
    vc.storeCart()
    self.navigationController?.pushViewController(vc, animated: true)

}

在搜索購物車中確實加載了 function

if UserDefaults.standard.value(forKey: "showLineItemView") != nil{
              let condition = UserDefaults.standard.value(forKey: "showLineItemView") as! Bool
              if condition{
                 self.view.isHidden= true
                self.lineItemView.isHidden = false
                UserDefaults.standard.set(false, forKey: "showLineItemView")
              }
         }

解決方案:

@IBAction func addMedicineBtnTapped(_ sender: Any) {
  
    let defaults = UserDefaults.standard
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyBoard.instantiateViewController(withIdentifier: "searchCart") as!  searchCart
    vc.finalcart = cart
    vc.storeCart()
    vc.showLineItemView = true
    vc.modalPresentationStyle = .overFullScreen
    //push as NavigationController
    //self.navigationController?.pushViewController(vc, animated: true)
    //push as UIViewController
    self.present(vc, animated: false, completion: nil) 

}


class searchCart:UIViewController{
    var showLineItemView = false

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if(showLineItemView){
            self.view.backgroundColor = .clear
            //disable all other UI
            self.lineItemView.isHidden = false
        }
    }
}

使用另一個viewcontroller子視圖不是一個好主意,嘗試為lineItemView制作組件UI(XIB),讓lineItemView由原始viewcontroller控制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM