I am developing an application with swift.
It says screen A and screen B
Screen B is called from screen A.
So, when I return to screen A from screen B, I would like to run the function of screen A.
I don't know how.
Screen A to screen B are modalized using the Storyboard ID as shown below.
[Screen A]
var selfStoryboard:UISstoryboard?
selfStoryboard = self.storyboard
nex=selfStoryboard!.instantiateViewControllerWithIdentifier("Storyboard_ID") as UIViewController
self.presentViewController(nex, animated:true, completion:nil)
On screen B, the screen is closed with the description below.
[Screen B]
self.dismissViewControllerAnimated(true, completion:nil)
After closing the screen in this way, how do I perform a specific function on screen A?
swift iphone
For modal screen transitions, the transition source ViewController can be retrieved in the UIViewController
class property presentingViewController
.
If the transition source ViewController is BaseViewController
and the method you want to call baseViewController
is func doAnything()
,
iflet controller=self.presentingViewController as? BaseViewController {
controller.doAnyThing()
}
I want to run the function of screen A when I return to screen A from screen B
Strictly speaking, it does not meet your requirements, as the code above will be executed before returning.I don't think that's a problem, but if there is a situation where it has to be "after closing", use the argument completion
in dismissViewControllerAnimated()
.
let controller=self.presentingViewController as? BaseViewController
self.dismissViewControllerAnimated(true, completion:{
controller?.doAnything()
})
If you use self.presentingViewController
in the closure, it doesn't work well, so this is how it is written.
I think you can do it with the viewWillAppear function.
class ViewController:UIViewController {
Override func viewWillAppear(_animated:Bool) {// Runs on return
super.viewWillAppear(animated)
// Code here
}
}
Note: However, with this method, it will also be executed when screen A is first displayed.
Therefore,
class ViewController:UIViewController {
varisBack=false
Override func viewWillAppear(_animated:Bool) {// Runs on return
super.viewWillAppear(animated)
// Code here
ifisBack{
// Code here
}
isBack.toggle()
}
}
As shown in , you can define the variables and make sure that you returned using the conditional branch.
However, in this way,
C->A->B
C->*A->C->*/A
If you transition in this order, you will see WillAppear with the * mark, but if you return to the / mark, you will have to do something else, such as pass the value from B.
© 2024 OneMinuteCode. All rights reserved.