gpt4 book ai didi

ios - 如何使用 SWrevaelviewController swift 3 将数据从 TableView 的特定行传递到另一个 View Controller

转载 作者:行者123 更新时间:2023-11-30 12:09:18 25 4
gpt4 key购买 nike

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

-> 目前我可以在选择一行时导航到不同的 View Controller

当我选择一行时,我想导航到listviewcontroller(仅限case:1、case:2、case:3)。我想根据我的行数据设置我的 labelText (recievedString)和。在休息开关情况下,我只想导航到不同的 Controller 。

这是我的 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)

}

最佳答案

好的。您可以稍微清理一下。

替换这个:

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

我希望这会有所帮助。

关于ios - 如何使用 SWrevaelviewController swift 3 将数据从 TableView 的特定行传递到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46266110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com