我们可以使用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! 
+0

如果您有2个XIB,那么会出现两个IBOutlet连接。 –

+0

@ReshmiMajumder我不想为每个xib创建单独的参考网点。有什么办法吗? – Audi

+0

如果您有2个XIB,那么您需要连接2个插座,否则需要连接一个故事板使用大小的类 –

是啊,它是可行的,只要您加载基于设备上的厦门国际银行。

即,通过代码处理笔尖加载。在斯威夫特

解决方案:

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) 

    } 
} 
+0

你可以请swift提供样本参考代码吗? – Audi

从iPhone厦门国际银行的文件所有者只需设置为您在Interface Builder类。然后将参考拖动到您的插座。 在运行时,您可以选择加载哪个xib。

更新: 斯威夫特代码:为iPhoneiPad.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

+0

@Audi让我知道,它的工作与否? –

只需创建以下结构即可。

  • ViewController.swift
  • ViewController.xib
  • 的ViewController〜ipad.xib

不会有任何需要在运行时来识别要加载的厦门国际银行。

通过使用〜ipad它会自动识别。

我想要的是:我想使用iPhone xib的这些参考插座。

只需复制这一观点来自iPad的厦门国际银行控制器,并把它在iPhone厦门国际银行

保持大小为“自由”

enter image description here

现在你就能够调整UI组件和所有网点会是一样的。

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 }) 
      }