Passing data from view controller to table view controller in ios swift 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
Pass data between TableView and another ViewController in Swift
Your prepareForSegue
must override the UIViewController method which is
func prepare(for segue: UIStoryboardSegue, sender: Any?)
Therefore your method in NewsfeedViewController
should be
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondViewController = segue.destination as? EventViewController
secondViewController?.data = sender as? eventStruct
}
it would also be a good idea to add some error checking
Passing data from tableView to ViewController in Swift
Try this.
ModelViewViewController
var selectedImage:String?
var selectedLabel:String?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("Row \(indexPath.row)selected")
selectedImage = self.tableData[indexPath.row]
selectedLabel = self.tableData[indexPath.row]
performSegueWithIdentifier("detailView", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailView") {
var vc = segue.destinationViewController as DetailViewController
vc.img = selectedImage
vc.lblDetail = selectedLabel
}
}
class DetailViewController: UIViewController {
@IBOutlet var imgDetail: UIImage!
@IBOutlet var lblDetail: UILabel!
var img:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
imgDetail = UIImage(named: img)
}
This should work.
How to pass data with index between view controllers programmaticallly?
You seem to be a long way off where you want to get to. Your second VC will need the able to render the content and at the moment it can do none of that. However to address the question of passing the data between VCs, the easier way would be to inject the data you need. There are many different approaches, but a simple one would be to...
Create a struct to hold the data you want, and then hold an array of these rather than an array of Strings as your primary data source. For example:
struct CountryInfo {
let name: String
let capital: String
let flag: UIImage
// etc...
}
and replace your myArray
with an array of these structs populated with the appropriate data.
Then in your initial VC's cellForRowAt
you can refer to the name property:
cell.textLabel!.text = "\(myArray[indexPath.row].name)"
In your second VC you will need a property that allows the appropriate struct to be injected and then referenced for display
class SecondViewController: UIViewController {
var country: CountryInfo!
//etc
}
Then you can inject the selected country's info into the second VC in didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let nextVC = SecondViewController()
nextVC.country = myArray[indexPath.row]
navigationController?.pushViewController(nextVC, animated: true)
}
Then all you have to do is display this data, which can be done from the second VC's viewDidLoad()
Passing Data from ViewController to UITableViewController
The solution
PIECE DEFINITIONS
ViewController.swift
import UIKit
class ViewController: UIViewController {
var someData: String? = ""
@IBOutlet weak var cpChoice: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func startBut(_ sender: Any) {
performSegue(withIdentifier: "startUp", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//get the string representing the segment chosen and assign it to someData
let theSegmentChosen = cpChoice.selectedSegmentIndex
someData = cpChoice.titleForSegment(at: theSegmentChosen)
let barVC = segue.destination as! UITabBarController //
let navigationControllerDestination = barVC.viewControllers?[0] as? navVC //the array index [0] indicates the first view controller belonging to the TabBarController
let firstViewControllerOfNavigationController = navigationControllerDestination?.topViewController as! CheckpointVC
firstViewControllerOfNavigationController.data = someData!
}
}
TabBarVC
import UIKit
class TabBarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
NavVC
import UIKit
class navVC: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
CheckpointVC.swift
import UIKit
class CheckpointVC: UITableViewController {
//the variable we'll pass the data to from the starting viewcontroller
var data: String = ""
@IBOutlet weak var theHeader: UINavigationItem!
override func viewDidLoad() {
super.viewDidLoad()
theHeader.title = data
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 10
}
}
Related Topics
Change Cell Height by The Content of The Textview Inside The Cell
How to Figure Out When a HTML5 Video Player Enters The Full Screen Mode on iOS/Ipads
iOS 8 [Uiapplication Sharedapplication].Scheduledlocalnotifications Empty
Default Uifont Size and Weight But Also Support Preferredfontfortextstyle
Swift: Iboutlets Are Nil in Custom Cell
Perform "Use Photo" Button on Custom Image Picker Overlay
In What Format Is This Date String
How to Set Requestserializer in Alamofire
Checking Cellular Network Type in iOS
How to Add Pagination in UItableview.
Data Repeat in UItableview When Scrolling
Initialization of 'Unsafepointer<Int>' Results in a Dangling Pointer
Is Calling Cellforrowatindexpath: Ever Practical
How to Launch IPA File in iOS 10 Real Device Using Appium 1.6.0