在运行时

时间:2016-12-02 20:40:50

标签: swift3

Swift新手在这里,试图学习代码。我有一个javascript背景。我在故事板中使用嵌套的堆栈视图设计了一个简单的界面: enter image description here

层次结构是

Vertical Stack
   -Horizontal Stack   
      -Vertical Stack
        Label
        Textfield
      -Vertical Stack
        Label
        TextField

我基本上设置了一个类似于显示的表,我希望每次用户点击“添加成员”按钮时都会复制该表。它需要复制那里的内容并将其添加到顶层堆栈。添加一个由两个垂直堆栈组成的新水平堆栈,其中包含标签和文本字段。

当我按下按钮时没有任何反应。也没有错误。我使用打印来确认按钮事件正在触发。

这是控制器代码:

class createCrewController: UIViewController{
    //MARK: Properties

    var memberList:UIStackView!
    override func viewDidLoad() {
        super.viewDidLoad()
        memberList = view.viewWithTag(1) as! UIStackView!;
        // Do any additional setup after loading the view.
    }

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


    var rowArray = [UIStackView]()
    //MARK: Actions
    @IBAction func addMemberBTN(_ sender: UIButton) {
        //Create name and TextField
        let nameLabel = UILabel();
        nameLabel.text = "Name";
        let nameTextField = UITextField();
        nameTextField.placeholder = "Enter Name";
        let phoneLabel = UILabel();
        phoneLabel.text = "Name";
        let phoneTextField = UITextField();
        phoneTextField.placeholder = "Enter Phone";
        //Insert them into vertical Stack
        let nameRow = UIStackView(arrangedSubviews: [nameLabel,nameTextField]);
        nameRow.axis = .vertical;
        let phoneRow = UIStackView(arrangedSubviews: [phoneLabel,phoneTextField]);
        phoneRow.axis = .vertical;
        let columnView = UIStackView(arrangedSubviews:[nameRow,phoneRow]);
        columnView.axis = .horizontal;

        self.memberList.addSubview(columnView);
    }

0 个答案:

没有答案