Skip to content

Commit

Permalink
Merge pull request mac-cain13#153 from mac-cain13/feature/unwind-segues
Browse files Browse the repository at this point in the history
Feature/unwind segues
  • Loading branch information
mac-cain13 committed Jan 22, 2016
2 parents 68875a9 + 61c5a70 commit cb56b38
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
48 changes: 32 additions & 16 deletions R.swift/Generators/SegueGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,13 @@ struct SegueGenerator: Generator {
let seguesWithInfo = storyboards.flatMap { storyboard in
storyboard.viewControllers.flatMap { viewController in
viewController.segues.flatMap { segue -> SegueWithInfo? in
let destinationViewControllerType = storyboard.viewControllers
.filter { $0.id == segue.destination }
.first?
.type

let destinationViewControllerPlaceholderType = storyboard.viewControllerPlaceholders
.filter { $0.id == segue.destination }
.first
.flatMap { storyboard -> Type? in
switch storyboard.resolveWithStoryboards(storyboards) {
case .CustomBundle: return Type._UIViewController // Not supported, fallback to UIViewController
case let .Resolved(vc): return vc?.type
}
}

guard let destinationType = destinationViewControllerType ?? destinationViewControllerPlaceholderType else {
guard let destinationType = SegueGenerator.resolveDestinationTypeForSegue(
segue,
inViewController: viewController,
inStoryboard: storyboard,
allStoryboards: storyboards)
else
{
warn("Destination view controller with id \(segue.destination) for segue \(segue.identifier) in \(viewController.type) not found in storyboard \(storyboard.name). Is this storyboard corrupt?")
return nil
}
Expand Down Expand Up @@ -74,6 +65,31 @@ struct SegueGenerator: Generator {
)
}

private static func resolveDestinationTypeForSegue(segue: Storyboard.Segue, inViewController: Storyboard.ViewController, inStoryboard storyboard: Storyboard, allStoryboards storyboards: [Storyboard]) -> Type? {
if segue.kind == "unwind" {
return Type._UIViewController
}

let destinationViewControllerType = storyboard.viewControllers
.filter { $0.id == segue.destination }
.first?
.type

let destinationViewControllerPlaceholderType = storyboard.viewControllerPlaceholders
.filter { $0.id == segue.destination }
.first
.flatMap { storyboard -> Type? in
switch storyboard.resolveWithStoryboards(storyboards) {
case .CustomBundle:
return Type._UIViewController // Not supported, fallback to UIViewController
case let .Resolved(vc):
return vc?.type
}
}

return destinationViewControllerType ?? destinationViewControllerPlaceholderType
}

private static func seguesWithInfoForSourceTypeToStruct(seguesWithInfoForSourceType: [SegueWithInfo]) -> Struct? {
guard let sourceType = seguesWithInfoForSourceType.first?.sourceType else { return nil }

Expand Down
6 changes: 4 additions & 2 deletions R.swift/ResourceTypes/Storyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ struct Storyboard: WhiteListedExtensionsResourceType, ReusableContainer {
let identifier: String
let type: Type
let destination: String
let kind: String
}
}

Expand Down Expand Up @@ -138,11 +139,12 @@ private class StoryboardParserDelegate: NSObject, NSXMLParserDelegate {
}

if let segueIdentifier = attributeDict["identifier"],
segueDestination = attributeDict["destination"]
destination = attributeDict["destination"],
kind = attributeDict["kind"]
{
let type = customType ?? Type._UIStoryboardSegue

let segue = Storyboard.Segue(identifier: segueIdentifier, type: type, destination: segueDestination)
let segue = Storyboard.Segue(identifier: segueIdentifier, type: type, destination: destination, kind: kind)
currentViewController?.1.addSegue(segue)
}

Expand Down
19 changes: 16 additions & 3 deletions ResourceApp/ResourceApp/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="49e-Tb-3d3">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
</dependencies>
<customFonts key="customFonts">
Expand Down Expand Up @@ -114,10 +113,24 @@
<view key="view" contentMode="scaleToFill" id="Xle-dW-hVf">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8B6-zH-yBB">
<rect key="frame" x="274" y="285" width="52" height="30"/>
<state key="normal" title="Unwind"/>
<connections>
<segue destination="gh1-ro-RFV" kind="unwind" identifier="unwindSegue" unwindAction="unwindSomethingSomthing:" id="UNt-3N-uEw"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="8B6-zH-yBB" firstAttribute="centerY" secondItem="Xle-dW-hVf" secondAttribute="centerY" id="JNz-kz-XlF"/>
<constraint firstItem="8B6-zH-yBB" firstAttribute="centerX" secondItem="Xle-dW-hVf" secondAttribute="centerX" id="vuH-Hu-ZP0"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="thV-Zl-Ztw" userLabel="First Responder" sceneMemberID="firstResponder"/>
<exit id="gh1-ro-RFV" userLabel="Exit" sceneMemberID="exit"/>
</objects>
<point key="canvasLocation" x="1587" y="360"/>
</scene>
Expand All @@ -134,15 +147,15 @@
<rect key="frame" x="0.0" y="28" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Lre-Cj-0QS" id="sq1-mW-7Wa">
<rect key="frame" x="0.0" y="0.0" width="600" height="43"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="fullCell" id="L8j-6T-BZ6">
<rect key="frame" x="0.0" y="72" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="L8j-6T-BZ6" id="Vn1-wc-sJz">
<rect key="frame" x="0.0" y="0.0" width="600" height="43"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
</tableViewCellContentView>
</tableViewCell>
Expand Down
4 changes: 3 additions & 1 deletion ResourceApp/ResourceApp/SecondViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ class SecondViewController: UIViewController {
// Dispose of any resources that can be recreated.
}

}
@IBAction func unwindSomethingSomthing(segue: UIStoryboardSegue) {

}
}

0 comments on commit cb56b38

Please sign in to comment.