Swift 代表 - 新手 3 ViewControllers

Posted

技术标签:

【中文标题】Swift 代表 - 新手 3 ViewControllers【英文标题】:Swift delegates - Newbie 3 ViewControllers 【发布时间】:2015-12-28 03:01:31 【问题描述】:

所以这是我的问题。 我从 Swift 和 C# 开始了我的旅程,我正在尝试以最简单的方式学习委托。

我创建了一个 xcode 项目,在其中设置了 3 个视图控制器。 我创建了 2 个转场:

    我可以从 firstViewController (firstVC) 转到 secondViewController (secondVC) 我可以从 firstViewController 转到thirdViewController (thirdVC)

应用目的:

    从 firstVC 转到 secondVC,点击一个按钮,关闭视图并根据点击的按钮更新标签。 -- 这个很简单。

    从第一个 VC 转到第三个 VC,点击那里唯一的按钮,关闭视图并使用一些简单的文本更新与以前相同的标签 -- 这也完成了。

    如果我点击 thirdVC 中的按钮,我想更改 secondVC 中的标签

第三个对我来说并不容易。

代码如下:

FirstViewController:

import UIKit

class FirstViewController: UIViewController, SecondViewControllerDelegate, ThirdViewControllerDelegate 

@IBOutlet weak var answerLabel: UILabel!

override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.



override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    if segue.identifier == "firstToSecondSegue" 
        let secondVC = segue.destinationViewController as! SecondViewController
        secondVC.delegate = self
     else if segue.identifier == "firstToThirdSegue" 
        let thirdVC = segue.destinationViewController as! ThirdViewController
        thirdVC.delegate = self
    


@IBAction func answerButtonTapped(sender: UIButton) 
    if sender.tag == 0 
        performSegueWithIdentifier("firstToSecondSegue", sender: self)
     else if sender.tag == 1 
        performSegueWithIdentifier("firstToThirdSegue", sender: self)
    


func giveTheAnswer(yes: Bool) 
    if yes 
        print("Yes is the answer")
        self.answerLabel.text = "I do like chicken!"
     else 
        print("No is the answer")
        self.answerLabel.text = "I do not like chicken!"
    


func iWantPie(kindOfPie: String, disableButton: Bool) 
    if kindOfPie == "Apple pie" 
        print("Textlabel will change to applepie.")
        self.answerLabel.text = "Fu*k chicken. Let's eat apple pie!"
    
    if disableButton 
        print("DisableButton set to true")
        self.answerLabel.backgroundColor = UIColor.redColor()
    


SecondViewController:

protocol SecondViewControllerDelegate 
func giveTheAnswer(yes: Bool)


class SecondViewController: UIViewController, ThirdViewControllerDelegate 

@IBOutlet weak var theOnlyLabel: UILabel!
@IBOutlet weak var yesButton: UIButton!
@IBOutlet weak var noButton: UIButton!

var delegate: SecondViewControllerDelegate?

override func viewDidLoad() 
    super.viewDidLoad()


    // Do any additional setup after loading the view.


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


@IBAction func answerButtonPressed(sender: UIButton) 

    if sender.tag == 0 
        //Yes tapped
        self.delegate?.giveTheAnswer(true)
        dismissViewControllerAnimated(true, completion: nil)
     else if sender.tag == 1 
        //No Tapped
        self.delegate?.giveTheAnswer(false)
        dismissViewControllerAnimated(true, completion: nil)
    


func iWantPie(kindOfPie: String, disableButton: Bool) 
    print("Function iWantPie in secondViewController is Running")
    if disableButton == true 
        print("DisableButton running")
        self.theOnlyLabel.text = "Pie was tapped"
    


第三视图控制器:

protocol ThirdViewControllerDelegate 
func iWantPie(kindOfPie: String, disableButton: Bool)

class ThirdViewController: UIViewController 

var delegate: ThirdViewControllerDelegate?


override func viewDidLoad() 
    super.viewDidLoad()    

    // Do any additional setup after loading the view.


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


@IBAction func piePressed(sender: UIButton) 
    self.delegate?.iWantPie("Apple pie", disableButton: true)
    dismissViewControllerAnimated(true, completion: nil)


【问题讨论】:

你好,我看不出SeconVC 和ThirdVC 同时存在吗? 意思是你想在第3个按btn,然后回到第1个然后去第2个看看变化?如果是,那么流程是一样的,但是在第一次你不更新任何东西,而是将它传递给第二个,你不能真正调用第二个委托,因为如果你不从第二个去第三个,它就没有初始化 正如@view vu 所指出的那样,我认为在segue 堆栈中没有第二个和第三个VC 的可能性。你要么从 1->2 要么 1->3,除了你在代码中提到的那些之外,还有其他的 segue 吗? 本例中没有 Objective-C。可以去掉 Objective-C 标签吗? 【参考方案1】:

请通过在 if else 条件中添加断点来检查以下代码是否正常工作。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    if segue.identifier == "firstToSecondSegue" 
        let secondVC = segue.destinationViewController as! SecondViewController
        secondVC.delegate = self
     else if segue.identifier == "firstToThirdSegue" 
        let thirdVC = segue.destinationViewController as! ThirdViewController
        thirdVC.delegate = self
    

还要检查您是否为情节提要中的 segue 提供了正确的 segue 标识符。 "firstToSecondSegue" 和 "firstToThirdSegue"

【讨论】:

它正在运行。我的意思是:SecondVC 连接到 FirstVC,ThirdVC 也连接到 FirstVC。但是如何将 ThirdVC 连接到 SecondVC 而无需在它们之间创建 segue?

以上是关于Swift 代表 - 新手 3 ViewControllers的主要内容,如果未能解决你的问题,请参考以下文章

代表不再被称为 swift

从 Objc 超类继承的 Swift 子类中未调用 viewDidLoad

ios开发之Swift新手入门

Swift新手教程系列5-函数+selector在swift中的使用方法

UITapGestureRecognizer 干扰 UISlider

SWIFT4:如何让两个点击手势识别器一起工作