I have two view controllers : ManualViewController, AutomaticViewController ManualViewController has a table view with each cell having a label and a switch.
How do I access these switches from AutomaticViewController?
Heres my ManualViewController code:
import UIKit
class ManualViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var objects: NSMutableArray! = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = false
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.tabBarController?.tabBar.barTintColor = UIColor.blackColor()
// Do any additional setup after loading the view.
self.objects.addObject("iPhone")
self.objects.addObject("Apple Watch")
self.objects.addObject("Mac")
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.objects.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
cell.deviceName.text = self.objects.objectAtIndex(indexPath.row) as? String
cell.deviceState.tag = indexPath.row;
return cell
}
@IBAction func whatToDo(sender: UISwitch) {
var c = sender.tag
if c == 2
{
if sender.on
{
println("on")
}
else
{
println("off")
}
}
}
}
`
You don't want to access the cells of the other UIViewController, you want to access the datasource of the other UIViewController
.
AutomaticViewController will need a reference to the ManualViewController instance you've created, from there you can access the objects
NSMutableArray
but you haven't specified how the two currently have a reference to each other.
eg
var array = manualViewController.objects
This effectively gives you access to whatever cells are in the other ViewController.
In order to access the switches, you'll want to store their state in the NSMutableArray. So rather than an array of strings, have an array of dictionaries.
var objects = [["label" : "iPhone", "switchState": true],["label" : "Apple Watch", "switchState": true],["label" : "Mac", "switchState": true]]
Then in your cellForRowIndexPath method, you can access the state to determine the switch state and update that when the switch is tapped. Using what I mentioned earlier, you can then share that data between the two UIViewControllers.
好吧,您的问题还不完整,因为您没有说如何启动“ AutomaticViewController”,但是如果您使用Segue启动此viewController,则只需将单元格数据(而不是单元格对象)转发到下一个视图控制器,更新AutomaticViewController中的数据,然后使用协议更新ManualViewController中的tableview数据
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.