Hi Im getting the swift error = Value of type 'UITableView' has no member 'labelText' - swift

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Share" {
var destViewController = (segue.destinationViewController as! UITableViewController).tableView //Warning
destViewController.labelText = TEXT.text!
//Error = Value of type 'UITableView' has no member 'labelText'
}

The reason that you are getting that error is pretty self-explanatory: there is no defined variable "labelText" in the UITableViewController class. What exactly are you trying to accomplish with this code? It is very ambiguous. If you were more clear about what you were trying to do, providing an answer for you would be much easier.

I think you want to use textLabel instead of labelText

Related

what replaces indexPathForSelectedRow in Swift 3?

In projects prior to Swift 3 - If I was looking to segue an indexPath from a tableController, I'd have a line of code like
let indexPath = self.tableView.indexPathForSelectedRow!
However, in Swift 3, I am unable to write this. I can't seem to find any documentation on Apple's site or other SO questions to find a workable piece of code.
In response to Frankie's comment, I went back to my code (here is all of the current segue Swift 3 code) and have tried to copy and paste above code snippet... but to no avail. Xcode does not auto complete if I enter "self.tableView". It does not recognize indexPathForSelectedRow! either. I'm missing something.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "dineSegue"
{
if let destinationViewController = segue.destination as? RestaurantController
{
let indexPath = DineController.indexPathForSelectedRow!
destinationViewController.restaurantIndex = index
}
}
}
I googled 'indexPathForSelectedRow Apple documentation` and was able to find the property reference for you as it was conveniently the first result.
https://developer.apple.com/reference/uikit/uitableview/1615000-indexpathforselectedrow
That being said, it's exactly what you posted in Swift 3.
self.tableView.indexPathForSelectedRow
Are you sure the "DineController" reference is to the actual UITableView
When I had this issue, my initial reference was to the generic class, not the actual table I had coded.

Use of unresolved identifier 'tableView'

I had an a problem with this code, this identifier didn't run with me
import UIKit
class RestaurantDetailViewController: UIViewController {
#IBOutlet var restaurantImageView: UIImageView!
var restaurantImage = ""
override func viewDidLoad () {
super.viewDidLoad ()
restaurantImageView.image = UIImage(named: restaurantImage)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showRestaurantDetail" {
if let indexPath = tableView.indexPathSelectedRow {
let destinationController = segue.destinationViewController as! RestaurantDetailViewController
destinationController.restaurantImage = restaurantImageView[indexPath.row]
}
}
}
}
The error is : Use of unresolved identifier 'tableView'
Looking for a solution.
Thanks,
The prepareForSegue method is obviously in the wrong class (copy and paste error?).
It belongs to the Master controller.
Do you have a tableview in your VIEWCONTROLLER ? If yes then create a IBOOutlet between your class and viewcontroller
you can see this question or this this for reference.
This behavior is called "scope" and is crucial to any programming language. Variables declared inside a method are neither visible outside it nor do they persist when that method has finished running. and in your case for creating tableView variable you have to create an IBOoutlet or you can define it programmatically too you can get more detail from here
and here

Swift weak var scenario

I was reading the CoreData docs here and came across the following example illustrating how to implement a segue from a parent list to a child using dependency injection and was a little confused by the code sample given.
class DetailViewController: UIViewController {
weak var employee: AAAEmployeeMO?
}
and in the MasterViewController
let CellDetailIdentifier = "CellDetailIdentifier"
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segue.identifier! {
case CellDetailIdentifier:
let destination = segue.destinationViewController as! DetailViewController
let indexPath = tableView.indexPathForSelectedRow!
let selectedObject = fetchedResultsController.objectAtIndexPath(indexPath) as! AAAEmployeeMO
destination.employee = selectedObject
default:
print("Unknown segue: \(segue.identifier)")
}
}
I've got a decent understanding of weak variables but am a bit confused with this particular case because I don't see how it is necessary in this scenario.
Can anyone enlighten me as to why it is used here? Where is the potential for a strong reference cycle?

Thread 1: Signal SIGABRT with segue in swift

