【发布时间】:2015-06-01 07:21:22
【问题描述】:
我无法在我的 AppDelegate 中快速解决问题。数周来,我一直在研究这个小问题(在后台,而不是连续),并进行了大量的谷歌搜索。我一定是错过了什么。
当远程通知进入我的 iOS 应用程序时,我想以模态方式呈现适合消息的视图,并带有一个带有后退按钮的 UINavigationController。这个想法是,当用户处理完通知后,他们可以按“返回”并返回到原来的位置。我无法以编程方式在导航控制器中显示“返回”按钮。
并非所有我现有的视图都嵌入在导航控制器中。因此,我没有在堆栈中找到现有的 UINavigationController,而是创建了一个新的 UINavigationController 并尝试将其设置并呈现如下:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var mvc = mainStoryboard.instantiateViewControllerWithIdentifier("MyViewIdentifier") as! MySpecialViewController
/* here is where I setup the necessary variables in the mvc view controller, using data from the remote message. You don't need to see all that */
/* this seems to work, I do get what looks to be a navigation controller */
let newNavController = UINavigationController(rootViewController: mvc)
/* this part doesn't work, I don't get a "Back" button */
let btn = UIBarButtonItem(title: "back", style: UIBarButtonItemStyle.Done, target: mvc, action: "backPressed")
newNavController.navigationItem.leftBarButtonItem = btn
/* this block seems to work. The idea is to work through the hierarchy of any modal presentations
until we get to the screen that is actually showing right now. I'm not
sure this will work in all situations, but it seems to work in my app's hierarchy. */
var currentViewController = self.window!.rootViewController
while currentViewController?.presentedViewController != nil {
currentViewController = currentViewController?.presentedViewController
}
/* this I suppose shows my new view controller modally, except without
the "back" button it's hard to be sure the navigation controller is even there. */
currentViewController?.presentViewController(newNavController, animated: true, completion: nil)
我在相关视图控制器中确实有一个名为“backPressed”的方法。
关于为什么导航栏按钮没有显示的任何线索?
编辑:我知道 backBarButtonItem 只有在我们不在堆栈顶部时才会显示。但是,即使我们在堆栈的顶部,leftBarButtonItem 也应该出现?
【问题讨论】:
标签: ios swift uinavigationcontroller uibarbuttonitem