从alert message-swift保存输入

时间:2015-11-09 21:29:09

标签: ios swift uitableview uialertcontroller

我遇到了一个问题,导致我无法将警报消息的输入显示在表格视图中。没有错误,但我不知道是什么导致表空的真正问题。

import UIKit
import Parse
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{

    @IBOutlet weak var childTbl: UITableView!
    var names=[String] ()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.childTbl.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    }
    @IBAction func Addchild(sender: AnyObject) {
        var alert=UIAlertController(title: "",
            message: "إضافة طفل جديد",
            preferredStyle: .Alert)
        alert.addTextFieldWithConfigurationHandler({(textField) -> Void in
          textField.text=""

            })


        alert.addAction((UIAlertAction(title: "حفظ", style: .Default, handler: { (ACTION) -> Void in

            self.dismissViewControllerAnimated(true, completion: nil)



            if  let textField = alert.textFields!.first as?UITextField{

               self.names.append(textField.text)
                println(self.names)
            }
        })))
        alert.addAction((UIAlertAction(title: "إلغاء", style: .Default, handler: { (ACTION) -> Void in

            self.dismissViewControllerAnimated(true, completion: nil)

        })))


        //to display the alert
        presentViewController(alert,
            animated: true,
            completion: nil)
    }

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

    //UITabledatasource
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return  self.names.count;
    }
    //function to creat a cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        var cell:UITableViewCell = self.childTbl.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

        cell.textLabel?.text = self.names[indexPath.row]

        return cell
    }

}

0 个答案:

没有答案