如何使用SWrevaelviewController swift 3

时间:2017-09-17 16:05:32

标签: ios swift swrevealviewcontroller

我正在使用带有sw_front和sw_right的SWrevealViewController

- >目前我能够在选择行时导航到不同的viewcontrollers

当我选择一行时,我想导航到listviewcontroller(例如:1,case:2,case:3)。我想根据我的行数据设置labelText(recievedString) 和。在休息切换的情况下,我想导航到不同的控制器。

这是我的sw_front(sidebaritemsViewController):

class sidebaritemsViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var passwordimage: UIImageView!
@IBOutlet weak var profileimage: UIImageView!
@IBAction func changepassword(_ sender: AnyObject) {
}
@IBOutlet weak var name: UILabel!
@IBOutlet weak var employeeid: UILabel!

var revealController: RevealViewController?
var tableviewitems:Array = [String]()
var image:Array = [UIImage]()


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    // for profile image
    revealController = self.revealViewController() as? RevealViewController
    profileimage.clipsToBounds = true
    profileimage.layer.masksToBounds = true
    profileimage.layer.cornerRadius = 60


   tableviewitems = ["Dashboard","Mark Attendance","Update Attendance","delete Attendance","Time Table","Academic Calendar","Reports","About Us","Logout"]
    image = [UIImage(named: "dashbord10")!,UIImage(named: "attendanceimg")!,UIImage(named: "updateimg")!,UIImage(named: "delete10")!,UIImage(named: "updateimg-1")!,UIImage(named: "calendar-icons")!,UIImage(named: "updateimg-2")!,UIImage(named: "aboutus10")!,UIImage(named: "logout10")!]



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return tableviewitems.count

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "sidebarTableViewCell") as! sidebarTableViewCell
    cell.cellimage.image = image[indexPath.row]
    cell.cellitem.text = tableviewitems[indexPath.row]
if cell.cellitem.text == "Reports" {

    cell.showlineView.isHidden = false
} else {
    cell.showlineView.isHidden = true

}
    return cell

}
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   // let cell = tableView.dequeueReusableCell(withIdentifier: "sidebarTableViewCell") as! sidebarTableViewCell
    var a = Int()
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    var controller: UIViewController!
    switch indexPath.row {
    case 0: controller = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController

    case 1: controller = storyboard.instantiateViewController(withIdentifier: "listViewController") as! listViewController

    case 2: controller = storyboard.instantiateViewController(withIdentifier: "listViewController") as! listViewController

    case 3: controller = storyboard.instantiateViewController(withIdentifier: "listViewController") as! listViewController

    case 4: controller = storyboard.instantiateViewController(withIdentifier: "timeTableViewController") as! timeTableViewController

    case 5: controller  = storyboard.instantiateViewController(withIdentifier: "calenderViewController") as! calenderViewController

    case 6: controller = storyboard.instantiateViewController(withIdentifier: "reportFirstViewController") as! reportFirstViewController

    case 7: a = 1
    case 8:let alert = UIAlertController(title: "alert", message: "are you sure you want to logout", preferredStyle: UIAlertControllerStyle.alert);
    let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel){
        (ACTION) in print("cancel button tapped");

    }
    let confirmButton = UIAlertAction(title: "Logout", style: UIAlertActionStyle.default){
        (ACTION) in print("logout button tapped");

    }
    alert.addAction(cancelButton)
    alert.addAction(confirmButton)

    self.present(alert, animated: true, completion: nil)
    a = 1




    default: break

    }



         if a == 0 {
    let navController = UINavigationController(rootViewController: controller)
    let revealController = self.revealViewController() as! RevealViewController
    revealController.rightViewController = navController
    revealController.reveal(self)
    revealController.rightViewController.view.addGestureRecognizer(revealController.panGestureRecognizer())
    }
}
}

我的listViewControllerClass:(sw_right):

class listViewController: UIViewController {
var receivedString: String?

    override func viewDidLoad() {
    super.viewDidLoad()

    let lbNavTitle = UILabel (frame: CGRect(x: 0, y: 0, width: 340, height: 44))
    lbNavTitle.textColor = UIColor.white
    lbNavTitle.textAlignment = .left
    lbNavTitle.text = receivedString
        print(receivedString)
    lbNavTitle.font = UIFont.boldSystemFont(ofSize: 15)
    self.navigationItem.titleView = lbNavTitle

    let myBtn : UIButton = UIButton()
    myBtn.setImage(UIImage(named: "menufinal"), for: .normal)
    myBtn.addTarget(self, action: #selector(ViewController.fbButtonPressed), for: UIControlEvents.touchUpInside)
    myBtn.frame = CGRect(x: 5, y: 0, width: 25, height: 17)
    self.navigationItem.setLeftBarButton(UIBarButtonItem(customView: myBtn), animated: false)

    self.navigationController!.navigationBar.barTintColor = UIColor(r:76,g:101,b:111)
    self.navigationItem.leftBarButtonItem?.tintColor = UIColor.white
    self.navigationController?.navigationBar.isTranslucent = false
    self.navigationController?.view.backgroundColor = .clear
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func fbButtonPressed(_ sender:UIBarButtonItem!)
{
    let revealController = self.revealViewController() as! RevealViewController
    revealController.reveal(sender)

}

1 个答案:

答案 0 :(得分:0)

确定。你可以稍微清理一下。

替换它:

var controller: UIViewController!
switch indexPath.row {
case 0: controller = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
case 1: controller = storyboard.instantiateViewController(withIdentifier: "listViewController") as! listViewController
case 2: controller = storyboard.instantiateViewController(withIdentifier: "listViewController") as! listViewController
case 3: controller = storyboard.instantiateViewController(withIdentifier: "listViewController") as! listViewController
case 4: controller = storyboard.instantiateViewController(withIdentifier: "timeTableViewController") as! timeTableViewController
case 5: controller  = storyboard.instantiateViewController(withIdentifier: "calenderViewController") as! calenderViewController
case 6: controller = storyboard.instantiateViewController(withIdentifier: "reportFirstViewController") as! reportFirstViewController

使用:

var controller: UIViewController!
var navController: UINavigationController!
switch indexPath.row {
case 0:
  controller = storyboard.instantiateViewController(withIdentifier: "ViewController")
  navController = UINavigationController(rootViewController: controller as! ViewController)
case 1, 2, 3:
  controller = storyboard.instantiateViewController(withIdentifier: "listViewController")
  (controller as! listViewController).receivedString = tableviewitems[indexPath.row]
  navController = UINavigationController(rootViewController: controller as! listViewController)
case 4:
  controller = storyboard.instantiateViewController(withIdentifier: "timeTableViewController")
  navController = UINavigationController(rootViewController: controller as! timeTableViewController)
case 5:
  controller = storyboard.instantiateViewController(withIdentifier: "calenderViewController")
  navController = UINavigationController(rootViewController: controller as! calendarViewController)
case 6:
  controller = storyboard.instantiateViewController(withIdentifier: "reportFirstViewController")     
  navController = UINavigationController(rootViewController: controller as! reportFirstViewController)

请注意,您还必须更改此部分:

if a == 0 {
  let navController = UINavigationController(rootViewController: controller)
  let revealController = self.revealViewController() as! RevealViewController
  revealController.rightViewController = navController

要:

if a == 0 {
  let revealController = self.revealViewController() as! RevealViewController
  revealController.rightViewController = navController

我希望这会有所帮助。