我在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")
}
}
答案 0 :(得分:3)
接受答案的问题在于只关注视图本身,这不足以使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()
如果没有其他方法,您可能会在许多方面发现故障,例如:
参考: https://developer.apple.com/reference/uikit/uiviewcontroller
"实现容器视图控制器"
在将子视图的根视图添加到视图层次结构之前,容器视图控制器必须将子视图控制器与其自身关联
答案 1 :(得分:1)
在你的项目中弹出控制器是UIViewController的子类,有几个方法你可以像这样添加视图:
self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController", bundle: nil)
self.view.addSubview(popViewController)
您可以参考教程了解如何创建Pop up window in Swift或Adding a subview using a XIB and Storyboard
答案 2 :(得分:0)
这是您真正想要的。
主要我们可以使用自定义.xib
来加载自定义UITableViewCell, UICollectionViewCell
和自定义弹出窗口
UITableViewCell
创建一对自定义.xib及其类文件(例如CustomCell.Swift
和CustomCell.xib
)
转到项目导航器,然后右键单击创建名为CustomCell
的新类,其子类为UITableViewCell
转到项目导航器并创建具有相同类名的新.xib文件
新文件..->用户界面->视图->另存为您的类名“ CustomCell
”->创建
现在您将获得带有UIView子类的.xib文件,但是我们需要一个UITableViewCell
,所以我们该怎么做?我们删除该UIView文件并添加一个UITableViewCell
,并同时给该类名CustomCell
和标识符。 (按Command + Shift + L)UITableView,然后根据您的项目需求拖放到xib窗口和设计单元中。
现在我们将移至代码部分并在ViewContoller
类文件中初始化.xib并将其加载到UITableView
中。
我们将从捆绑包中获取单元格,并在UITableView中注册该单元格。
UITableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
cellForRowAt indexPath
中的单元格 let cell : CustomCell = self.(YourTableView).dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
return cell
UICollectionViewCell
注意:重复上述步骤,将UITableViewCell
替换为UICollectionViewCell
并像这样注册UICollectionViewCell
self.collectionView.register(UINib(nibName: "CollectoinCustCell", bundle: nil), forCellWithReuseIdentifier: "CollectoinCustCell")
cellForRowAt indexPath
中的单元格 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : CollectoinCustCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectoinCustCell", forIndexPath: indexPath) as! CollectoinCustCell
return cell
}
作为弹出窗口的“自定义视图”(结束于上面问题的适当答案)
创建一对xib文件,这一次您不必从.xib文件中删除UIView。
从捆绑包中获取xib,例如:
let CustomXIB = Bundle.main.loadNibNamed("CustomXIB", owner: nil, options: nil)?.first as? CustomXIB
在viewWillAppear
CustomXIB?.frame = UIScreen.main.bounds
创建appDelegate实例文件以在主窗口上添加xib文件:您的AppDelegate将如下所示。
var AppInstance: AppDelegate!
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
AppInstance = self
}
}
AppInstance.window?.addSubview(self.CustomXIB!)
self.CustomXIB?.removeFromSuperview()