mus*_*ies 2 uinavigationcontroller uibarbuttonitem ios swift
我无法在我的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)
Run Code Online (Sandbox Code Playgroud)
我在相关的视图控制器中有一个名为"backPressed"的方法.
有关为什么导航栏按钮没有显示的任何线索?
编辑:我知道backBarButtonItem仅在我们不在堆栈顶部时显示.但是,即使我们位于堆栈顶部,leftBarButtonItem也会出现?
Dha*_*esh 11
这是一个简单的示例,您可以从第一个视图以编程方式设置导航栏:
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)
}
Run Code Online (Sandbox Code Playgroud)
如果您想在该导航中添加后退按钮,请将此代码用于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)
}
Run Code Online (Sandbox Code Playgroud)
如果你想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)
}
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你.
归档时间: |
|
查看次数: |
6384 次 |
最近记录: |