在iOS开发中使用子视图时遇到问题

时间:2016-01-04 02:33:18

标签: ios swift uiview viewcontroller subview

我一直在努力尝试在我的iOS开发中使用子视图。我一直试图通过将所有UI和布局代码放在子视图中来遵循最佳实践,但这对我来说相当棘手,我需要回答一下我应该如何正确地解决这个问题,因为我不能似乎在网上找到了可以直接回答我问题的任何内容。

我有一个视图在后台有一张图片,两个要自定义的文本字段和一个按钮被自定义为在其周围有一个白色边框。我的子视图模糊了背景,设计了两个文本字段和一个XIB文件上显示的按钮。我将此视图连接到我的故事板上的视图,我需要能够控制从故事板上的uiview文件显示的按钮拖动到我的viewController以创建按钮的操作。 viewController文件属于故事板上的视图,而不属于XIB文件中的视图。所以,我的子视图文件中有一个该按钮的插座连接,我需要在viewController文件中为该按钮建立一个动作连接。显然我无法做到这一点,因为该按钮是XIB文件的一部分。如果有人可以通过使用子视图和故事板告诉我如何以不同的方式解决这个问题,那么我可以设计这个按钮,还可以在我的viewController文件中使用它,同时仍然遵循MVC模式?如果我做错了什么或任何事情,我们将非常感谢任何单独的建议。

这是子视图代码:

@IBDesignable class SignUpView: UIView {

@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var signUpButton: UIButton!
@IBOutlet weak var image: UIImageView!

var view: UIView!

override init(frame: CGRect) {
    super.init(frame: frame)
    configure()
}

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

func configure() {
    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]

    //Add blur
    self.view.backgroundColor = UIColor.clearColor()
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = self.view.bounds
    blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

    self.image.addSubview(blurEffectView)

    //Design text fields
    designTextField(usernameTextField, placeHolder: "Choose username")
    designTextField(passwordTextField, placeHolder: "Choose password")

    signUpButton.layer.cornerRadius = 5
    signUpButton.layer.borderWidth = 2
    signUpButton.layer.borderColor = UIColor(white: 1.0, alpha: 0.7).CGColor

    addSubview(view)
}

func designTextField(textField: UITextField, placeHolder: String) {
    textField.backgroundColor = UIColor.clearColor()
    textField.borderStyle = UITextBorderStyle.None
    textField.attributedPlaceholder = NSAttributedString(string: placeHolder, attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])

}

func addBorder(textField: UITextField) {
    let border = CALayer()
    let width = CGFloat(2.0)
    border.borderColor = UIColor.whiteColor().CGColor
    border.opacity = 0.7 as Float
    border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width:  textField.frame.size.width, height: textField.frame.size.height)
    border.borderWidth = width
    textField.layer.addSublayer(border)
    textField.layer.masksToBounds = true
}


func loadViewFromNib() -> UIView {

    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "SignUpView", bundle: bundle)
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

    return view
    }
}

viewController代码:

class SignUpViewController: UIViewController, UITextFieldDelegate {

//The action below needs to be connected to the button from the subview file
@IBAction func toggleSignUpButton(sender: AnyObject) {

}

func displayAlert(title: String, message: String) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}


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

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

0 个答案:

没有答案