Cannot assign value of type String to type UILabel
You need to pass the String
instead of setting text to label, because when you correct it and set like this sendToDetailViewController.messageLabelDos.text = sendingText
, you will get nil error because messageLabelDos
is not initialize yet, so try like this. Create one string instance in DetailViewController
and use that inside prepareForSegue
for passing String and then use that String
instance in viewDidLoad
to assign Label
to text.
class ViewController: UIViewController {
//Your other methods
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SendDataSegue" {
if let sendToDetailViewController = segue.destinationViewController as? DetailViewController {
var sendingText = metadataObj.stringValue
sendToDetailViewController.messageDos = sendingText
}
}
}
}
Inside DetailViewController
var messageDos: String = ""
override func viewDidLoad() {
super.viewDidLoad()
self.messageLabelDos.text = messageDos
}
Cannot assign value of type 'String?' to type 'UILabel?'
Change this
self.resultLabel = svc.label.text
to
self.resultLabel.text = svc.label.text
Cannot assign value of type 'String?' OR 'Int' to type 'UILabel?'
You need to assign it to text
property of UILabel
if let boards = self.boards {
let model = boards[indexPath.row]
cell.txtTitle.text = model.b_title
cell.txtUserId.text = model.userId
cell.txtConut.text = String(model.b_count ?? 0)
cell.txtSaveTime.text = String(model.b_date ?? 0)
cell.txtComment.text = String(model.commentCount ?? 0)
}
Cannot assign value of type 'String' to type 'UILabel?'
If your label
is declared as a Label?
, try:
if let realLabel = label {
realLabel.text = " "
} else {
print("There is no label")
}
Cannot assign value of type 'String' to type 'UILabel'
Actually you would have to write
detail.lblName.text = name[indexPath.row]
but this will cause a crash. Assign the value to the name
property
detail.name = name[indexPath.row]
Trying to pass the value of a label but receiving an error 'Cannot assign value of type 'Organizations?' to type 'String'
The error is telling you why. You’re trying to assign the organization to the variable that wants a string.
You want to assign it to the actual organization
inside the detailController
Or assign the string value to one of the row titles or whatever value you want to show.
organizationName = selectedOrganization.rowTitles[indexPath.row]
For a cleaner detail I would use the didSet
option inside your detail view controller
Class DetailViewController:UIViewController {
var organization:Organizations? = nil
var selectedRow: Int = 0
func viewDidLoad() {
super.viewDidLoad()
setupLabel()
}
private func setupLabel() {
guard let org = self.organization else {
print(“no organization set”)
return
}
self.organizationLabel.text = org.rowTitles[selectedRow]
}
}
Then in your tableView VC I would just assign the selected organization to the variable in the detail
So you could add another variable that holds the selected row alongside your selectedOrganization in your tableView controller in your didSelectRow
func, set the variable to the selected row and you should be good to go
var selectedRow = 0
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? OrganizationsDetailViewController {
destination.organization= selectedOrganizations
destination.selectedRow = selectedRow
}
}
Related Topics
Change Text Color of Items in Uiactionsheet - iOS 8
How to Detect Delete Key on an Uitextfield in iOS 8
Receiving Push Notifications While in Background
Core Data: Do Child Contexts Ever Get Permanent Objectids for Newly Inserted Objects
Passing Data to Apple Watch App
In iOS, How to Create a Button That Is Always on Top of All Other View Controllers
Steps to Upload an iPhone Application to the Appstore
Iphone: Where the .Dsym File Is Located in Crash Report
How to Throttle Search (Based on Typing Speed) in iOS Uisearchbar
Instead of Push Segue How to Replace View Controller (Or Remove from Navigation Stack)
iOS How to Detect When App Was Removed from Process
Swift - Saving Highscore Using Nsuserdefaults
Presentviewcontroller from Custom Tablecell in Xib
How to Get the Udid in iOS 6 and iOS 7
Force Landscape Viewcontroller in iOS 7
Can't Assign Multiple Buttons to Uinavigationitem When Using Storyboard with iOS 5