在其他文件的 tabBarController 中显示 ViewController,Swift
Present ViewController in tabBarController from other file, Swift
我正在尝试展示来自另一个 swift 文件的 ViewController。如果我想展示另一个 ViewController 的 ViewController,这会起作用。
这次我想在属于 xib 的 class 中的 tabBarController 中显示一个 ViewController。这会打印:"In new init" in presenter,但不会打印:"setting up presenter" in VC。有什么想法吗?
(无关代码删掉)
xib 中 class 的代码:
@IBAction func timerBtnAction(_ sender: Any) {
let vc = UIStoryboard(name: "SleepTimerPopup", bundle: nil).instantiateViewController(withIdentifier: "SleepTimerPopup_UI") as! SleepTimerPopup_UI
vc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
let currentController = self.getCurrentViewController()
currentController?.tabBarController?.present(vc, animated: true, completion: nil)
}
func getCurrentViewController() -> UIViewController? {
if let rootController = UIApplication.shared.keyWindow?.rootViewController {
var currentController: UIViewController! = rootController
while( currentController.presentedViewController != nil ) {
currentController = currentController.presentedViewController
}
return currentController
}
return nil
}
ViewController 我想在当前上下文中展示:
class SleepTimerPopup_UI: UIViewController, SleepTimerPopupDelegate{
@IBOutlet var shortcutOutlets: [UIButton]!
@IBOutlet weak var timerDoneOutlet: UIButton!
@IBOutlet weak var timePickerOutlet: UIDatePicker!
@IBOutlet weak var sleepTimerContainerView: UIView!
@IBOutlet weak var sleepTimeLabel: UILabel!
@IBOutlet weak var stopWatchBackgroundView: UIView!
@IBOutlet weak var stopWatchImage: UIView!
let sleepTimerPopupDelegate = SleepTimerPopupPresenter()
override func viewDidLoad() {
super.viewDidLoad()
setupSleepTimerUI()
setupSleepTimerDelegate()
}
func setupSleepTimerDelegate(){
print("setting up presenter") <-- THIS IS NOT PRINTED
sleepTimerPopupDelegate.setSleepTimerViewDelegate(sleepTimerDelegate : self)
}
ViewController我想在当前上下文中展示的演示者:
class SleepTimerPopupPresenter{
weak private var sleepTimerDelegate : SleepTimerPopupDelegate?
static var sleepTimer = SleepTimer(sleepTimerPopupPresenter: SleepTimerPopupPresenter())
static var doneBtnIsSelected = false
private var latestOutlet : UIButton?
private var setTime : (Int, Int)
init(){
setTime = (0, 0)
print("in new init") <----- THIS IS PRINTED
NotificationCenter.default.addObserver(
self,
selector: #selector(self.timeChanged),
name: Notification.Name("TimeUpdate"),
object: nil)
}
func setSleepTimerViewDelegate(sleepTimerDelegate : SleepTimerPopupDelegate){
self.sleepTimerDelegate = sleepTimerDelegate
}
可能您的 currentViewController?.tabBarController 为 nil。通常不是UITabBarController的rootViewController或者不在stack中的时候都是可以的。
您可以像这样简单地展示您的视图控制器:
vc.modalPresentationStyle = .overFullScreen // .fullScreen
currentVieController.present(vc, animated: true, completion: nil)
我正在尝试展示来自另一个 swift 文件的 ViewController。如果我想展示另一个 ViewController 的 ViewController,这会起作用。
这次我想在属于 xib 的 class 中的 tabBarController 中显示一个 ViewController。这会打印:"In new init" in presenter,但不会打印:"setting up presenter" in VC。有什么想法吗?
(无关代码删掉)
xib 中 class 的代码:
@IBAction func timerBtnAction(_ sender: Any) {
let vc = UIStoryboard(name: "SleepTimerPopup", bundle: nil).instantiateViewController(withIdentifier: "SleepTimerPopup_UI") as! SleepTimerPopup_UI
vc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
let currentController = self.getCurrentViewController()
currentController?.tabBarController?.present(vc, animated: true, completion: nil)
}
func getCurrentViewController() -> UIViewController? {
if let rootController = UIApplication.shared.keyWindow?.rootViewController {
var currentController: UIViewController! = rootController
while( currentController.presentedViewController != nil ) {
currentController = currentController.presentedViewController
}
return currentController
}
return nil
}
ViewController 我想在当前上下文中展示:
class SleepTimerPopup_UI: UIViewController, SleepTimerPopupDelegate{
@IBOutlet var shortcutOutlets: [UIButton]!
@IBOutlet weak var timerDoneOutlet: UIButton!
@IBOutlet weak var timePickerOutlet: UIDatePicker!
@IBOutlet weak var sleepTimerContainerView: UIView!
@IBOutlet weak var sleepTimeLabel: UILabel!
@IBOutlet weak var stopWatchBackgroundView: UIView!
@IBOutlet weak var stopWatchImage: UIView!
let sleepTimerPopupDelegate = SleepTimerPopupPresenter()
override func viewDidLoad() {
super.viewDidLoad()
setupSleepTimerUI()
setupSleepTimerDelegate()
}
func setupSleepTimerDelegate(){
print("setting up presenter") <-- THIS IS NOT PRINTED
sleepTimerPopupDelegate.setSleepTimerViewDelegate(sleepTimerDelegate : self)
}
ViewController我想在当前上下文中展示的演示者:
class SleepTimerPopupPresenter{
weak private var sleepTimerDelegate : SleepTimerPopupDelegate?
static var sleepTimer = SleepTimer(sleepTimerPopupPresenter: SleepTimerPopupPresenter())
static var doneBtnIsSelected = false
private var latestOutlet : UIButton?
private var setTime : (Int, Int)
init(){
setTime = (0, 0)
print("in new init") <----- THIS IS PRINTED
NotificationCenter.default.addObserver(
self,
selector: #selector(self.timeChanged),
name: Notification.Name("TimeUpdate"),
object: nil)
}
func setSleepTimerViewDelegate(sleepTimerDelegate : SleepTimerPopupDelegate){
self.sleepTimerDelegate = sleepTimerDelegate
}
可能您的 currentViewController?.tabBarController 为 nil。通常不是UITabBarController的rootViewController或者不在stack中的时候都是可以的。
您可以像这样简单地展示您的视图控制器:
vc.modalPresentationStyle = .overFullScreen // .fullScreen
currentVieController.present(vc, animated: true, completion: nil)