override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case "profile"? :
if let destination = segue.destination as? Profile{
destination.username = "demo"
}
break
case "contact"?:
if let destination = segue.destination as? Contact{
destination.email = "email1@xxxxx_com"
}
break
default:
print("no segue has been called")
break
}
}
在其他ViewControllers中我有如下的全局变量:
class Profile: UIViewController {
var username = ""
override func viewDidLoad() {
super.viewDidLoad()
print("username : ", username)
}
但是当Profile ViewController启动时,username的值保持为空!表示我所更新的内容将再次丢失,变量将再次获得原始值!!!
已更新: 备注:我要编辑的值是Profile.swift,但是segue连接到ProfileMain.swift。我在主ViewController" ProfileMain"上使用了一个容器。喜欢这里的截图图片: screenshot1
故事板的演示布局:
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--ProfileMain-->
<scene sceneID="y2Q-vO-42x">
<objects>
<viewController id="vZJ-mp-RUK" userLabel="ProfileMain" customClass="ProfileMain" customModule="Segue_with_Data" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="OXb-l2-lpG">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<containerView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="3k0-LA-yQx">
<rect key="frame" x="27" y="68" width="320" height="226"/>
<constraints>
<constraint firstAttribute="width" constant="320" id="74O-jJ-2Oy"/>
<constraint firstAttribute="height" constant="226" id="vwt-h9-yTg"/>
</constraints>
<connections>
<segue destination="R2w-XN-4gi" kind="embed" id="Wv6-xm-hP9"/>
</connections>
</containerView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="3bp-10-M6F">
<rect key="frame" x="0.0" y="20" width="375" height="40"/>
<color key="backgroundColor" red="0.57312941549999996" green="0.57311660050000002" blue="0.57311910389999998" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="RL2-dG-8Ax"/>
</constraints>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
<state key="normal" title="Close Profile">
<color key="titleColor" red="0.1221005991" green="0.12947699430000001" blue="0.1412781179" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</state>
<connections>
<action selector="BtnCloseProfile:" destination="vZJ-mp-RUK" eventType="touchUpInside" id="XdI-21-0cF"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="0.49327188729999999" blue="0.47399842739999998" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="3bp-10-M6F" firstAttribute="trailing" secondItem="TFs-jh-JJy" secondAttribute="trailing" id="3mA-uw-h7K"/>
<constraint firstItem="3k0-LA-yQx" firstAttribute="centerX" secondItem="OXb-l2-lpG" secondAttribute="centerX" id="JKC-5A-PFW"/>
<constraint firstItem="3bp-10-M6F" firstAttribute="top" secondItem="TFs-jh-JJy" secondAttribute="top" id="Ksn-Rt-FAW"/>
<constraint firstItem="3k0-LA-yQx" firstAttribute="top" secondItem="3bp-10-M6F" secondAttribute="bottom" constant="8" id="TjK-LS-dPm"/>
<constraint firstItem="3bp-10-M6F" firstAttribute="leading" secondItem="TFs-jh-JJy" secondAttribute="leading" id="lO3-oZ-h4E"/>
</constraints>
<viewLayoutGuide key="safeArea" id="TFs-jh-JJy"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="R5o-du-dms" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-1055.2" y="117.39130434782609"/>
</scene>
<!--ProfileMain-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" userLabel="ProfileMain" customClass="ViewController" customModule="Segue_with_Data" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="A20-gr-1Kl">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="mycell" rowHeight="130" id="3wm-VQ-0zg" customClass="TableViewCell" customModule="Segue_with_Data" customModuleProvider="target">
<rect key="frame" x="0.0" y="28" width="375" height="130"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="3wm-VQ-0zg" id="o3N-Ob-VA8">
<rect key="frame" x="0.0" y="0.0" width="375" height="129.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="deW-A9-4Vv">
<rect key="frame" x="0.0" y="35" width="375" height="30"/>
<color key="backgroundColor" red="0.610782767" green="0.66666668650000005" blue="0.5935411089" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="zpb-Fb-z4M"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="30"/>
<state key="normal" title="contact">
<color key="titleColor" red="0.1215686275" green="0.12941176469999999" blue="0.14117647059999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<segue destination="esD-m9-R0s" kind="show" identifier="contact" id="EhF-dw-AgB"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="6ad-cu-gKu">
<rect key="frame" x="0.0" y="73" width="375" height="30"/>
<color key="backgroundColor" red="0.8828310989" green="0.93938174539999997" blue="0.95077186820000004" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<constraints>
<constraint f***irstAttribute="height" constant="30" id="Gnm-SA-5vV"/>
</constraints>
<fontDescription*** key="fontDescription" type="system" pointSize="30"/>
<state key="normal" title="Profile">
<color key="titleColor" red="0.1221005991" green="0.12947699430000001" blue="0.1412781179" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</state>
<connections>
<segue destination="vZJ-mp-RUK" kind="show" identifier="profile" id="aFR-aF-KsG"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="deW-A9-4Vv" firstAttribute="top" secondItem="o3N-Ob-VA8" secondAttribute="top" constant="35" id="9FJ-oa-bNS"/>
<constraint firstAttribute="trailing" secondItem="6ad-cu-gKu" secondAttribute="trailing" id="9q8-5C-CmJ"/>
<constraint firstItem="deW-A9-4Vv" firstAttribute="leading" secondItem="o3N-Ob-VA8" secondAttribute="leading" id="Gmy-Ld-JrK"/>
<constraint firstItem="6ad-cu-gKu" firstAttribute="top" secondItem="deW-A9-4Vv" secondAttribute="bottom" constant="8" id="VRl-X7-BeG"/>
<constraint firstItem="6ad-cu-gKu" firstAttribute="leading" secondItem="o3N-Ob-VA8" secondAttribute="leading" id="mLt-gu-lm7"/>
<constraint firstAttribute="trailing" secondItem="deW-A9-4Vv" secondAttribute="trailing" id="rWS-2B-EE7"/>
</constraints>
</tableViewCellContentView>
</tableViewCell>
</prototypes>
</tableView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<connections>
<outlet property="tableview1" destination="A20-gr-1Kl" id="3X7-ec-Qm2"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-98.400000000000006" y="118.29085457271366"/>
</scene>
<!--View Controller-->
<scene sceneID="dLf-bm-qRT">
<objects>
<viewController id="esD-m9-R0s" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="lpj-V1-Wi3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="0.8828310989" green="0.93938174539999997" blue="0.95077186820000004" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<viewLayoutGuide key="safeArea" id="r8n-lk-rjP"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="l65-qO-Rsu" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="642" y="117"/>
</scene>
<!--Profile-->
<scene sceneID="YDY-De-cS5">
<objects>
<viewController id="R2w-XN-4gi" customClass="Profile" customModule="Segue_with_Data" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="25T-V9-Zvb">
<rect key="frame" x="0.0" y="0.0" width="320" height="226"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Uub-nT-ZkO" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-1056" y="710"/>
</scene>
</scenes>
</document>
答案 0 :(得分:1)
使用Struct或Enum不是更好吗?我建议你使用其中任何一种。
在sourceVC中,创建此结构。
struct Segue {
static let profile = "ProfileSegueID"
static let contact = "ContactSegueID"
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch (segue.identifier, segue.destination, sender) {
case (Segue.profile?, let vc as Profile):
vc.username = "User Name"
default: break
}
super.prepare(for: segue, sender: sender)
}
答案 1 :(得分:0)
像这样你可以通过多个Segues将数据传递给其他ViewControllers
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "profile"
{
let destination = segue.destination as? Profile
destination.username = "demo"
}
if segue.identifier == "contact"
{
let destination = segue.destination as? Contact
destination.email = "email1@xxxxx_com"
}
}
答案 2 :(得分:0)
尝试在行上添加断点,将segue.destination向下转换为UIViewController的子类,并检查是否真的发生了向下转换。
但根据我的喜好,在这种特殊情况下执行强制向下转换(使用“!”而不是“?”)会更好,因为您确切知道UIViewController的哪个子类必须位于segue目标中,如果有错误一,明确崩溃应用程序是非常好的理由。
答案 3 :(得分:-4)
如果你想将数据从ViewController传递到另一个ViewController,我建议你使用全局变量