gpt4 book ai didi

ios - 实现可重用和可配置的 ViewController

转载 作者:行者123 更新时间:2023-11-28 07:57:36 25 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我需要跨多个父 ViewControllers 的通用模式 ViewController(我们称它为 GetData)。 GetData 需要是可配置的(如隐藏/显示一些 Views)。 GetData 应该能够将数据返回到调用 ViewController

根据我对 SO 和网络上其他资源的阅读,这是我尝试实现它的简化方式:

// ViewController.swift

import UIKit

class ViewController: UIViewController, GetDataDelegate {

// This shows the data received from GetData
@IBOutlet weak var textviewResult: UITextView!

// Call GetData with only mandatory and without optional views
@IBAction func onClickGetData(_ sender: Any) {
let getData = GetData(caller: self)
getData.display(delegate: self)
}

// Call GetData with both mandatory and optional data views
@IBAction func onClickGetDataWithOptional(_ sender: Any) {
let getData = GetData(caller: self, hasOptional: true)
getData.display(delegate: self)
}

// Delegate function to received data sent by GetData
func gotData(data1: String, data2: String!) {
textviewResult.text = "Mandatory: \(data1)"
+ (data2 != nil ? "\nOptional: \(data2!)" : "")
}
}

// GetData.swift

import UIKit

protocol GetDataDelegate {
func gotData(data1: String, data2: String!)
}

class GetData: UIViewController {

@IBOutlet weak var textfieldMandatory: UITextField!
@IBOutlet weak var textfieldOptional: UITextField!
// View with the optional views
@IBOutlet weak var stackViewOptional: UIStackView!

var delegate:GetDataDelegate?
var caller:UIViewController?
var hasOptional:Bool?

init(caller: UIViewController, hasOptional: Bool? = false) {
super.init(nibName: "GetData", bundle: nil)

self.caller = caller
self.hasOptional = hasOptional
}

func display(delegate:GetDataDelegate) {
self.delegate = delegate

let viewController = UIStoryboard(name: "GetData", bundle: nil).instantiateViewController(withIdentifier: "GetData")
viewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
viewController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve

caller?.present(viewController, animated: true, completion: nil)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

@IBAction func onSubmit(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
delegate?.gotData(data1: textfieldMandatory.text!, data2: hasOptional! ? textfieldOptional.text! : nil)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// Configure GetData (hide/show views)
// But hasOptional is nil!
//stackViewOptional?.isHidden = !hasOptional!
}
}

我面临以下问题:

  1. delegate func 未在 GetData 中调用。
  2. 我无法更改 GetData views(在 func viewWillAppear 中)。<

以上两个似乎是因为members delegate(在func onSubmit)和hasOptional(在 func viewWillAppear 中)即使在初始化后也是 nil

需要进行哪些更正或者我需要采用不同的方法?

注意:以上是一种简化。在我的实际用例中,有多个 View Controller 将调用 GetData,有或没有可选的 Views

最佳答案

我认为这会更好地用容器来实现——你可以在一个屏幕上有多个 View Controller 。戴夫·德隆写道 great articles关于这种方法。

关于ios - 实现可重用和可配置的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47606785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com