1

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")
        }
    }
 }

}

`

Rakshith G B
  • 816
  • 2
  • 8
  • 24

2 Answers2

1

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.

e.g.

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.

Tim
  • 8,932
  • 4
  • 43
  • 64
  • Thank You so much! Could you please modify my complete code and post it? I've never worked with dictionaries and I'd like to know how. My actual situation is I have a button in AutomaticViewController. When this button is pressed it should change switch states in the ManualViewController. – Rakshith G B Jun 24 '15 at 19:06
  • The question has been answered. If there is part of the solution you aren't sure about, then I can elaborate. Otherwise, no I can't write the code for you. – Tim Jun 24 '15 at 19:52
  • I managed to add the dictionary my self like this: cell.deviceName.text = object["label"] as? String cell.deviceState.setOn(object["switchState"] as! Bool, animated: false) It worked. Now how do I implement the WhatToDo function, how do I know which switch is being pressed? – Rakshith G B Jun 24 '15 at 19:54
  • From the indexPath of the cell; you can either set a tag on each switch when you dequeue the cell or you can get cell by going through the superviews of the switch, finding the cell and asking the tableView for that cell's indexPath. – Tim Jun 24 '15 at 20:03
  • This answer is in Objective-C but should help guide you in the right direction: http://stackoverflow.com/questions/5869411/a-button-in-uitableviewcell/9160390#9160390 – Tim Jun 24 '15 at 20:04
  • Tagging method works fine, but the link you suggested has a part "indexPathForRowAtPoint: currentTouchPosition" how do i implement this in swift? I couldn't find anything called currentTouchPosition in swift. – Rakshith G B Jun 24 '15 at 20:22
  • `var indexPath = tableView.indexPathForRowAtPoint(somePoint)` – Tim Jun 24 '15 at 20:31
0

Well, You're question is not complete because you don't say how you start your "AutomaticViewController" but if you use a Segue to launch this viewController, you can simply forward the cell data (not the cell object) to the next view controller, update the data in AutomaticViewController and then use a protocol to update the tableview data in your ManualViewController

Mikael
  • 2,355
  • 1
  • 21
  • 45