1

enter image description hereI am using swift 2 beaucse my lappy does not suppoeted swift 3 beacuse of some issue. I want to pass data from view controller to table view controller .. but i got some error and here is my code

I want to pass value form view controller .I am using label . whatever i will write in label it will disply in table view controller. so it will REALTIME value !

I am using two view controller and one table view controller ...i have error in table view controller. I have some errors

1) cell1.textLabel!.text = tblLable.text! // error UItableview has no memeber in 'text'

view controller

@IBOutlet var labelText: UITextField!

override func viewDidLoad()
{
    super.viewDidLoad()
}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let DestViewController:PassDataView = segue.destinationViewController as! PassDataView
DestViewController.passdata = labelText.text!
 //print(DestViewController.passdata)
}

passdataview

@IBOutlet var Label: UILabel!

var passdata=String()

override func viewDidLoad()
{
    Label.text=passdata
}

}

tableviewcontroller

 override func viewDidLoad() {
    super.viewDidLoad()
    //myTableView.delegate = self;
   // myTableView.dataSource = self;
}

@IBOutlet var tblLable: UITableView!

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    let DestViewController:PassDataView = segue.destinationViewController as! PassDataView
    DestViewController.passdata = tblLable.text!// error UItableview has no memeber in 'text' 
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell1 = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath)
    cell1.textLabel!.text = tblLable.text!  // error UItableview has no memeber in 'text' 
    return cell1
}
4
  • are there two different view controller ? Commented Aug 16, 2017 at 12:02
  • A UITableView has no variable text, that's why you're seeing the error Commented Aug 16, 2017 at 12:03
  • then how i will disply my value? cell1.textLabel!.text = tblLable.text! @Ollie Commented Aug 16, 2017 at 12:06
  • Yes i am using two view controller @Dev_Tandel Commented Aug 16, 2017 at 12:06

1 Answer 1

2

You have to declare String variable in your 3rd View Controller (Table View Controller) like

var myString: String = ""

override func viewDidLoad() {
    super.viewDidLoad()
}

You can assign value to myString variable from your first view controller like

let DestViewController:tableviewcontroller = segue.destinationViewController as! tableviewcontroller
DestViewController.myString = labelText.text!

And your your tableview view method will be like

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell1 = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath)
    cell1.textLabel!.text = myString
    return cell1
}

The Above is the solution, below i am explaining what was the problem

There is no such text method for UITableview, hence you were getting error at

DestViewController.passdata = tblLable.text! //In first View Controller
cell1.textLabel!.text = tblLable.text! //In second View controller
Sign up to request clarification or add additional context in comments.

9 Comments

Hope this helps in future too
DestViewController.myString = labelText.text! //error no such text method for UITableview,
and i already declare value in passdataview controller see ---- var passdata=String()
You need to declare the string variable where you want to use it.
Change it to var passdata : String = "". () parentheses indicate this is class, we just need variable to of type String
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.