Skip to content

Commit e29d08c

Browse files
committed
UICatalog: simplify example code
Use the new Action based handler to remove an extension and a private method. This demonstrates more of the API surface and reduces the amount of code.
1 parent 50957c7 commit e29d08c

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

Examples/UICatalog/UICatalog.swift

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ import func WinSDK.MessageBoxW
88
import let WinSDK.MB_OK
99
import struct WinSDK.UINT
1010

11-
private extension Button {
12-
convenience init(frame: Rect = .zero, title: String) {
13-
self.init(frame: frame)
14-
setTitle(title, forState: .normal)
15-
}
16-
}
17-
1811
private extension Label {
1912
convenience init(frame: Rect, title: String) {
2013
self.init(frame: frame)
@@ -32,7 +25,10 @@ final class UICatalog: ApplicationDelegate, SceneDelegate {
3225

3326
lazy var button: Button =
3427
Button(frame: Rect(x: 72.0, y: 4.0, width: 96.0, height: 32.0),
35-
title: "Press Me!")
28+
primaryAction: Action(title: "Press Me!") { _ in
29+
MessageBoxW(nil, "Swift/Win32 Demo!".wide,
30+
"Swift/Win32 MessageBox!".wide, UINT(MB_OK))
31+
})
3632

3733
lazy var checkbox: Switch =
3834
Switch(frame: Rect(x: 4.0, y: 40.0, width: 256.0, height: 24.0))
@@ -111,9 +107,6 @@ final class UICatalog: ApplicationDelegate, SceneDelegate {
111107

112108
self.label.font = Font(name: "Consolas", size: 10)!
113109

114-
self.button.addTarget(self, action: UICatalog.pressMe(_:),
115-
for: .primaryActionTriggered)
116-
117110
self.checkbox.title = "Check me out"
118111

119112
self.textfield.text = "Introducing Swift/Win32"
@@ -159,11 +152,6 @@ Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deseru
159152
print("Goodbye cruel world!")
160153
}
161154

162-
private func pressMe(_: Button?) {
163-
MessageBoxW(nil, "Swift/Win32 Demo!".wide,
164-
"Swift/Win32 MessageBox!".wide, UINT(MB_OK))
165-
}
166-
167155
private func stepperValueDidChange(_ stepper: Stepper) {
168156
self.stepperLabel.text = String(Int(stepper.value))
169157
self.tableview.reloadData()
@@ -179,8 +167,11 @@ extension UICatalog: TableViewDataSource {
179167
public func tableView(_ tableView: TableView,
180168
cellForRowAt indexPath: IndexPath) -> TableViewCell {
181169
let cell = TableViewCell(style: .default, reuseIdentifier: nil)
182-
cell.addSubview(Button(frame: Rect(x: 0, y: 0, width: 80, height: 32),
183-
title: "Button \(indexPath.row)"))
170+
171+
let button: Button = Button(frame: Rect(x: 0, y: 0, width: 80, height: 32))
172+
button.setTitle("Button \(indexPath.row)", forState: .normal)
173+
cell.addSubview(button)
174+
184175
return cell
185176
}
186177
}

0 commit comments

Comments
 (0)