我们可以使用iPhone和iPad的两个单独的xib用于一个视图控制器
我为iPad创建了两个单独的xib。从iPad xib我给了viewcontroller的参考插座。我们可以使用iPhone和iPad的两个单独的xib用于一个视图控制器
我想要的是:我想使用iPhone xib的这些参考插座。
注意:我不想使用大小类。
@IBOutlet weak var zoomButton: UIButton!
var delegate: FlipsideViewControllerDelegate? = nil
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var sliderPlotStrip: F3PlotStrip!
@IBOutlet var scrollView2: UIScrollView!
@IBOutlet var sliderPlotStrip2: F3PlotStrip!
@IBOutlet var detailsButton: UIButton!
@IBOutlet var analyzeButton: UIBarButtonItem!
是啊,它是可行的,只要您加载基于设备上的厦门国际银行。
即,通过代码处理笔尖加载。在斯威夫特
解决方案:
class ViewController: UIViewController {
@IBOutlet weak var deviceLabel : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadFromNib()
}
func loadFromNib() {
//Detecting Xib name by device model
let nib = (UIDevice.currentDevice().model == "iPad") ? "iPad" : "iPhone"
//Loading the nib
let v: UIView = UINib(nibName: nib, bundle: nil).instantiateWithOwner(self, options: nil)[0] as! UIView
//Adding the loaded nib to viewcontroller's subview
view.addSubview(v)
}
}
你可以请swift提供样本参考代码吗? – Audi
从iPhone厦门国际银行的文件所有者只需设置为您在Interface Builder类。然后将参考拖动到您的插座。 在运行时,您可以选择加载哪个xib。
更新: 斯威夫特代码:为iPhone
或iPad
.xib
您的出口
switch UIDevice.currentDevice().userInterfaceIdiom {
case .Phone:
// It's an iPhone
case .Pad:
// It's an iPad
case .Unspecified:
//
}
if UIDevice.currentDevice().userInterfaceIdiom() == .Phone {
// If iPhone 5 or new iPod Touch
if UIScreen.mainScreen().bounds.size.height == 568 {
var myViewController: UIViewController = UIViewController(nibName: "ViewControllerExt", bundle: nil)
} else {
// Regular iPhone
var myViewController: UIViewController = UIViewController(nibName: "ViewController", bundle: nil)
}
// If iPad
}
else {
var myViewController: UIViewController = UIViewController(nibName: "ViewControllerPad", bundle: nil)
}
拖动引用。无需单独创建IBOutlet
只需创建一个并将其绑定到.xib
@Audi让我知道,它的工作与否? –
I在视图加载之前实现了这些逻辑。 Swift代码在这里:
if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Phone {
let controller = FlipsideViewController(nibName: "FlipsideViewController_iPhone", bundle: nil)
self.presentViewController(controller, animated: true, completion: { _ in })
}
else
{
let controller = FlipsideViewController(nibName: "FlipsideViewController", bundle: nil)
self.presentViewController(controller, animated: true, completion: { _ in })
}
如果您有2个XIB,那么会出现两个IBOutlet连接。 –
@ReshmiMajumder我不想为每个xib创建单独的参考网点。有什么办法吗? – Audi
如果您有2个XIB,那么您需要连接2个插座,否则需要连接一个故事板使用大小的类 –