委托无法调用第二视图控制器

时间:2017-07-06 08:34:13

标签: ios swift uiviewcontroller delegation swift-protocols

我想通过点击第一个视图控制器中的按钮将图像传递给第二个控制器。我无法理解即使我已经将我的1个视图控制器的引用提供给我的第二个控制器,那么代理也没有被调用

1.这是我的第一个视图控制器的代码

protocol ViewControllerDelegate: class{
func Imagine(get: UIImage?)
}
class ViewController: UIViewController  {
let button = UIButton()
weak var delegate:ViewControllerDelegate?
var image = UIImage(named:"face1.png")
override func viewDidLoad() {
    super.viewDidLoad()
    addButton()
    addConstraints()

    // Do any additional setup after loading the view, typically from a nib.
}


func addButton() -> Void {
     button.setImage(UIImage(named:"face1.png"), for: .normal)
     button.layer.borderColor = UIColor.white.cgColor
     button.clipsToBounds = true
     button.showsTouchWhenHighlighted = true
     button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
     view.addSubview(button)
}
func buttonPressed() -> Void {
    pressed()
    let alert = UIAlertController(title: "Alert", message: "Image  sent to Tab 3", preferredStyle: UIAlertControllerStyle.alert)
    let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
    alert.addAction(ok)
    self.present(alert, animated: true, completion:nil)

}

func addConstraints() -> Void {
    button.snp.makeConstraints { (make) in
        make.width.height.equalTo(150)
        make.center.equalToSuperview()
       }

}

func pressed(){
    delegate?.Imagine(get: UIImage(named:"face1.png"))
}


}

2.这是第二视图控制器的代码

class ViewController3: UIViewController, ViewControllerDelegate{
var imageView=UIImageView()
var obj:ViewController?
override func viewDidLoad() {
    super.viewDidLoad()
     view.backgroundColor = UIColor.white
    // getImage = UIImage(named: "face3.png")
     addView()
     addConstraints()
    **obj.delegate = self**
}


func addView() -> Void{

    view.addSubview(imageView)
}


func addConstraints() -> Void {
    imageView.snp.makeConstraints { (make) in
        make.width.height.equalTo(150)
        make.center.equalToSuperview()
                                  }
    }
//NOT Called
    func Imagine(get: UIImage?) {
    imageView.image = get
}
}

3.这是我的标签栏控制器

class ViewController4: UITabBarController ,UITabBarControllerDelegate {
var v:ViewController?
override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.white
    self.delegate = self
}

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

    let tab1 = ViewController()
    let tab1BarItem = UITabBarItem(title: "Tab1", image:UIImage(named:"bell-off-7.png"), selectedImage:UIImage(named:"bell-7.png") )
    tab1.tabBarItem = tab1BarItem


    let tab2 = ViewController2()
    let tab2BarItem = UITabBarItem(title: "Tab2", image: UIImage(named:"bell-off-7.png"), selectedImage: UIImage(named:"bell-7.png"))
    tab2.tabBarItem = tab2BarItem


    let tab3 = ViewController3()

    let tab3BarItem = UITabBarItem(title: "Tab3", image:     UIImage(named:"bell-off-7.png"), selectedImage: UIImage(named:"bell-7.png"))
    tab3.tabBarItem = tab3BarItem
    self.viewControllers = [tab1,tab2,tab3]
}

}

1 个答案:

答案 0 :(得分:0)

切换选项卡时必须设置图像。 目前,您正在做的是更改尚未显示的viewController的图像。

您可以为每个项目添加标记,并使用didSelectViewController委托功能

在tabBar ViewController中添加一个变量tappedImage,如

var tappedImage:UIImage?
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    let tab1 = ViewController()
    let tab1BarItem = UITabBarItem(title: "Tab1", image:UIImage(named:"bell-off-7.png"), selectedImage:UIImage(named:"bell-7.png") )
    tab1BarItem.tag = 0
    tab1.tabBarItem = tab1BarItem


    let tab2 = ViewController2()
    let tab2BarItem = UITabBarItem(title: "Tab2", image: UIImage(named:"bell-off-7.png"), selectedImage: UIImage(named:"bell-7.png"))
    tab2BarItem.tag = 1
    tab2.tabBarItem = tab2BarItem


    let tab3 = ViewController3()

    let tab3BarItem = UITabBarItem(title: "Tab3", image:     UIImage(named:"bell-off-7.png"), selectedImage: UIImage(named:"bell-7.png"))
    tab3BarItem.tag = 2
    tab3.tabBarItem = tab3BarItem
    self.viewControllers = [tab1,tab2,tab3]
}

    func Imagine(get: UIImage?) {
        tappedImage = get
    }

        func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
            if let secondVC = viewController as? ViewController2 {
              secondVC.selectedImage = tappedImage
           }
        }

在ViewController2的viewWillAppear中,将tappedImage设为imageView的图像