我向现有项目添加了一个带有接口 (.h)、实现 (.m) 文件和 UI (.xib) 的自定义视图控制器类。
当我在笔尖和界面之间按住 Ctrl 键单击并拖动时,拖动时不会放置插座或 IBAction,因此我无法将 UI 元素链接到属性。另外,当我在助理编辑器模式下查看笔尖时,XCode 不会调出相应的界面文件。
注意:我更改了自定义类的名称。我更改了文件名以及源代码中对其的引用。
为了创建 xib 文件,我添加了一个新的 UIViewController 的可可触摸文件子类,并选中了“也创建 XIB 文件”。
我的 xib 文件中只有一个标签。
在我的视图控制器中:
import UIKit
class TestControllerViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Run Code Online (Sandbox Code Playgroud)
除了我的 IBoutlet 之外别无他物(我至少检查了连接检查器 10 次)。
和我的自定义类:
import Foundation
import UIKit
class Card: UIView {
internal var number:Int?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) …Run Code Online (Sandbox Code Playgroud) 我正在研究框架。在框架内部,我在 Xib 中创建了UIViewController -> UICollectionView。为 UICollectionView 单元格创建了单独的 Xib。并将单元格注册为“TaskCollectionViewCell”
我尝试为 collectionView 注册 nib,它崩溃并显示无法在捆绑包中加载 NIB:NSBundle
这是我的 UIViewController -> UICollectionView 代码:
override public func viewDidLoad() {
super.viewDidLoad()
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.register(UINib(nibName: "TaskCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "TaskCollectionViewCell")
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TaskCollectionViewCell", for: indexPath) as! TaskCollectionViewCell
return cell
}
Run Code Online (Sandbox Code Playgroud)
控制台错误:
2019-11-12 12:08:00.487285+0530 …Run Code Online (Sandbox Code Playgroud) 有没有办法检测用户使用的设备,然后使用该信息,使应用程序使用特定的nib文件?
现在,我的代码是
- (BOOL)application: (UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions{
if(isiPhone5){
self.RootViewController = @"RootViewController_iPhone5";
}
else{
self.RootViewController = @"RootViewController_iPhone4";
}
}
Run Code Online (Sandbox Code Playgroud)
我找到设备的其他代码可以工作并告诉我用户正在使用什么设备,但实际上并没有将.xib文件更改RootViewController_iPhone4.xib为RootViewController_iPhone5.xib.有没有办法做到这一点?
我不想使用自动布局,因为我想根据设备发生其他自定义事件,例如视图控制器中的不同按钮名称,如果使用的设备是iPhone 4和iPhone 5
我在.xib文件中有一个按钮.
我希望如果我点击那个按钮.应该改变viewController的.xib文件颜色.
我怎样才能做到这一点?
我应该使用NSNotification还是有其他方法可以做到这一点?