Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

ios - Pass data back from a popped ViewController using Swift

I would like to know how do we pass a data back from a popped ViewController

FirstViewController -----push----> SecondViewController

SecondViewController -----popped(Pass Value?) ----> FirstViewController 

I have searched around and found many solutions asking to use delegates, but those are in Objective C which I am not familiar with.

How do we do this in Swift?

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually delegates are not just available in Objective C. Delegation is available in Swift (anything that does not involve the dynamic nature of Objective-C is able in Swift) and delegation is a design pattern (delegation as a design pattern), not a language implementation. You can use one of two methodologies, blocks/closures, or delegation. An example of delegation in swift can be found in Apple's documentation as referenced here:

Apple documentation on delegation

You may also see references for Apple's documentation on closures here: Apple documentation on closures

An example of delegation can be shown below:

Noted that the delegate is declared via the protocol below:

protocol DiceGame {
    var dice: Dice { get }
    func play()
}
protocol DiceGameDelegate {
    func gameDidStart(game: DiceGame)
    func game(game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int)
    func gameDidEnd(game: DiceGame)
}

The class checks if it has a delegate, if it does, it calls the methods the class must implement by conforming to the protocol above

  class SnakesAndLadders: DiceGame {
        let finalSquare = 25
        let dice = Dice(sides: 6, generator: LinearCongruentialGenerator())
        var square = 0
        var board: [Int]
        init() {
            board = [Int](count: finalSquare + 1, repeatedValue: 0)
            board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
            board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
        }
        var delegate: DiceGameDelegate?
        func play() {
            square = 0
            delegate?.gameDidStart(self)//Calls the method gameDidEnd on the delegate passing self as a parameter
            gameLoop: while square != finalSquare {
                let diceRoll = dice.roll()
                delegate?.game(self, didStartNewTurnWithDiceRoll: diceRoll)
                switch square + diceRoll {
                case finalSquare:
                    break gameLoop
                case let newSquare where newSquare > finalSquare:
                    continue gameLoop
                default:
                    square += diceRoll
                    square += board[square]
                }
            }
            delegate?.gameDidEnd(self)//Calls the method gameDidEnd on the delegate passing self as a parameter
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...