Skip to content

Commit 12255d4

Browse files
committed
Added demo for UIAlertAction.Action
1 parent 62ff2d0 commit 12255d4

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

Demo/Demo/Base.lproj/Main.storyboard

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@
3535
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
3636
<subviews>
3737
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="SZL-3G-VZh">
38-
<rect key="frame" x="277" y="31" width="46" height="30"/>
38+
<rect key="frame" x="264" y="31" width="73" height="30"/>
3939
<animations/>
40-
<state key="normal" title="Button"/>
40+
<state key="normal" title="Show alert"/>
4141
</button>
42-
<activityIndicatorView hidden="YES" opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" hidesWhenStopped="YES" style="gray" translatesAutoresizingMaskIntoConstraints="NO" id="OrY-7C-i77">
43-
<rect key="frame" x="290" y="258" width="20" height="20"/>
44-
</activityIndicatorView>
4542
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Doing some work" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ulN-2s-mAd">
46-
<rect key="frame" x="234" y="229" width="133" height="21"/>
43+
<rect key="frame" x="234" y="467" width="133" height="21"/>
4744
<fontDescription key="fontDescription" type="system" pointSize="17"/>
4845
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
4946
<nil key="highlightedColor"/>
5047
</label>
48+
<activityIndicatorView hidden="YES" opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" hidesWhenStopped="YES" style="gray" translatesAutoresizingMaskIntoConstraints="NO" id="OrY-7C-i77">
49+
<rect key="frame" x="290" y="496" width="20" height="20"/>
50+
</activityIndicatorView>
5151
</subviews>
5252
<animations/>
5353
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
5454
<constraints>
5555
<constraint firstItem="OrY-7C-i77" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Dyl-A6-bud"/>
56-
<constraint firstItem="OrY-7C-i77" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="KWX-Rs-PVL"/>
5756
<constraint firstItem="SZL-3G-VZh" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="31" id="Np0-fa-c3f"/>
5857
<constraint firstItem="ulN-2s-mAd" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="a5T-qt-gDR"/>
5958
<constraint firstItem="OrY-7C-i77" firstAttribute="top" secondItem="ulN-2s-mAd" secondAttribute="bottom" constant="8" id="eqf-Tp-OJd"/>
6059
<constraint firstItem="SZL-3G-VZh" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="hb0-j9-0rK"/>
60+
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="OrY-7C-i77" secondAttribute="bottom" constant="20" id="jhU-Jj-aCX"/>
6161
</constraints>
6262
</view>
6363
<extendedEdge key="edgesForExtendedLayout"/>

Demo/Demo/ViewController.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,23 @@ class ViewController: UIViewController {
2222
super.viewDidLoad()
2323

2424
// Demo: add an action to a button in the view
25-
let action = CocoaAction { _ in
26-
return create { observer -> Disposable in
27-
// Do whatever work here.
28-
print("Doing work for button at \(NSDate())")
29-
observer.onCompleted()
30-
return NopDisposable.instance
25+
let action = CocoaAction {
26+
print("Button was pressed, showing an alert and keeping the activity indicator spinning while alert is displayed")
27+
return create {
28+
[weak self] observer -> Disposable in
29+
30+
// Demo: show an alert and complete the view's button action once the alert's OK button is pressed
31+
let alertController = UIAlertController(title: "Hello world", message: "This alert was triggered by a button action", preferredStyle: .Alert)
32+
let ok = UIAlertAction.Action("OK", style: .Default)
33+
ok.rx_action = CocoaAction {
34+
print("Alert's OK button was pressed")
35+
observer.onCompleted()
36+
return empty()
37+
}
38+
alertController.addAction(ok)
39+
self!.presentViewController(alertController, animated: true, completion: nil)
40+
41+
return NopDisposable.instance
3142
}
3243
}
3344
button.rx_action = action
@@ -38,7 +49,7 @@ class ViewController: UIViewController {
3849
return empty().delaySubscription(2, MainScheduler.sharedInstance)
3950
}
4051

41-
// Demo: obseve the output of both actions, spin an activity indicator
52+
// Demo: observe the output of both actions, spin an activity indicator
4253
// while performing the work
4354
combineLatest(
4455
button.rx_action!.executing,

0 commit comments

Comments
 (0)