I have several tableview. I am trying to make a simple application and after adding more view when I get the third tableView this error "Thread 1 Signal SIGABRT" and the app wont open in iOS Simulator. The error points to this line of code:
let VC :DetailCityTableViewController = segue.destinationViewController as! DetailCityTableViewController
and display error :
Could not cast value of type 'UITableViewController' (0x102af47f8) to 'balen.DetailCityTableViewController' (0x1013d7560).
(lldb)
Full Code :
tableview2 pass to tableView3
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let path : NSIndexPath = self.tableView.indexPathForSelectedRow!
if segue.identifier == "DetailCitySegue" {
let VC :DetailCityTableViewController = segue.destinationViewController as! DetailCityTableViewController
VC.urlDetailCity = urlDetail!// pass url city
VC.cityIdSelectet = cityId[path.row] //pas CountryID selected
print(cityId[path.row])
}
}
I am , I think the problem is segue .What do you think?
No, the problem is '
Could not cast value of type 'UITableViewController' (0x102af47f8) to
'balen.DetailCityTableViewController''
You didn't change the class type of the view controller to DetailCityTableViewController in interface builder or the segue has a different destination than you think.

Type 'MasterViewController' does not conform to protocol 'CropPerformanceControllerDelegate'

On my app there are several views where you input certain values (settings) and so these settings get sent back to the master view using delegates. I already had this setup with 2 other views, and just copied and pasted the required code and changed the variables, however this brought up the error; "Type 'MasterViewController' does not conform to protocol 'CropPerformanceControllerDelegate'"The Code is below:
// MasterViewController
class MasterViewController: UIViewController, SettingsControllerDelegate, RainfallDataControllerDelegate, CropPerformanceControllerDelegate {
// Other variables/functions...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "MasterToSettings" {
let vc = segue.destinationViewController as! Settings
vc.delegate = self
}
if segue.identifier == "MasterToRainfallData" {
let vc = segue.destinationViewController as! RainfallData
vc.delegate = self
}
if segue.identifier == "MasterToCropPerformance" {
let vc = segue.destinationViewController as! CropPerformance
vc.delegate = self
}
}
func cropPerformanceSettingsSaved(controller: CropPerformance, irrigationResponseFactor: Double, wdImpactFactor: Double, potentialYield: Double) {
controller.navigationController?.popViewControllerAnimated(true)
irrigationResponseFactorM = irrigationResponseFactor
wdImpactFactorM = wdImpactFactor
potentialYieldM = potentialYield
}
}
// New View
// CropPerformanceView
protocol CropPerformanceControllerDelegate {
func cropPerformanceSettingsSaved(controller: CropPerformance, irrigationResponseFactor: Double, wdImpactFactor: Double, potentialYield: Double)
}
class CropPerformance: UIViewController {
var delegate: CropPerformanceControllerDelegate? = nil
// Other functions and Variables
#IBAction func updateCropSettings(sender: AnyObject) {
// Other stuff
if (delegate != nil) {
delegate!.cropPerformanceSettingsSaved(self, irrigationResponseFactor: irrigationResponseFactor!, wdImpactFactor: wdImpactFactor!, potentialYield: potentialYield!)
}
}
}
So this exact same code is used for the settings and rainfallData views and there are no issues, however now on the master view the 'CropPerformanceControllerDelegate' does not seem to be recognised and any uses of the class "CropPerformance" cause the error; "Use of undeclared type 'CropPerformance'". I hope this is enough information, all code to do with the delegate is posted, all other unnecessary variable declarations and functions I left out.
I had looked for other answers and they all said that you need to implement all required methods if you want to conform to the protocols. What exactly does that mean? All of my functions are inside my class and the code works perfectly when I remove the parts regarding the delegate.
Thanks in advance.
I was not able to solve the problem as there was no mistake in the code, I simply started from scratch, made a new document, copy and pasted the same code, changed the name to CropPerformance2 and it worked. (It works with any other name except for CropPerformance). This is very strange and must have something to do with that the initial class "CropPerformace" that I had deleted as not completely wiped from the systems memory, at least that is my assumption. In conclusion there is no answer, I simply recreated everything and it worked.