动态生成的按钮不会正确加载视图控制器内容

时间:2016-02-09 04:42:31

标签: xcode swift2 segue

我创建了视图控制器,创建了segue并在属性检查器中对其进行了命名,但是当我以编程方式在第二个视图控制器中创建一个按钮并向调用详细信息视图控制器添加操作时,详细信息视图控制器正在显示,但其所有控件都是没有显示。例如如果我将UITextfield添加到详细信息视图控制器,它在运行时不会显示。

在设计模式中:

enter image description here

enter image description here

代码:

enter image description here

enter image description here

但运行后,文本字段未显示:

enter image description here

前两页工作非常好,在第三页中创建动态按钮的问题。 在第三页,当我从菜单导航时,文本字段未显示:

enter image description here

第二个视图控制器代码:

//

//  SecondViewController.swift
//  BMSAppIOS
//
//  Created by Md. Muzahid-ul Islam on 1/31/16.
//  Copyright © 2016 NovoTel Limited. All rights reserved.
//

import Foundation

import UIKit


class SecondViewController: UIViewController {

    @IBOutlet weak var btnImage: UIButton!

    var userName: NSString = ""

    let baseUrl = "http://103.248.13.76/BMSRestService/values/"

    let loginParam = "GetMenuItems?LoginId="


    let picker = UIImageView(image: UIImage(named: "picker"))

    struct properties {
        static let moods = [
            ["title" : "Change Password          ", "color" : "#8647b7"],
            ["title" : "Sign Out                ", "color": "#4870b7"]
        ]
    }


    @IBAction func btnMenu(sender: UIButton) {

        picker.hidden ? openPicker() : closePicker()


    }


    func btnTouched(sender: UIButton) {

        if(sender.tag  == 0){

            self.performSegueWithIdentifier("ChPasswordIdentifier", sender: nil)


        }


        /*let alert = UIAlertController(title: "Alert", message: "you clicked menu button", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Click to remove alert", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)*/


    }

    func createPicker()
    {
        //picker.frame = CGRect(x: ((self.view.frame.width / 2) - 143), y: 200, width: 286, height: 291)
        picker.frame = CGRect(x: 10, y: 200, width: 286, height: 291)
        picker.alpha = 0
        picker.hidden = true
        picker.userInteractionEnabled = true

        var offset = 21

        for (index, feeling) in properties.moods.enumerate()
        {
            let button = UIButton()
            button.frame = CGRect(x: 2, y: offset, width: 260, height: 43)
            button.setTitleColor(UIColor(rgba: feeling["color"]!), forState: .Normal)
            button.setTitle(feeling["title"], forState: .Normal)
            button.tag = index
            button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside)

            picker.addSubview(button)

            offset += 44
        }

        view.addSubview(picker)
    }


    func openPicker()
    {
        self.picker.hidden = false

        UIView.animateWithDuration(0.3,
            animations: {
                //self.picker.frame = CGRect(x: ((self.view.frame.width / 2) - 143), y: 230, width: 286, height: 291)

                self.picker.frame = CGRect(x: 155, y: 50, width: 286, height: 291)
                self.picker.alpha = 1
        })
    }

    func closePicker()
    {
        UIView.animateWithDuration(0.3,
            animations: {
                self.picker.frame = CGRect(x: 155, y: 50, width: 286, height: 291)
                self.picker.alpha = 0
            },
            completion: { finished in
                self.picker.hidden = true
            }
        )
    }





    override func viewDidLoad() {
        super.viewDidLoad()

        createPicker()

        let restUrl=self.baseUrl + self.loginParam + String(self.userName)

        let restClient = RestClient()
        restClient.getResponse(restUrl) { responseObject, error in
            if responseObject == nil {
                print(error)
                return
            }

         if let statusCode: AnyObject = responseObject?.valueForKey("status_code"){
                if statusCode as! NSString == "200"{

                   var x = CGFloat(0)
                    var y = CGFloat(0)
                    var width = CGFloat(300)
                    var height = CGFloat(50)
                    var j = 0

                    if let menuItem = responseObject?.mutableArrayValueForKeyPath("data.MENUITEMHREF") {


                        for var i=0;i<menuItem.count;i++ {
                        print (String(menuItem[i]))
                            let imageName = String(menuItem[i]) + ".png"
                        let image = UIImage(named: imageName)
                        let button   = UIButton(type: UIButtonType.System) as UIButton

                           y = CGFloat(70*j+170)

                            if i == 0 {

                                 x = CGFloat(50)

                                 j = j + 1

                            }
                            else{

                                width = CGFloat(125)

                                if i%2 == 0 {


                                    x = CGFloat(225)
                                    j = j + 1
                                }
                                else{

                                    x = CGFloat(50)


                                }


                            }




                        button.frame = CGRectMake(x , y, width, height)
                        button.setBackgroundImage(image, forState: UIControlState.Normal)



                        //button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside)
                        self.view.addSubview(button)

                        }
                    }


                }
                else
                {
                    let alert = UIAlertController(title: "Alert", message: "Error to retrieve data", preferredStyle: UIAlertControllerStyle.Alert)
                    alert.addAction(UIAlertAction(title: "Click to remove alert", style: UIAlertActionStyle.Default, handler: nil))
                    self.presentViewController(alert, animated: true, completion: nil)
                }

            }
        }


    }





}

第三视图控制器:

import Foundation

import UIKit


class DetailsViewController: UIViewController {


    @IBOutlet weak var txtUserName: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()


        //self.view.backgroundColor = UIColor(patternImage: UIImage(named: "bg_main.png")!)


        }


    }

1 个答案:

答案 0 :(得分:1)

为任何高度选择已安装。希望它能奏效。

content of view controller attribute settings

enter image description here