How to create a UITableView selection parameter similarly in iOS settings in Swift?

I am developing an application that requires users to select one option from a table view list. This is similar to the ringtone in iPhone settings. I have attached a screen illustration of exactly what I'm trying to accomplish.

Now I want this parameter to be selected for sending as the value for my PHP Script with HTTPMethod. This is the type of query I use, I think it is just a matter of storing data in a variable.

HTTP Demo Request

   let requestURL = NSURL(string: "http://example.com/script.php")
   let request = NSMutableURLRequest(URL: requestURL!)
   request.HTTPMethod = "POST"

Illustration: enter image description here

I understand that this is a beginner, but I have not found a tutorial that does this quickly.

+4
source share
3 answers

All this can be done with segue.

  • , viewController. show segue
  • , unwind segue, . segue .
  • .
+2

I recorded code examples. Check this.

If you have questions, ask me whenever you want.

MainViewController

class MainViewController: UIViewController, ListViewControllerProtocol {

    @IBOutlet weak var button:UIButton?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func toList() {
        var listViewController = ListViewController(nibName: "ListViewController", bundle: nil)
        listViewController.delegate = self
        self.pushViewController(listViewController, animated: true)
    }

    @IBAction func buttonClicked(sender: UIButton) {
        self.toList()
    }

    func onSelectObj(obj:NSDictionary) {
        // set data to button
    }
}

Listviewcontroller

class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView:UITableView?

    var delegate:ListViewControllerProtocol?

    var identifier = "ListViewController"
    var data = []

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: identifier)

        self.updateData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: UITableViewDataSource
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return data.count
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 37
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! UITableViewCell

        var row = UInt(indexPath.row)

        var obj = self.data[0] as! NSDictionary
        cell.textLabel?.text = obj["name"]

        return cell;
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        var obj = self.data[0] as! NSDictionary
        self.selectObj(obj)
    }

    private func selectObj(obj: NSDictionary) {
        self.delegate?.onSelectObj(obj)
        self.navigationController?.popViewControllerAnimated(true)
    }

    private func updateData() {
        // Code for getting data

        self.data = someDeserializedData

        self.tableView!.reloadData() // reload!
    }
}

protocol ListViewControllerProtocol {
    func onSelectObj(obj:NSDictionary)
}
0
source

All Articles