Skip to content

Commit e2cb9f8

Browse files
author
Chris Hu
committed
DemoRxSwift: calculator
1 parent 6df8f31 commit e2cb9f8

File tree

5 files changed

+120
-10
lines changed

5 files changed

+120
-10
lines changed

DemoRxSwift/DemoRxSwift.xcodeproj/project.pbxproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
/* Begin PBXBuildFile section */
1010
7BBDF59ABB4783C09CF5E536 /* Pods_DemoRxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 866EF8C38E44D5926A7C5E15 /* Pods_DemoRxSwift.framework */; };
1111
A4B5F7EF1E2F7AD3005BA002 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4B5F7EE1E2F7AD3005BA002 /* AppDelegate.swift */; };
12-
A4B5F7F11E2F7AD3005BA002 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4B5F7F01E2F7AD3005BA002 /* ViewController.swift */; };
12+
A4B5F7F11E2F7AD3005BA002 /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4B5F7F01E2F7AD3005BA002 /* TableViewController.swift */; };
1313
A4B5F7F41E2F7AD3005BA002 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A4B5F7F21E2F7AD3005BA002 /* Main.storyboard */; };
1414
A4B5F7F61E2F7AD3005BA002 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A4B5F7F51E2F7AD3005BA002 /* Assets.xcassets */; };
1515
A4B5F7F91E2F7AD3005BA002 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A4B5F7F71E2F7AD3005BA002 /* LaunchScreen.storyboard */; };
1616
A4B5F8011E2F7BF8005BA002 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4B5F8001E2F7BF8005BA002 /* User.swift */; };
1717
A4B5F8031E2F7C8E005BA002 /* ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4B5F8021E2F7C8E005BA002 /* ViewModel.swift */; };
1818
A4B5F8061E306428005BA002 /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4B5F8051E306428005BA002 /* LoginViewController.swift */; };
19+
A4B5F8261E31A3CB005BA002 /* CalculatorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4B5F8251E31A3CB005BA002 /* CalculatorViewController.swift */; };
1920
/* End PBXBuildFile section */
2021

2122
/* Begin PBXFileReference section */
@@ -24,14 +25,15 @@
2425
8FA16A04CB7A3A61C6CB4EE3 /* Pods-DemoRxSwift.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoRxSwift.release.xcconfig"; path = "Pods/Target Support Files/Pods-DemoRxSwift/Pods-DemoRxSwift.release.xcconfig"; sourceTree = "<group>"; };
2526
A4B5F7EB1E2F7AD3005BA002 /* DemoRxSwift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DemoRxSwift.app; sourceTree = BUILT_PRODUCTS_DIR; };
2627
A4B5F7EE1E2F7AD3005BA002 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
27-
A4B5F7F01E2F7AD3005BA002 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
28+
A4B5F7F01E2F7AD3005BA002 /* TableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = "<group>"; };
2829
A4B5F7F31E2F7AD3005BA002 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
2930
A4B5F7F51E2F7AD3005BA002 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
3031
A4B5F7F81E2F7AD3005BA002 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
3132
A4B5F7FA1E2F7AD3005BA002 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3233
A4B5F8001E2F7BF8005BA002 /* User.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = "<group>"; };
3334
A4B5F8021E2F7C8E005BA002 /* ViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewModel.swift; sourceTree = "<group>"; };
3435
A4B5F8051E306428005BA002 /* LoginViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = "<group>"; };
36+
A4B5F8251E31A3CB005BA002 /* CalculatorViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CalculatorViewController.swift; sourceTree = "<group>"; };
3537
/* End PBXFileReference section */
3638

