Iphone 如何添加在故事板中创建的UIView作为Swift中的弹出窗口?

Iphone 如何添加在故事板中创建的UIView作为Swift中的弹出窗口?,iphone,swift,uiview,uiviewcontroller,ios-app-group,Iphone,Swift,Uiview,Uiviewcontroller,Ios App Group,我做了以下工作: 1.拖动UIView以将其放置在ViewController功能区中 创建自定义UIView类并将插座添加到“编辑纵断面图”视图字段: 导入UIKit 类EditProfileView:UIView{ let globalDataHandler = GlobalDataHandler.sharedInstance @IBOutlet weak var firstNameTextField: UITextField! @IBOutlet weak var lastNameTe

我做了以下工作: 1.拖动UIView以将其放置在ViewController功能区中

  • 创建自定义UIView类并将插座添加到“编辑纵断面图”视图字段:
  • 导入UIKit

    类EditProfileView:UIView{

    let globalDataHandler = GlobalDataHandler.sharedInstance
    
    @IBOutlet weak var firstNameTextField: UITextField!
    @IBOutlet weak var lastNameTextField: UITextField!
    @IBOutlet weak var userNameTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var emailTextField: UITextField!
    @IBAction func saveEditButton(sender: AnyObject) {
    }
    @IBAction func cancelEditButton(sender: AnyObject) {
    }
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
    }

  • 已将“EditProfileView”类作为情节提要中“Edit Profile View”视图的自定义类

  • 在ProfileViewController中为“EditProfileView”类创建了一个对象,并在单击ProfileViewController中的编辑按钮时将“编辑纵断面图”视图添加到主视图中

  • 类ProfileViewController:UIViewController{

    let profileView = EditProfileView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func editProfileButton(sender: AnyObject) {
        profileView.firstNameTextField.text = "First Name"
        profileView.lastNameTextField.text = "Last Name"
        profileView.userNameTextField.text = "User Name"
        profileView.emailTextField.text = "Email"
        let testFrame : CGRect = CGRectMake(50,100,300,300)
        profileView.frame = testFrame
        self.view.addSubview(profileView)
    }
    
    }

    但是,“编辑纵断面图”视图未显示在ProfileViewController上。
    请提供帮助。

    您必须在Xib中链接您的UIView和您的视图(应该是不同的UIViewController)

    profileView = storyboard.instantiateViewControllerWithIdentifier("ProfileViewController")
    
    您的ProfileVC必须有一个UIPresentationController

    class ProfilePresentationController : UIPresentationController {
        override func frameOfPresentedViewInContainerView() -> CGRect {
            return CGRect(x: (presentingViewController.view.frame.width / 2.0) - 150.0, y: (presentingViewController.view.frame.height / 2.0) - 100.0, width: 300.0, height: 200.0)
        }
    }
    
    EditProfileView必须实现以下委托:

    UIViewControllerTransitioningDelegate
    
    profileView的以下属性也必须设置为:

    profileView.modalPresentationStyle = UIModalPresentationStyle.Custom
    profileView.transitioningDelegate = self
    
    之后,您可以实现委托所需的方法

    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
            return ProfilePresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
    

    您必须在Xib中链接UIView和视图(应该是不同的UIViewController)

    profileView = storyboard.instantiateViewControllerWithIdentifier("ProfileViewController")
    
    您的ProfileVC必须有一个UIPresentationController

    class ProfilePresentationController : UIPresentationController {
        override func frameOfPresentedViewInContainerView() -> CGRect {
            return CGRect(x: (presentingViewController.view.frame.width / 2.0) - 150.0, y: (presentingViewController.view.frame.height / 2.0) - 100.0, width: 300.0, height: 200.0)
        }
    }
    
    EditProfileView必须实现以下委托:

    UIViewControllerTransitioningDelegate
    
    profileView的以下属性也必须设置为:

    profileView.modalPresentationStyle = UIModalPresentationStyle.Custom
    profileView.transitioningDelegate = self
    
    之后,您可以实现委托所需的方法

    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
            return ProfilePresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
    

    为什么不通过在文档大纲中选择“编辑纵断面图”并将控件拖动到
    profileView控制器中,将您拖动到情节提要中的视图制作成
    profileView
    和IBOutlet?在类似于
    EditProfileView()
    的代码中初始化它将不会产生预期效果,因为有重写的初始值设定项可以设置子视图。为什么不通过选择“编辑概要视图”使
    profileView
    和IBOutlet成为拖入情节提要的视图在文档大纲和控件中拖动到您的
    ProfileViewController
    ?在类似于
    EditProfileView()
    的代码中初始化它不会产生预期的效果,因为有重写的初始值设定项可以设置子视图。