2017-09-17 3 views
0

sw_frontとsw_rightでSWrevealViewControllerを使用しています。SWrevaelviewController swiftを使用して、テーブルビューの特定の行から別のビューコントローラにデータを渡す方法3

- >現在私は

私はlistviewcontrollerに移動する行選択に異なるviewcontrollersに移動することができています(場合:1、ケース2、ケース3のみ)Iは、行を選択したときと。私は私の行データ とに従って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) 

} 

答えて

0

[OK]をクリックします。これを少しきれいにすることができます。

これを置き換えます

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 

で:

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 
あなたもこの部分を変更する必要が

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) 

注意を

こちらがお役に立てば幸いです。

+0

エラーを出しています:タイプ 'UIViewController'の値にメンバー 'receivedString'がありません –

+0

OKこの新しい編集を確認してください。私はそれがあなたのために働くことを望む。 –

+0

ありがとうPranav kasetti –

関連する問題