一、簡介
StoryBoard 是蘋果提供的用於描述和展示 iOS 應用程序界面的一種技術。它可以方便地讓開發者設置應用程序的用戶界面,並且可視化操作方便快捷。使用 StoryBoard 可以省去很多代碼,簡化開發難度,使得開發者可以更快地開發出更加漂亮、複雜的用戶界面。
二、優勢
1、可視化操作
使用 Xcode 編輯 StoryBoard 時,它會提供可視化的編輯器。你可以在該編輯器中直觀地看到應用程序的完整界面,以及各個控件之間如何相互交互。這使得開發人員在界面設計和設置中更加輕鬆自如,縮短開發時間。
2、OOP的編程方式
viewController 這個對象在 storyboard 中出現時,和 storyboard 中其他元素一樣,可以通過連接線連接起來,變成一個完整的圖形化的整體來方便管理和操作我們的視圖控制器中的各類控件。通過 StoryBoard 中的 Object 在 viewController 中的添加或刪除也可以視為『基於對象』編程的方式來體現,這體現了一種 OOP(面向對象編程)的編程風格。
3、隨時修改
使用 StoryBoard 設計用戶界面後,可以通過拖拽、改動、設置屬性等方式隨意修改,再利用 Interface Builder 精確設置各個控件的位置及布局。而且 StoryBoard 就是申明式定義用戶界面的,而非命令式。因此,修改起來簡單、直觀並且方便。
4、便於理解和維護
StoryBoard 中每個界面元素之間的關係用連接線表示,界面之間的轉換與應用程序流程清晰明了。同時 StoryBoard 實現了 MVC 分離設計模式,將 View 和 Controller 分離開,結構清晰直觀。這樣代碼更加容易理解和維護。
三、代碼示例
下面是一個 StoryBoard 操作的示例代碼,通過拖拽、設置屬性、連接等方式創建了一個按鈕並實現點擊事件。
<?xml version="1.0" encoding="UTF-8"?> <!-- storyboard --> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="-BFh-h-fWh"> <dependencies> <!-- 框架依賴 --> <deployment identifier="iOS"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13090"/> <capability name="Storyboard.XIB" minToolsVersion="5.1"/> </dependencies> <scenes> <!-- view controller --> <scene sceneID="LfS-bh-mtq"> <objects> <!-- view controller --> <viewController id="-Bh-h-fWh" customClass="ViewController" customModuleProvider="" sceneMemberID="viewController"> <layoutGuides> <viewControllerLayoutGuide type="top" id="fdf-bN-JFZ"/> <viewControllerLayoutGuide type="bottom" id="At0-j3-RjH"/> </layoutGuides> <view key="view" contentMode="scaleToFill" id="KrV-1L-x1e"> <rect key="frame" x="0.0" y="0.0" width="414" height="896"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/> <constraints> <constraint firstItem="KrV-1L-x1e" firstAttribute="width" secondItem="Q5X-mv-u6j" secondAttribute="width" multiplier="1:1" id="Zem-6v-09B"/> <constraint firstItem="KrV-1L-x1e" firstAttribute="height" secondItem="Q5X-mv-u6j" secondAttribute="height" multiplier="1:1" id="ySS-Vt-4Nx"/> </constraints> <subviews> <!-- button --> <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" id="C6z-uQ-Ck6"> <rect key="frame" x="45" y="406" width="296" height="84"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <color key="backgroundColor" red="1" green="0.41703509926795864" blue="0.31852233457565596" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> <:state key="normal" title="Click Me"> <color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> <:/state> <:connections> <!-- button 點擊事件 --> <action selector="buttonClick:" destination="-Bh-h-fWh" eventType="touchUpInside" id="tKu-rM-Om3"/> <:/connections> </button> </subviews> </view> <simulatedMetricsContainer key="defaultSimulatedMetrics"> <simulatedStatusBarMetrics key="statusBar"/> </simulatedMetricsContainer> <simulatedTopBarMetrics key="topBar"/> <simulatedBottomBarMetrics key="bottomBar"/> <simulatedOrientationMetrics key="orientation"/> <point key="point" x="0.5" y="0.5"/> </viewController> <placeholder placeholderIdentifier="IBFirstResponder" id="OYz-Va-Qx4" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="312" y="430.6666666666667"/> </scene> </scenes> </document>
ViewController 的代碼示例
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @IBAction func buttonClick(_ sender: UIButton) { print("你好,這是一個按鈕") } }
四、總結
StoryBoard 是一個非常強大的 iOS 應用程序開發工具,能夠極大地簡化應用程序的界面開發,代碼量也會減少很多,同時也有利於代碼的維護以及團隊協作。開發者在利用 StoryBoard 開發應用程序的時候,應該結合自己的實際情況,合理地運用 StoryBoard 的各種優點,減小程序開發的難度,並在實際問題中不斷摸索,掌握更為精細的技巧。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/227327.html