我无法在我的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也会出现?
答案 0 :(得分:10)
以下是一个简单的示例,您可以从第一个视图以编程方式设置导航栏:
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("SecondView") as? SecondView {
let navController = UINavigationController(rootViewController: resultController) // Creating a navigation controller with resultController at the root of the navigation stack.
self.presentViewController(navController, animated:true, completion: nil)
}
如果您想在该导航中添加后退按钮,请将此代码用于SecondView.swift
类:
override func viewDidLoad() {
super.viewDidLoad()
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
navigationItem.leftBarButtonItem = backButton
}
func goBack(){
dismissViewControllerAnimated(true, completion: nil)
}
如果您想从firstView
完成所有这些操作,那么这是您的代码:
@IBAction func btnPressed(sender: AnyObject) {
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("SecondView") as? SecondView {
resultController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
let navController = UINavigationController(rootViewController: resultController) // Creating a navigation controller with VC1 at the root of the navigation stack.
self.presentViewController(navController, animated:true, completion: nil)
}
}
func goBack(){
dismissViewControllerAnimated(true, completion: nil)
}
希望这会对你有所帮助。
答案 1 :(得分:1)
我认为您的视图控制器被多个segue使用?
在故事板中,您可以将视图控制器嵌入导航控制器中。
对于模态演示,您将转到导航控制器而不是视图控制器。这将为模态演示提供一个条。
完成按钮更可能用于解除模态演示。 (用户可能会对后退按钮的非典型使用感到困惑,因为您呈现的导航控制器没有从其堆栈中弹出视图控制器。)