构造Swift UIViews和UIViewControllers的正确方法是什么?

时间:2016-08-04 17:59:08

标签: ios swift

UIViewController是否只管理UIViews的显示?或者它是否也用于控制其中的内容?

例如,现在我目前在我的UIViewController中有代码,它根据按钮点击从一个视图切换到下一个视图,如下所示:

class TTViewController: UIViewController {

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

    //other code 

    @IBAction func OpenUserSelectView(sender: AnyObject) {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "PlayerSelectView", bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        super.view.addSubview(view)
    } 
}

但我也有UIViews做的事情,比如创建按钮,对这些按钮有动作,以及其他看起来像这样的东西:

class PlayerSelectView :UIScrollView {
    let viewWidth = CGFloat(300)
    let viewHeight = CGFloat(500)
    var addPlayerButton :TTBlueButton?
    var cancel :TTBlueButton?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let screenSize: CGRect = UIScreen.mainScreen().bounds
        self.frame = CGRectMake((screenSize.width/2) - (viewWidth/2), (screenSize.height/2) - (viewHeight/2), viewWidth, viewHeight)

        styleView()
        addAddPlayerButton()

        self.contentSize = CGSizeMake(viewWidth, viewHeight-100)
        self.userInteractionEnabled = true
    }

    func styleView() {
        super.layer.cornerRadius = 8.0
        super.layer.shadowColor = UIColor.blackColor().CGColor
        super.layer.shadowOpacity = 0.2
        super.layer.shadowRadius = 4.0
        super.layer.shadowOffset = CGSizeMake(0.0, 5.0)
    }

    func addAddPlayerButton(){
        addPlayerButton = TTBlueButton(frame: CGRectMake(10, 10, viewWidth-20, 40))
        addPlayerButton!.setTitle("Add New Player", forState: UIControlState.Normal)
        addPlayerButton!.layer.cornerRadius = 5.0
        addPlayerButton!.userInteractionEnabled = true
        addPlayerButton!.addTarget(self, action: #selector(PlayerSelectView.addPlayer(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        self.addSubview(addPlayerButton!)
    }

    func addCancelButton(){
        addPlayerButton = TTBlueButton(frame: CGRectMake(10, 10, viewWidth-20, 40))
        addPlayerButton!.setTitle("Cancel", forState: UIControlState.Normal)
        addPlayerButton!.layer.cornerRadius = 5.0
        addPlayerButton!.userInteractionEnabled = true
        addPlayerButton!.addTarget(self, action: #selector(PlayerSelectView.addPlayer(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        self.addSubview(addPlayerButton!)
    }

    @IBAction func addPlayer(sender: AnyObject) {

        if addPlayerButton?.currentTitle != "Create Player" {
            let addPlayerTextField = TextField(frame: CGRect(x: 10, y: 10, width: viewWidth - 20, height: 40))
            let border = CALayer()
            let width = CGFloat(2.0)
            addPlayerButton!.setTitle("Create Player", forState: UIControlState.Normal)
            border.borderColor = UIColor.darkGrayColor().CGColor
            border.frame = CGRect(x: 0, y: 0, width:  addPlayerTextField.frame.size.width, height: addPlayerTextField.frame.size.height)
            border.borderWidth = width
            border.cornerRadius = 5
            addPlayerTextField.layer.addSublayer(border)
            addPlayerTextField.layer.masksToBounds = true

            self.addSubview(addPlayerTextField)
            addPlayerButton?.frame.origin = CGPoint(x: 10, y: 60)
        } else {

        }

    }
}

关于我做这件事的方式感觉不对,所以我想知道我的怀疑是否正确,或者这是否是可以接受的方式。

2 个答案:

答案 0 :(得分:1)

你可以做任何你喜欢的事情,但在我看来,所出现的视图的代码配置会进入控制器(例如视图控制器 - 注意名称)。

您不必在一个视图控制器中执行所有操作 - 您可以嵌套视图控制器 - 但您显示的是控制器代码,并且(在我看来)应该在视图控制器中。

答案 1 :(得分:1)

  

顾名思义,View Controller管理一组视图,并且是   类UIViewController的一个实例。他们的工作是管理   视图大小调整,布局,与交互作出反应等   视图,处理动作或触摸等事件,以及协调   数据模型甚至其他视图控制器。

你需要使用导航控制器学习视图naigation.beacuse在上面的代码中你要在你的uiviewcontroller视图中添加一个视图,而且代码也有一些内存泄漏。

创建一个或多个子视图控制器并在该视图控制器上执行您想要的操作。之后当您从父视图控制器单击按钮时,使用uinavigationcontroller推送您的子视图控制器