2014-10-29 30 views
0

我正將舊應用程序從.xib-s遷移到.storyboard-s。這些.xib文件有很多IBOutlet連接,並且複製&粘貼視圖並重新創建它們非常耗時。我想知道是否有辦法做到這一點無縫。任何人都知道解決這個問題?從.xib複製到.storyboard並保留IBOutlet連接

+0

我認爲只要您將ViewController類更改爲您的實際控制器類,Xcode就會自動執行此操作。你不能做任何事情,但如果xcode不會自動完成的話。 – iphonic 2014-10-29 10:23:09

回答

0

是的,有一種方法。

  1. 確保故事板視圖控制器場景具有爲視圖控制器設置的正確類。

  2. 右鍵單擊xib文件,然後選擇打開爲 - >源代碼。

  3. 找到<subviews> ... </subviews>對標籤,並複製該部分。

  4. 以同樣的方式打開故事板(打開爲 - >源代碼)並在xml中找到相應的視圖控制器(用xml註釋標記,例如<!--MyViewController>),然後從xib複製和粘貼xml子視圖,裏面的視圖控制器<view> ... </view>標籤。例如

    <viewController id="vXZ-lx-hvc" customClass="TwoViewController" sceneMemberID="viewController"> 
          <layoutGuides> 
           <viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/> 
           <viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/> 
          </layoutGuides> 
          <view key="view" contentMode="scaleToFill" id="kh9-bI-dsS"> 
           <rect key="frame" x="0.0" y="0.0" width="320" height="568"/> 
           <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> 
           <subviews> 
            <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fLe-1C-hTu"> 
             <rect key="frame" x="116" y="244" width="46" height="30"/> 
             <state key="normal" title="Button"> 
              <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/> 
             </state> 
            </button> 
           </subviews> 
           <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> 
          </view> 
         </viewController> 
    
  5. 重複與<connections> ... </connections>以上步驟,在所有IBOutlets複製。需要注意的是連接去<view> .. </view>標籤的下面,但裏面的<viewController> ... </viewController>

    <viewController id="vXZ-lx-hvc" customClass="TwoViewController" sceneMemberID="viewController"> 
           <layoutGuides> 
            <viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/> 
            <viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/> 
           </layoutGuides> 
           <view key="view" contentMode="scaleToFill" id="kh9-bI-dsS"> 
            <rect key="frame" x="0.0" y="0.0" width="320" height="568"/> 
            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> 
            <subviews> 
             <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fLe-1C-hTu"> 
              <rect key="frame" x="116" y="244" width="46" height="30"/> 
              <state key="normal" title="Button"> 
               <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/> 
              </state> 
              <connections> 
               <action selector="samButtonTap:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="1sQ-0S-S8z"/> 
              </connections> 
             </button> 
            </subviews> 
            <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> 
           </view> 
           <connections> 
            <outlet property="samButton" destination="fLe-1C-hTu" id="8GK-Nn-Loy"/> 
           </connections> 
          </viewController> 
    

你會從第二片斷IBActions與觀點,其中作爲IBOutlets中表示在一起後子視圖有代表注意到已被宣佈。

根據xib文件的複雜程度,您可能需要做更多的工作。如果您決定重新創建一些xib組件,而不是將它們複製到ID中,則會發生更改,因此請注意這一點。一旦完成,不要忘記從項目中刪除你的xib文件。

+1

我想過這個,但希望有另一種方式。現在誰想要編寫一個能夠自動完成的應用:) – 2015-03-06 14:25:35