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的參考也委託不叫
1.Heres我的代碼。這裏是第二個視圖控制器
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.And這裏的代碼是我的標籤欄控制器
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]
}
}
雅是多數民衆贊成我想問我怎麼能在切換之間 – vasu
沒有這是行不通的 – vasu
什麼是錯誤你得到了什麼? –