【发布时间】:2020-08-06 16:53:16
【问题描述】:
一个简单的配置文件页面,根据我在设置页面上选择的内容显示图像,控制器和视图的不同文件,单击配置文件的勾号转到设置页面,选择 image1 或 image2 并且该图像必须显示在配置文件中页面,我尝试在设置上创建一个协议以便能够添加图像,然后在配置文件视图文件上实现一个委托,以便它可以更新它不起作用的图像,任何人都可以指出我的错误
设置控制器
import UIKit
protocol ShowImage: class {
func displayImage(_ of: UIImage)
}
class SettingsController: UIViewController {
weak var delegate: ShowImage?
override func viewDidLoad() {
super.viewDidLoad()
let settings = SettingsView()
view.addSubview(settings.view)
settings.btn1.addTarget(self, action: #selector(dCode), for: .touchUpInside)
// Do any additional setup after loading the view.
}
@objc func dCode() {
let image = UIImage(named: "homei")
delegate?.displayImage(image!)
navigationController?.pushViewController(ProfileController(), animated: true)
}
}
设置视图
import UIKit
class SettingsView: UIViewController {
var btn1 = UIButton()
var btn2 = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
view.translatesAutoresizingMaskIntoConstraints = false
view.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height).isActive = true
view.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
view.backgroundColor = UIColor.white
btn1.heightAnchor.constraint(equalToConstant: 30).isActive = true
btn1.widthAnchor.constraint(equalToConstant: 150).isActive = true
btn1.setTitleColor(UIColor.red, for: .normal)
btn1.backgroundColor = UIColor.green
btn2.heightAnchor.constraint(equalToConstant: 30).isActive = true
btn2.widthAnchor.constraint(equalToConstant: 150).isActive = true
btn2.setTitleColor(UIColor.red, for: .normal)
btn2.backgroundColor = UIColor.green
btn1.setTitle("Image1", for: .normal)
btn2.setTitle("Image2", for: .normal)
let stackP = UIStackView()
stackP.axis = .horizontal
stackP.alignment = .top
stackP.spacing = 10
stackP.distribution = .fill
stackP.addArrangedSubview(btn1)
stackP.addArrangedSubview(btn2)
stackP.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackP)
stackP.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stackP.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
// Do any additional setup after loading the view.
}
}
配置文件控制器
import UIKit
class ProfileController: UIViewController {
let profile = ProfileView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(profile.view)
// Do any additional setup after loading the view.
profile.settingsBtn.addTarget(self, action: #selector(gotoSettings), for: .touchUpInside)
}
@objc func gotoSettings(){
let settings = SettingsController()
navigationController?.pushViewController(settings, animated: true)
}
}
个人资料视图
import UIKit
class ProfileView: UIViewController, ShowImage{
func displayImage(_ of: UIImage) {
apply(img: of)
}
var bgImage = UIImageView()
var settingsBtn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
view.translatesAutoresizingMaskIntoConstraints = false
view.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height).isActive = true
view.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
bgImage.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bgImage)
bgImage.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height).isActive = true
bgImage.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
settingsBtn.heightAnchor.constraint(equalToConstant: 60).isActive = true
settingsBtn.widthAnchor.constraint(equalToConstant: 60).isActive = true
// settingsBtn.setTitle("Settings", for: .normal)
settingsBtn.setImage(UIImage(named: "tick"), for: .normal)
settingsBtn.backgroundColor = UIColor.red
settingsBtn.layer.cornerRadius = 5
view.addSubview(settingsBtn)
settingsBtn.translatesAutoresizingMaskIntoConstraints = false
settingsBtn.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
settingsBtn.topAnchor.constraint(equalTo: view.topAnchor, constant: 70).isActive = true
let set = SettingsController()
set.delegate = self
// Do any additional setup after loading the view.
}
func apply(img: UIImage)
{
bgImage.image = img
}
}
在此处输入图片说明
【问题讨论】:
-
请注意
ProfileController和ProfileView中创建的SettingsController()实例是不同的。在后者中,您创建实例,设置委托,然后将其丢弃。 -
@vadian,谢谢,如果我点击按钮一并选择一个图像,我怎样才能让它出现在配置文件页面上,这是空白的,我应该让配置文件的视图或配置文件的控制器委托
-
显然您不使用情节提要,因此您有责任维护视图的生命周期和引用。
-
@vadian,谢谢,所以我想知道如果我想在 viewDidLoad 中发生一些事情,就像在我需要让委托发生的地方,但是如何在视图文件中发生某些事情,这个协议委托是否会有所帮助我可以这样做吗?
-
如果对象在视图层次结构中彼此相关并且您对两者都有(强)引用,则协议/委托很有用。