gpt4 book ai didi

ios - 如何在 ViewController 中将 xib 作为 "pop up"调出?

转载 作者:可可西里 更新时间:2023-11-01 00:41:41 26 4
gpt4 key购买 nike

我在 Xcode 中设计了一个很好的登录框作为 xib 文件,当用户在我的应用程序的登录页面上点击“登录”时,我希望登录框弹出并让他们输入他们的信息.

目前我很难让它出现,因为这是我第一次使用 xibs。任何帮助,将不胜感激。似乎我的 IBAction 没有连接,因为我做了第二堂课,它断开了登录按钮与 IBAction 的连接。不确定如何解决这个问题。

提前致谢!在 Xcode 上使用 Swift 3 和最新版本。

import UIKit

class AuthMainViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.isHidden = true
}
}

class loginClass: UIView {

class func instanceFromNib() -> UIView {
return UINib(nibName: "LoginView.xib", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}

@IBAction func loginBtnTapped(_ sender: Any) {
let loginView = loginClass.instanceFromNib()
self.addSubview(loginView)
print("Login got pressed")
}
}

最佳答案

接受答案的问题是 in 只处理 View 本身,这不足以让 UIKit 完全正常运行。为了拥有功能齐全的 UIViewController,你应该使用这个:

//adding UIViewController to another UIViewController:
addChildViewController(viewController)
view.addSubview(viewController.view)
viewController.view.frame = view.bounds
viewController.didMove(toParentViewController: self)

//removing UIViewController from another UIViewController:
viewController.willMove(toParentViewController: nil)
viewController.view.removeFromSuperview()
viewController.removeFromParentViewController()

如果没有其他方法,您可能会发现很多方面的故障,例如:

  • 外观方法
  • 旋转方法
  • 过渡方法
  • 响应链
  • 导航 Controller 、标签栏 Controller 属性

引用: https://developer.apple.com/reference/uikit/uiviewcontroller

“实现容器 View Controller ”

Your container view controller must associate a child view controller with itself before adding the child's root view to the view hierarchy

关于ios - 如何在 ViewController 中将 xib 作为 "pop up"调出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42750997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com