簡體   English   中英

iOS popToRootViewControllerAnimated無法在委托方法上使用

[英]iOS popToRootViewControllerAnimated not working on delegate method

我實現了一個自定義委托方法,如果某些數據來自服務器,則該方法應彈出根視圖控制器。 通過單擊子控制器1st方法中的“后退”按鈕, back()檢查文本字段中是否進行了任何更改,並在發生某些更改的情況下顯示UIActionSheet。

1。

func back() {
        if modified {
            let actionSheet = UIActionSheet(title: NSLocalizedString("SAVESETTINGS", comment: "Settings were changed. Do you want to save them?"), delegate: self, cancelButtonTitle: "No", destructiveButtonTitle: "Yes")
            actionSheet.actionSheetStyle = .Default
            actionSheet.showInView(self.view)
        }
        else {
            self.navigationController?.popToRootViewControllerAnimated(true)
        }
    }

ActionSheet委托方法發送數據,並且控制器正在等待答案
2。

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
    if buttonIndex == 0 {
        self.socket.send(someData, tag: someTag)
    }
    else {
       self.modified = false
       self.navigationController?.popToRootViewControllerAnimated(true)
    }     
}

委托方法檢查是否在此處找到正確答案,並應彈出根視圖控制器。

3。

func ackReceived(tag: Int32) {
    if tag == someTag {
        self.navigationController?.popToRootViewControllerAnimated(true)
    }
}

如果沒有更改或在UIActionSheet中按下了第二個按鈕,則根視圖控制器會在第一種和第二種方法中彈出,但在3d方法中不起作用。

調用所有方法。 我最終

self.navigationController?.popToRootViewControllerAnimated(true)

我還檢查了調試器,導航控制器不是nil

希望您能幫我解決這個問題。 提前謝謝了。

感謝Wain的提示,我已經找到了問題。 在主隊列中未調用func ackReceived(tag: Int32) {} UI作為UIKit方法不是線程安全的,應在主隊列中調用。

func ackReceived(tag: Int32) {
    if tag == someTag {
        dispatch_async(dispatch_get_main_queue(), {
                self.navigationController?.popToRootViewControllerAnimated(true)
                return
            })
    }
}

是調用UI方法的正確方法。

非常感謝您的幫助。

不起作用,因為您的資產凈值為零。 您可以通過例如print(“ mainIconTapped:” + String(self.navigationController))進行檢查;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM