![](/img/trans.png)
[英]iOS Swift Navigate to certain ViewController programmatically from push notification
[英]iOS Swift Programmatically Trigger button from previous ViewController
當 BackgroundClicked 被觸發時,返回上一個視圖控制器並以編程方式單擊負載上前一個視圖控制器上的按鈕。 但僅當從子視圖控制器單擊 BackgroundClicked 時
例如
//this button is on child controller
@IBAction func BackgroundClicked(sender: AnyObject)
{
self.navigationController?.popToRootViewControllerAnimated(true)
// when im on the root controller click button abc
// button abc resides on the root controller
}
我只希望按鈕 abc 僅在單擊 BackgroundClicked 時才單擊根控制器的負載。 我不知道該怎么做,或者是否有可能
您可以在將按鈕按下ChildViewController
時發送通知,如下所示:
當您按下按鈕時,將此代碼添加到您的ChildViewController
:
@IBAction func BackgroundClicked(sender: AnyObject)
{
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil)
self.navigationController?.popToRootViewControllerAnimated(true)
}
在您的ParentViewController
將此代碼添加到您的viewDidLoad
方法中:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshList:", name:"refresh", object: nil)
}
這是您的輔助功能:
func refreshList(notification: NSNotification){
println("parent method is called")
}
現在,當您從 ChildViewController refreshList
方法按下后退按鈕時,將從您的refreshList
調用。
檢查此示例項目以獲取更多信息。
希望這會有所幫助。
所以這將回到根視圖控制器,但你想要做的是你想在之前的視圖控制器中創建一個函數。 在viewController
中的代碼中,您將返回運行viewDidLoad()
方法中的函數名稱。 此外,每當您單擊后退按鈕時,您都想更改變量的值,因為您不想每次都運行該函數viewDidLoad()
。 上一個視圖控制器的代碼應該是這樣的:
var decideInt: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
if decideInt == 0 {
// Do Nothing
} else if decideInt == 1 {
functionYouWantToRun()
decideInt = 0
}
}
func functionYouWantToRun() {
//Write the code to the function you want to run
}
這是您需要編寫的代碼才能返回:
//this button is on child controller
@IBAction func BackgroundClicked(sender: AnyObject)
{
self.navigationController?.popToRootViewControllerAnimated(true)
var goToView: DestinationViewControllerName = DestinationViewControllerName()
goToView.decideInt = 1
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.