如何使用 SWrevaelviewController swift 3 将数据从 tableview 的特定行传递到另一个视图控制器
Posted
技术标签:
【中文标题】如何使用 SWrevaelviewController swift 3 将数据从 tableview 的特定行传递到另一个视图控制器【英文标题】:how to pass data from a particular row of a tableview to another view controller using SWrevaelviewController swift 3 【发布时间】:2017-09-17 16:05:32 【问题描述】:我正在使用带有 sw_front 和 sw_right 的 SWrevealViewController
。
-> 目前我可以在选择一行时导航到不同的视图控制器
当我选择一行时,我想导航到 listviewcontroller(仅在 case: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】:好的。你可以把它清理一下。
替换这个:
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
我希望这会有所帮助。
【讨论】:
它给出错误:'UIViewController' 类型的值没有成员'receivedString' 好的,请检查这个新的编辑。我希望它对你有用。 它的工作原理你能解释一下为什么它会出错以及你如何解决它 所以您已将controller
的类型设置为UIViewController
。但是receivedString
变量仅在listViewController
上可用。所以我把控制器变成了这种类型。
@himalayarajput 如果对您有帮助,请接受答案。以上是关于如何使用 SWrevaelviewController swift 3 将数据从 tableview 的特定行传递到另一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
如何在自动布局中使用约束标识符以及如何使用标识符更改约束? [迅速]
如何使用 AngularJS 的 ng-model 创建一个数组以及如何使用 jquery 提交?