【发布时间】:2015-11-18 00:08:01
【问题描述】:
我是 swift 语言的新手,开始创建具有滑动菜单的存根应用程序。我使用来自http://www.appcoda.com/sidebar-menu-swift/ 的教程来创建幻灯片菜单,但想要创建动态而不是静态的应用程序,如示例所示。我在创建 segue 或从 uitableviewcell 导航到连接到相应 uinavigationcontroller 的相应 uiviewcontrollers 时遇到问题。
以下是滑动菜单类的代码:
MenuController.swift
import UIKit
class MenuController:UITableViewController
{
let menuControlList = ["Category 1", "Category 2", "Category 3", "Category 4"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return menuControlList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MenuTableCell
let row = indexPath.row
cell.menuCellText.text = menuControlList[row]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("row clicked :: ", indexPath.row)
switch indexPath.row
{
case 0:
var cat1View = Category1(nibName:"Category1", bundle:nil)
self.navigationController?.pushViewController(cat1View, animated: true)
print(indexPath.row)
break
case 1:
var cat2View = Category2(nibName:"Category2", bundle:nil)
self.navigationController?.pushViewController(cat2View, animated: true)
print(indexPath.row)
break
case 3:
var cat3View = Category3(nibName:"Category3", bundle:nil)
self.navigationController?.pushViewController(cat3View, animated: true)
print(indexPath.row)
break
case 4:
var cat4View = Category4(nibName:"Category4", bundle:nil)
self.navigationController?.pushViewController(cat4View, animated: true)
print(indexPath.row)
break
default:
return;
}
}
}
以下是我的故事板截图:
如果我在创建这个时有任何错误,请告诉我并帮助我纠正它。
以下是我的第 1 类课程的代码:
class Category1 :UIViewController
{
@IBOutlet var menuBtn: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
menuBtn.target = self.revealViewController()
menuBtn.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
编辑:
尝试了以下解决方案:
let vc = storyboard.instantiateViewControllerWithIdentifier("Category1") as! Category1
presentViewController(vc, animated: true, completion: nil)
上面的代码直接打开了带有上滑动画的类别 1 视图控制器,但它没有通过与相应视图控制器相连的导航控制器打开。
如果我使用以下代码:
let vc = storyboard.instantiateViewControllerWithIdentifier("Category1") as! Category1
self.revealViewController().setFrontViewController(vc, animated: true)
上面的代码也加载了viewcontroller,但是滑动菜单没有滑回?
【问题讨论】:
-
看起来你已经用故事板定义了接口,但同样,你也从 nib 文件加载它们。你认为这需要 Category1(nibName:"Category1", bundle:nil)。
-
@GeneratorOfOne 是正确的。您不需要通过 nib 文件加载视图控制器。而是给他们故事板ID并以这种方式加载它们,我认为您不需要执行self.navigationController.pushViewController,而是像这样设置
SWRevealViewController的frontViewController(obj-c代码):[self.revealViewController setFrontViewController:newController];让我知道您是否需要 Objective-C 中的详细示例。抱歉,我不知道 Swift 的语法。 -
@GurtejSingh 请帮助我用 Objective-C 中的示例,这样我就可以知道我在哪里犯了愚蠢的错误。我也更新了我的 category1 类代码
-
@NiranjanBalkrishnaPrajapati 请给我一些时间,我会回答这个问题。
-
@GurtejSingh 我已经尝试过你和 GeneratorOfOne 建议这里是我的代码 'let vc = storyboard.instantiateViewControllerWithIdentifier("Category1") as! Category1 self.revealViewController().setFrontViewController(vc, animated: true)' 但它不能通过 navigationviewcontroller 工作,我认为它直接打开了 category1 屏幕
标签: ios swift uitableview swift2 swrevealviewcontroller