2016-07-27 28 views
0

我爲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個插座,否則需要連接一個故事板使用大小的類 –

回答

0

是啊,它是可行的,只要您加載基於設備上的廈門國際銀行。

即,通過代碼處理筆尖加載。在斯威夫特

解決方案:

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

2

從iPhone廈門國際銀行的文件所有者只需設置爲您在Interface Builder類。然後將參考拖動到您的插座。 在運行時,您可以選擇加載哪個xib。

更新: 斯威夫特代碼:爲iPhoneiPad.xib您的出口

switch UIDevice.currentDevice().userInterfaceIdiom { 
case .Phone: 
    // It's an iPhone 
case .Pad: 
    // It's an iPad 
case .Unspecified: 
    // 
} 
0
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讓我知道,它的工作與否? –

2

只需創建以下結構即可。

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

不會有任何需要在運行時來識別要加載的廈門國際銀行。

通過使用〜ipad它會自動識別。

我想要的是:我想使用iPhone xib的這些參考插座。

只需複製這一觀點來自iPad的廈門國際銀行控制器,並把它在iPhone廈門國際銀行

保持大小爲「自由」

enter image description here

現在你就能夠調整UI組件和所有網點會是一樣的。

0

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 }) 
      } 
相關問題