![](/img/trans.png)
[英]How to access from viewController another viewController in Swift 3
[英]Swift wait task finish from another viewcontroller
我有一個 Viewcontroller VC_B,我通過使用“NEHotspotConfigurationManager.shared”連接到 Wifi 到一個特殊設備。 這個 function 當前錯誤,因為無論成功或失敗它都會返回 nil。 請點擊以下鏈接。 https://developer.apple.com/forums/thread/109412 https://stackoverflow.com/.../nehotspotconfigurationmanag..
所以我將創建一個 Viewcontroller VC_A,然后在 wifi 連接發生變化時使用“pathUpdateHandler”來捕獲事件。 然后使用 CoreLocation 將 wifi SSID 保存到 CURENT_SSID。
1. VC_A
// get current SSID and save to UserData.shared.current_wifi_ssid
var ssid: String {
var return_ssid:String = "取得できませんでした。"
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary?,
let ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String {
return_ssid = ssid
}
}
}
UserData.shared.current_wifi_ssid = return_ssid
return return_ssid
}
//catch when having wifi connection changing event
func checkNetWorking() {
monitor.pathUpdateHandler = { path in
if path.status == .satisfied {
self.isConnected = true
//check sign in at backend
Backend.shared.checkSignedIn {(isSignedIn: Bool) in
if !isSignedIn {
DispatchQueue.main.async {
AlertHelper.displayOK(self, title: "エラー", message: "サインインしてください")
}
}
}
} else {
//the case no internet or already accessed to DEVICE_WIFI
//############# location - wifi
do something
//#############
}
}
}
2. VC_B
//connect to device wifi and check
func connectTheta(ID: String){
TableVIew.isUserInteractionEnabled = false
SVProgressHUD.show()
let manager = NEHotspotConfigurationManager.shared
let ssid = "THETAYN" + ID + ".OSC"
let password = ID
let isWEP = false
let hotspotConfiguration = NEHotspotConfiguration(ssid: ssid, passphrase: password, isWEP: isWEP)
hotspotConfiguration.joinOnce = true
hotspotConfiguration.lifeTimeInDays = 1
UserData.shared.current_wifi_ssid = "NULL"
manager.apply(hotspotConfiguration) { (error) in //error always nil
if error != nil {
// error
} else {
// success
}
check_wifi_theta();
func check_wifi_theta(){
//i'm using while here to wait task from VC_A (wait until UserData.shared.current_wifi_ssid is updated)
while(UserData.shared.current_wifi_ssid=="NULL"){
Thread.sleep(forTimeInterval: 0.5)
}
print("ssid theta", ssid)
print("ssid current", UserData.shared.current_wifi_ssid)
if (UserData.shared.current_wifi_ssid==ssid){
print("connect Theta OK")
AlertHelper.displayOK(self, title: "接続できました", message: "", okHandler: {_ in
self.navigationController?.popViewController(animated: true)
})
}
else{
AlertHelper.displayOK(self, title: "接続できません", message: "THETAの電源が入っている確認してください。")
}
self.TableVIew.isUserInteractionEnabled = true
SVProgressHUD.dismiss()
}
}
}
當我試圖等待“pathUpdateHandler”從 VC_B 完成時,我被困在第 2 步。 有時它會改變,但有時不會。 我在這里使用while循環,它解決了我的問題。 但我認為這不是最好的選擇。 有沒有更好的解決方案? 謝謝!
首先,清理您的示例代碼可能會更好。 在您的信息中,很難理解發生了什么。
在我看來,在這種情況下使用無限 while 循環是個壞主意。 我可以建議以下選項:
NSLock
以任何方式觀察事件。 iOS 中有多個選項,具體取決於任務。 您可以使用委托模式並創建委托協議並在 VC_A 內添加對 VC_B 的引用。 類似的方法是閉包使用(保存並調用閉包而不是委托)。
另一種選擇是使用 NotificationCenter 在 VC_A 中發布通知並在 VC_B 中觀察此事件。 這是 iOS 中的基本觀察者模式用法,但您可以使用更高級的技術,例如創建自己的觀察者模式實現,例如多委托觀察者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.