[英]Opening specific ViewController when selecting item from side menu?
I have CenterTableViewController
, Left, and Right Center is the main view when app starts, and Left and Right are side menu's, that are opened when swiped or clicked on navigation controller . 我有CenterTableViewController
,向左和向右中心是应用程序启动时的主视图,向左和向右是侧边菜单,在导航控制器上滑动或单击时会打开。 Buttons
Both Left and Right TableViewControllers
have their number of items, right one has around 10. When one of those 10 items is clicked, it needs to pop up a specific ExampleViewController
.That ViewController
appears without navigation controller (and I need it with it), and in that navigation controller I need a back button, that will send me back to CenterTableViewController
. Buttons
左和右TableViewControllers
有自己的项目的数量,正确的有10左右。当点击这10个项目中的一个,它需要弹出一个特定ExampleViewController
。那ViewController
出现无导航控制器(我需要它) ,并且在该导航控制器中,我需要一个后退按钮,它将把我发送回CenterTableViewController
。
Up until now, I reached to RightTableViewController
, in its class witch is named the same, i have this method 到现在为止,我到达RightTableViewController
,在其类中的女巫的名称相同,我有此方法
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch (indexPath.row){
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("Kurs_Evra") as! KursEvraViewController
self.navigationController?.pushViewController(VC, animated: true)
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
default:
println("JBG")
}
}
In case 5, it opens ViewController
that I need, but it opens in RightTableViewController
instead of Center (because of that "self" part). 在情况5中,它将打开我需要的ViewController
,但会在RightTableViewController
而不是Center中打开(因为该“自我”部分)。
Also, when that is opened, how to create navigation controller and a back button on that ViewController
that just opened? 此外,当打开该ViewController
时,如何在刚打开的ViewController
上创建导航控制器和后退按钮?
If your centre view controller has a navigation controller, then use that for the navigation. 如果中心视图控制器具有导航控制器,则将其用于导航。 For instance in your right navigation controller, instead of pushing the view controller, fire a notification to navigate to that page. 例如,在您的右侧导航控制器中,而不是按下视图控制器,而是触发通知以导航到该页面。 Then in your centre controller listen for the notification and do the navigation from there. 然后在您的中心控制器中收听通知并从那里进行导航。
This is what you should do: 这是您应该做的:
In your switch statement for case 5, change the code to this: NSNotificationCenter.defaultCenter().postNotificationName("MenuItemNotification", object: "Kurs_Evra”) 在案例5的switch语句中,将代码更改为:NSNotificationCenter.defaultCenter()。postNotificationName(“ MenuItemNotification”,对象:“ Kurs_Evra”)
In your CenterViewController in the viewDidLoad method, add this: 在您的CenterViewController中的viewDidLoad方法中,添加以下内容:
NSNotificationCenter.defaultCenter().addObserverForName("MenuItemNotification", object: nil, queue: NSOperationQueue.mainQueue()) { [weak self] notif in
if let strongSelf = self {
if let segueIdentifier = notif.object as? String {
strongSelf.performSegueWithIdentifier(segueIdentifier, sender: nil)
}
}
}
In the above code I assume you have a segue setup called Kurs_Evra and not just a storyboard Id. 在上面的代码中,我假设您有一个名为Kurs_Evra的segue设置,而不仅仅是一个Storyboard ID。 If you do not want to use a segue, then use the code you had for case 5 如果您不想使用segue,请使用案例5的代码
this was not so hard. 这并不难。
Go to your Right or Left side menu, in class create method didSelectRowAtIndex path, and use switch statement like above. 转到右侧或左侧菜单,在类创建方法didSelectRowAtIndex路径中,然后使用上述switch语句。
Then, in case post what code you need, and this is how you post a new storyboard ABOVE your navigation stack and your main ViewController 然后,以防万一发布所需的代码,这就是在导航堆栈和主ViewController上方发布新的故事板的方式
let Example = self.storyboard?.instantiateViewControllerWithIdentifier("ExampleViewController") as! 让Example = self.storyboard?.instantiateViewControllerWithIdentifier(“ ExampleViewController”)作为! ExampleViewController ExampleViewController
let navController = UINavigationController(rootViewController: Example) 让navController = UINavigationController(rootViewController:示例)
and finally... 最后...
self.presentViewController(navController, animated: true, completion: nil) self.presentViewController(navController,动画:true,完成:无)
using "present" will open a new controller you created, in this case ExampleViewController, and it will be first in new navigation stack.If you wish to close, or simulate back button, simply connect action to a button and call: 使用“ present”将打开您创建的新控制器,在本例中为ExampleViewController,它将首先出现在新的导航堆栈中。如果您想关闭或模拟后退按钮,只需将动作连接到按钮并调用:
self.dismissViewController(navController, animated: true, completion: nil) self.dismissViewController(navController,动画:true,完成:无)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.