Skip to content

Commit 0677d34

Browse files
author
Steve Trease
committed
Added button actions to filter IPv4 and IPv6 interfaces on and off
1 parent d99c82b commit 0677d34

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

IP Address/Base.lproj/Main.storyboard

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="fill" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="4D9-QY-Z7f">
7070
<rect key="frame" x="38" y="0.0" width="51" height="0.0"/>
7171
<color key="onTintColor" white="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
72+
<connections>
73+
<action selector="switchToggled:" destination="BYZ-38-t0r" eventType="primaryActionTriggered" id="5Ua-gF-yFX"/>
74+
</connections>
7275
</switch>
7376
</subviews>
7477
</stackView>
@@ -84,6 +87,9 @@
8487
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="fill" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="k1l-3t-xif">
8588
<rect key="frame" x="38" y="0.0" width="51" height="0.0"/>
8689
<color key="onTintColor" white="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
90+
<connections>
91+
<action selector="switchToggled:" destination="BYZ-38-t0r" eventType="primaryActionTriggered" id="gnG-yw-32a"/>
92+
</connections>
8793
</switch>
8894
</subviews>
8995
</stackView>
@@ -127,6 +133,8 @@
127133
</connections>
128134
</view>
129135
<connections>
136+
<outlet property="IPv4filterSwitch" destination="4D9-QY-Z7f" id="isA-GW-TSe"/>
137+
<outlet property="IPv6filterSwitch" destination="k1l-3t-xif" id="lvu-W6-Qvh"/>
130138
<outlet property="tableView" destination="ATv-ck-YdY" id="aCH-cM-tuT"/>
131139
</connections>
132140
</viewController>

IP Address/ViewController.swift

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@ import UIKit
1212
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
1313

1414
@IBOutlet var tableView: UITableView!
15+
@IBOutlet var IPv4filterSwitch: UISwitch!
16+
@IBOutlet var IPv6filterSwitch: UISwitch!
17+
1518

1619
var interfaces = Interface.allInterfaces()
1720

1821
override func viewDidLoad() {
1922
super.viewDidLoad()
2023

21-
interfaces = Interface.allInterfaces()
22-
interfaces.sort {
23-
if $0.description == $1.description { return $0.address! < $1.address! }
24-
return $0.description < $1.description
25-
}
24+
refreshAndSortAndFilterData()
2625
}
2726

2827

@@ -33,7 +32,6 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
3332

3433

3534
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
36-
print (interfaces.count)
3735
return interfaces.count
3836
}
3937

@@ -49,15 +47,42 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
4947
return cell
5048
}
5149

50+
@IBAction func switchToggled(_ sender: UISwitch) {
51+
print (NSURL (fileURLWithPath: "\(#file)").lastPathComponent!, "\(#function)")
52+
refreshAndSortAndFilterData()
53+
self.tableView.reloadData()
54+
}
55+
5256

5357
// screen tap to refresh
5458
@IBAction func screenTappedTriggered(sender: AnyObject) {
5559
print (NSURL (fileURLWithPath: "\(#file)").lastPathComponent!, "\(#function)")
60+
refreshAndSortAndFilterData()
61+
self.tableView.reloadData()
62+
}
63+
64+
65+
func refreshAndSortAndFilterData () {
5666
interfaces = Interface.allInterfaces()
67+
68+
let IPv4Interfaces = interfaces.filter { $0.family == .ipv4 }
69+
let IPv6Interfaces = interfaces.filter { $0.family == .ipv6 }
70+
71+
print ("\(IPv4Interfaces.count) \(IPv6Interfaces.count) \(interfaces.count)")
72+
73+
interfaces = []
74+
if (IPv4filterSwitch.isOn) {
75+
interfaces = interfaces + IPv4Interfaces
76+
}
77+
if (IPv6filterSwitch.isOn) {
78+
interfaces = interfaces + IPv6Interfaces
79+
}
80+
81+
print (interfaces.count)
82+
5783
interfaces.sort {
5884
if $0.description == $1.description { return $0.address! < $1.address! }
5985
return $0.description < $1.description
6086
}
61-
self.tableView.reloadData()
6287
}
6388
}

0 commit comments

Comments
 (0)