我想知道是否有可能推送到新的viewController,同时保持对进行推送的VC上数据的访问。我想知道是否有一种方法可以完成此操作,或者我是否可以以某种方式使特定信息在全球范围内可用于其他视图?
例如,我的用户名和图像是从profileVC的firebase中检索的,我想按下一个按钮,将其发送到另一个同时显示该名称和图像的ViewController。
我将在下面显示如何称呼此信息。
class userProfileHeader: UICollectionViewCell {
var user: User? {
didSet {
configureMessageAcceptButton()
let name = user?.name
nameLabel.text = name
guard let profileImageUrl = user?.profileImageUrl else { return }
profileImageView.loadImageUsingCacheWithUrlString(profileImageUrl)
}
}
lazy var acceptButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Loading", for: .normal)
button.layer.cornerRadius = 3
button.layer.borderColor = GREEN_Theme.cgColor
button.layer.borderWidth = 0.5
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.setTitleColor(GREEN_Theme, for: .normal)
button.addTarget(self, action: #selector(handleAcceptRequest), for: .touchUpInside)
return button
}()
let profileImageView: UIImageView = {
let image = UIImageView()
image.image = UIImage(named: "users")
image.translatesAutoresizingMaskIntoConstraints = false
image.contentMode = .scaleAspectFill
return image
}()
let nameLabel: UILabel = {
let label = UILabel()
label.text = "Jane John Doe"
label.font = UIFont.systemFont(ofSize: 30)
label.numberOfLines = 0
label.textColor = UIColor.black
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(imageView)
imageView.anchors(top: topAnchor, topPad: 0, bottom: bottomAnchor, bottomPad: 0, left: leftAnchor, leftPad: 0, right: rightAnchor, rightPad: 0, height: 100, width: 200)
setupProfileImageView()
setupNameView()
}
func setupProfileImageView() {
imageView.addSubview(self.profileImageView)
self.profileImageView.anchors(top: nil, topPad: 0, bottom: nil, bottomPad: 0, left: imageView.leftAnchor, leftPad: 16, right: nil, rightPad: 0, height: 100, width: 100)
self.profileImageView.translatesAutoresizingMaskIntoConstraints = false
self.profileImageView.centerYAnchor.constraint(equalTo: self.imageView.centerYAnchor).isActive = true
self.profileImageView.layer.zPosition = 10
self.profileImageView.layer.cornerRadius = 50
self.profileImageView.layer.masksToBounds = true
self.profileImageView.contentMode = .scaleAspectFill
self.profileImageView.image=#imageLiteral(resourceName: "user")
}
func setupNameView() {
imageView.addSubview(self.nameLabel)
self.nameLabel.anchors(top: profileImageView.bottomAnchor, topPad: 10, bottom: nil, bottomPad: 0, left: profileImageView.leftAnchor, leftPad: 0, right: nil, rightPad: 0, height: 30, width: 100)
self.nameLabel.translatesAutoresizingMaskIntoConstraints = false
self.nameLabel.layer.zPosition = 10
self.nameLabel.centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
}
@objc func handleAcceptRequest() {
let user = self.user
let username = user?.name
let alert = UIAlertController(title: "Are you Sure? ",message:" Would you like to accept \(username!) to complete your job?", preferredStyle: UIAlertController.Style.alert)
let cancelButton = UIAlertAction(title: "Cancel", style: .default, handler: {(_ action: UIAlertAction) -> Void in
print("you pressed Yes, please button")
})
let continueButton = UIAlertAction(title: "Continue", style: .default, handler: {(_ action: UIAlertAction) -> Void in
self.saveUserData(didAccept: "yes")
let acceptWorker = acceptWorkerViewController()
let navController = UINavigationController(rootViewController: acceptWorker)
self.window?.rootViewController?.present(navController, animated: true, completion: nil)
print("you pressed Continue")
})
continueButton.setValue(GREEN_Theme, forKey: "titleTextColor")
cancelButton.setValue(UIColor.red, forKey: "titleTextColor")
alert.addAction(cancelButton)
alert.addAction(continueButton)
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
fileprivate func configureMessageAcceptButton() {
guard let currentUID = Auth.auth().currentUser?.uid else { return }
guard let user = self.user else { return }
if currentUID != user.uid {
imageView.addSubview(self.acceptButton)
imageView.addSubview(self.sendMessageButton)
self.acceptButton.anchors(top: profileImageView.topAnchor, topPad: 0, bottom: nil, bottomPad: 0, left: nil, leftPad: 0, right: imageView.rightAnchor, rightPad: 16, height: 40, width: 120)
self.sendMessageButton.anchors(top: acceptButton.bottomAnchor, topPad: 16, bottom: nil, bottomPad: 0, left: acceptButton.leftAnchor, leftPad: 0, right: nil, rightPad: 0, height: 40, width: 120)
acceptButton.setTitle("Accept Request", for: .normal)
sendMessageButton.setTitle("Send Message", for: .normal)
} else {
print("this is the currentUser profile")
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
很抱歉,我只想展示代表我在说什么的所有代码。在func handleAcceptRequest()中,这就是我推送到新ViewController的方式。推送时是否可以保留此viewController内部的数据?
这里是Master-> Details场景的一些伪代码,其中detail viewController需要访问Master viewController中的数据。我认为这可能会回答您的问题
class MasterViewController: UITableViewController {
var detailViewController: DetailViewController? = nil
var some_string = "Hello, World"
func showDetailController() {
//code to instantiate the detailViewController
detailViewController.myParentViewContoller = self
}
.
.
.
}
然后是细节
class DetailViewController: UIViewController {
var myParentViewController: : MasterViewController? = nil
.
.
.
func printMessage() {
let message = self.myParentViewController.some_string
print(message)
}
}
请注意,可能有34种以上的方法可以完成相同的操作;您可以通过segue传递数据或从主数据库中填充明细变量等。