3739
/* Begin PBXFrameworksBuildPhase section */
@@ -85,8 +87,9 @@
8587
isa = PBXGroup;
8688
children = (
8789
A4B5F7EE1E2F7AD3005BA002 /* AppDelegate.swift */,
88-
A4B5F7F01E2F7AD3005BA002 /* ViewController.swift */,
90+
A4B5F7F01E2F7AD3005BA002 /* TableViewController.swift */,
8991
A4B5F8051E306428005BA002 /* LoginViewController.swift */,
92+
A4B5F8251E31A3CB005BA002 /* CalculatorViewController.swift */,
9093
A4B5F8001E2F7BF8005BA002 /* User.swift */,
9194
A4B5F8021E2F7C8E005BA002 /* ViewModel.swift */,
9295
A4B5F7F21E2F7AD3005BA002 /* Main.storyboard */,
@@ -221,10 +224,11 @@
221224
isa = PBXSourcesBuildPhase;
222225
buildActionMask = 2147483647;
223226
files = (
224-
A4B5F7F11E2F7AD3005BA002 /* ViewController.swift in Sources */,
227+
A4B5F7F11E2F7AD3005BA002 /* TableViewController.swift in Sources */,
225228
A4B5F7EF1E2F7AD3005BA002 /* AppDelegate.swift in Sources */,
226229
A4B5F8031E2F7C8E005BA002 /* ViewModel.swift in Sources */,
227230
A4B5F8061E306428005BA002 /* LoginViewController.swift in Sources */,
231+
A4B5F8261E31A3CB005BA002 /* CalculatorViewController.swift in Sources */,
228232
A4B5F8011E2F7BF8005BA002 /* User.swift in Sources */,
229233
);
230234
runOnlyForDeploymentPostprocessing = 0;

DemoRxSwift/DemoRxSwift/Base.lproj/Main.storyboard

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1010
</dependencies>
1111
<scenes>
12-
<!--View Controller-->
12+
<!--Table View Controller-->
1313
<scene sceneID="tne-QT-ifu">
1414
<objects>
15-
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="DemoRxSwift" customModuleProvider="target" sceneMemberID="viewController">
15+
<viewController id="BYZ-38-t0r" customClass="TableViewController" customModule="DemoRxSwift" customModuleProvider="target" sceneMemberID="viewController">
1616
<layoutGuides>
1717
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
1818
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
@@ -62,6 +62,7 @@
6262
</viewController>
6363
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
6464
</objects>
65+
<point key="canvasLocation" x="81" y="43"/>
6566
</scene>
6667
<!--Login View Controller-->
6768
<scene sceneID="MGn-kN-dsW">
@@ -152,5 +153,66 @@
152153
</objects>
153154
<point key="canvasLocation" x="866" y="43"/>
154155
</scene>
156+
<!--Calculator View Controller-->
157+
<scene sceneID="yip-v6-hEI">
158+
<objects>
159+
<viewController id="k81-1m-u7B" customClass="CalculatorViewController" customModule="DemoRxSwift" customModuleProvider="target" sceneMemberID="viewController">
160+
<layoutGuides>
161+
<viewControllerLayoutGuide type="top" id="nV6-h9-TKN"/>
162+
<viewControllerLayoutGuide type="bottom" id="Qe0-Wi-nYJ"/>
163+
</layoutGuides>
164+
<view key="view" contentMode="scaleToFill" id="HgD-cJ-0xl">
165+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
166+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
167+
<subviews>
168+
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="0" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="Ljj-1A-ULD">
169+
<rect key="frame" x="139" y="125" width="97" height="30"/>
170+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
171+
<nil key="textColor"/>
172+
<fontDescription key="fontDescription" type="system" pointSize="14"/>
173+
<textInputTraits key="textInputTraits" keyboardType="numberPad"/>
174+
</textField>
175+
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="0" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="fTH-r4-vAP">
176+
<rect key="frame" x="139" y="212" width="97" height="30"/>
177+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
178+
<nil key="textColor"/>
179+
<fontDescription key="fontDescription" type="system" pointSize="14"/>
180+
<textInputTraits key="textInputTraits" keyboardType="numberPad"/>
181+
</textField>
182+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="+" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qxf-qw-HBG">
183+
<rect key="frame" x="36" y="221" width="42" height="21"/>
184+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
185+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
186+
<nil key="textColor"/>
187+
<nil key="highlightedColor"/>
188+
</label>
189+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="=" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="uVu-M7-vFP">
190+
<rect key="frame" x="36" y="316" width="42" height="21"/>
191+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
192+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
193+
<nil key="textColor"/>
194+
<nil key="highlightedColor"/>
195+
</label>
196+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="0" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="irY-Hq-WhO">
197+
<rect key="frame" x="139" y="323" width="42" height="21"/>
198+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
199+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
200+
<nil key="textColor"/>
201+
<nil key="highlightedColor"/>
202+
</label>
203+
</subviews>
204+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
205+
</view>
206+
<nil key="simulatedStatusBarMetrics"/>
207+
<connections>
208+
<outlet property="number1" destination="Ljj-1A-ULD" id="JSW-OW-3DV"/>
209+
<outlet property="number2" destination="fTH-r4-vAP" id="dKr-rp-wya"/>
210+
<outlet property="result" destination="irY-Hq-WhO" id="cwC-5l-R35"/>
211+
</connections>
212+
</viewController>
213+
<placeholder placeholderIdentifier="IBFirstResponder" id="ELu-n3-UW0" userLabel="First Responder" sceneMemberID="firstResponder"/>
214+
</objects>
215+
<point key="canvasLocation" x="1599" y="43"/>
216+
</scene>
155217
</scenes>
156218
</document>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// CalculatorViewController.swift
3+
// DemoRxSwift
4+
//
5+
// Created by Chris Hu on 17/1/20.
6+
// Copyright © 2017年 com.icetime17. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import RxCocoa
11+
import RxSwift
12+
13+
14+
class CalculatorViewController: UIViewController {
15+
16+
@IBOutlet weak var number1: UITextField!
17+
18+
@IBOutlet weak var number2: UITextField!
19+
20+
21+
@IBOutlet weak var result: UILabel!
22+
23+
24+
let disposeBag = DisposeBag()
25+
26+
27+
override func viewDidLoad() {
28+
super.viewDidLoad()
29+
30+
// 将number1,number2绑定到一起,构成一个Observable
31+
Observable.combineLatest(number1.rx.text, number2.rx.text) { (num1, num2) -> Int in
32+
if let num1 = num1, num1 != "", let num2 = num2, num2 != "" {
33+
return Int(num1)! + Int(num2)!
34+
} else {
35+
return 0
36+
}
37+
}
38+
.map { $0.description }
39+
// Obsever为result的text
40+
.bindTo(result.rx.text)
41+
.addDisposableTo(disposeBag)
42+
}
43+
44+
}

DemoRxSwift/DemoRxSwift/LoginViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LoginViewController: UIViewController {
4040
super.viewDidLoad()
4141

4242

43-
// 声明Observable
43+
// 声明Observable,可观察对象
4444
let userValidation = textFieldUsername.rx.text.orEmpty
4545
.map { (user) -> Bool in
4646
let length = user.characters.count
@@ -61,7 +61,7 @@ class LoginViewController: UIViewController {
6161
}.shareReplay(1)
6262

6363

64-
// bind
64+
// bind,即将Observable与Observer绑定,最终也会调用subscribe
6565
// 此处是将isEnabled视为一个Observer,接收userValidation的消息,做出响应
6666
// 所以Observable发送的消息与Observer能接收的消息要对应起来(此处是Bool)
6767
userValidation

DemoRxSwift/DemoRxSwift/ViewController.swift renamed to DemoRxSwift/DemoRxSwift/TableViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ViewController.swift
2+
// TableViewController.swift
33
// DemoRxSwift
44
//
55
// Created by Chris Hu on 17/1/18.
@@ -11,7 +11,7 @@ import RxSwift
1111
import RxDataSources
1212

1313

14-
class ViewController: UIViewController {
14+
class TableViewController: UIViewController {
1515

1616
@IBOutlet weak var tableView: UITableView!
1717

0 commit comments

Comments
 (0)