D
D
Dmitry2014-11-23 19:48:57
Swift
Dmitry, 2014-11-23 19:48:57

How to implement Delegation in swift?

Hello. I am writing an application for Iphone, I have a difficulty.
let there be a ViewController (with a transition button and a label) and a TableViewController, let's say they filled the table and there, for example, in cell 3, it says "man". click on it and go to the ViewController. How to do. So that at the same time in the view the text of the label changes to a person. Please write in more detail how to implement this through Delegation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Flie, 2014-11-27
@Flie

Well, here it is.

import UIKit

class SomeViewController: UIViewController {
    @IBOutlet var myLable: UILabel!
    
    var someValue: String = "ноль"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        myLable.text = someValue
    }
}


class NextTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
    let array = ["один", "два", "три"]
    let sequeIdentifiers = "go"
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let selected = indexPath.row
        
        self.performSegueWithIdentifier(sequeIdentifiers, sender: selected)
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destControl = segue.destinationViewController as SomeViewController
        if let indexFromSender: Int = sender?.integerValue{
            destControl.someValue = array[indexFromSender]
        }
        else{println("Not Integer")}
        
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
        if cell == nil{
          cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
        }
        
        cell!.textLabel?.text = array[indexPath.row]
        return cell!
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question