Ios 从另一个类访问uiviewController元素
我正在处理这个项目,但是我的故事板的viewcontroller中的元素有问题,我想从另一个类更改它的属性! 我的第一种方法是在第二个类中从viewcontroller实例化一个对象!它在运行时返回nilIos 从另一个类访问uiviewController元素,ios,swift,xcode,uiviewcontroller,uielement,Ios,Swift,Xcode,Uiviewcontroller,Uielement,我正在处理这个项目,但是我的故事板的viewcontroller中的元素有问题,我想从另一个类更改它的属性! 我的第一种方法是在第二个类中从viewcontroller实例化一个对象!它在运行时返回nil func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { // let mainstampvc = MainStampVc().s
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// let mainstampvc = MainStampVc().storyboard?.instantiateViewController(withIdentifier: "mainstampvc") as? MainStampVc
// mainstampvc?.setstampimage(imageURL: list_images[indexPath.row])
let msvc = mainstampvc()
mainstampvc?.setstampimage(imageURL: list_images[indexPath.row])
}
我的第二个方法是在我的第二个类中再次实例化整个viewcontroller,它什么也不做
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainstampvc = MainStampVc().storyboard?.instantiateViewController(withIdentifier: "mainstampvc") as? MainStampVc
mainstampvc?.setstampimage(imageURL: list_images[indexPath.row])
}
我想要的就是当我点击我的uicollectionviewcell时,改变我的一个主ViewController视图的背景。这是我所有的课
viewcontroller.swift
import Foundation
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var stampholder: UIView!
@IBAction func TextViewButton(_ sender: Any) {
removerSubViews()
addSubView(ViewName: "text")
}
@IBAction func AViewButton(_ sender: Any) {
removerSubViews()
addSubView(ViewName: "mohr")
}
@IBAction func BorderViewButton(_ sender: Any) {
}
@IBAction func DlViewButton(_ sender: Any) {
}
@IBOutlet weak var holderView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
addSubView(ViewName: "mohr")
let mainstampvc = self.storyboard?.instantiateViewController(withIdentifier: "mainstampvc")
let mainstampview = mainstampvc?.view
mainstampview?.frame = stampholder.frame
stampholder.addSubview((mainstampview)!)
}
func removerSubViews(){
for view in self.holderView.subviews{
view.removeFromSuperview()
}
}
func addSubView(ViewName: String)
{
if let subview = Bundle.main.loadNibNamed(ViewName, owner: self, options: nil)?.first as? UIView {
self.holderView.addSubview(subview);
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
mohrcollectionview.swift
import Foundation
import UIKit
class MohrCollectionViewController: UIView,UICollectionViewDataSource,UICollectionViewDelegate{
var mohrPath: String = ""
var fileManager: FileManager!
var list_images : [String] = []
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fileManager = FileManager.default
let currentDir = Bundle.main.resourcePath
mohrPath = currentDir!
let mohrsPath = try? fileManager.contentsOfDirectory(atPath: mohrPath + "/mohr")
list_images = mohrsPath!
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return list_images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
collectionView.register(UINib(nibName: "mohrcell", bundle: nil), forCellWithReuseIdentifier: "mohrcell")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mohrcell", for: indexPath) as! mohrCellController
let image = UIImage(named: list_images[indexPath.row])
cell.cellimage.image = image
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainstampvc = MainStampVc().storyboard?.instantiateViewController(withIdentifier: "mainstampvc") as? MainStampVc
mainstampvc?.setstampimage(imageURL: list_images[indexPath.row])
}
}
斯威夫特
import Foundation
import UIKit
class MainStampVc: UIViewController{
@IBOutlet weak var stampimage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
setstampimage(imageURL: "golbanafsh.png")
}
public func setstampimage(imageURL: String)
{
stampimage.image = UIImage(named: imageURL)
}
}
任何帮助都将不胜感激
2.
这是我的授权代码,但仍然没有:(
我做错了什么吗?您可以在构建UIViewController时将UIViewController引用传递给MohrCollectionViewController(您应该调用此MohrCollectionView以避免混淆)。然后,无论何时需要更新背景,都可以调用引用上的相关函数
class ViewController : UIViewController {
...
override func viewDidLoad() {
...
let view = addSubView(ViewName: "mohr")
view?.vc = self
}
func addSubView(ViewName: String) -> UIView?
{
if let subview = Bundle.main.loadNibNamed(ViewName, owner: self, options: nil)?.first as? UIView {
self.holderView.addSubview(subview);
return subview
}
}
return nil
}
class MohrCollectionView {
func updateVcBackground() {
vc?.updateBackground()
}
var vc : ViewController? = nil
}
更简洁的方法是使用委托。委托使用协议定义两个类之间的接口
protocol UpdateBackgroundDelegate : class {
func updateBackground()
}
class ViewController : UIViewController, UpdateBackgroundDelegate {
...
override func viewDidLoad() {
...
let view = addSubView(ViewName: "mohr")
view?.updateBackgroundDelegate = self
}
func updateBackground() {
// update your background in this funcion
}
}
class MohrCollectionView {
func updateVcBackground() {
updateBackgroundDelegate?.updateBackground()
}
var updateBackgroundDelegate : UpdateBackgroundDelegate? = nil
}
要使学员工作,请执行以下操作:
protocol UpdateBackgroundDelegate : class {
func updateBackground(imageURL: String)
}
var updateDelegate: UpdateBackgroundDelegate?
updateDelegate.updateBackground(imageUrl: yourUrl)
collectionView.updateDelegate = self
updateDelegate.updateBackground(imageUrl: yourUrl)
collectionView.updateDelegate = self
class ViewController :UpdateBackgroundDelegate {
func updateBackground(imageUrl: yourUrl) {
//write code to load image from url
}
}
应用程序的结构是什么?是否要从第二个控制器更改第一个控制器上的颜色?是否有推送层次结构、模式…?@sci3nt15t请明确问题所在。请显示相关代码。这将有助于更好地理解问题,并快速解决问题。部分问题在于您有一个自定义的
UIView
你调用一个控制器,它向另一个视图提供数据。这使得结构很难使用。你能分享你的示例项目和适当的要求吗?更改MainStampVc或MohrCollectionViewController的颜色???@Stefan我实际处理的是我想从mohrcollectionviewc更改我的MainStampVc的背景图像这不是一个视图控制器,而是一个uiview!非常感谢你的帮助,我完全忘记了授权!但现在我有问题,我更新我的问题,你能帮我吗?