I have a managedObject that is being passed from 1 view controller to another the first pass works fine but when I try to pass the next object after the relationship has been set it doesn't send anything and comes back as either nil or if I try to use other methods comes back with a syntax error. The code I am using for the view controllers is as follows
View Controller 1, The first object set:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
switch identifier {
case "popOver":
if let VC = segue.destinationViewController as? ClassDeckNameViewController
{
if let ppc = VC.popoverPresentationController {
VC.modalPresentationStyle = UIModalPresentationStyle.Popover
ppc.permittedArrowDirections = UIPopoverArrowDirection.Any
ppc.delegate = self
}
VC.classSave = (sender as! ClassSelection)
}
default: break
}
}
}
#IBAction func buttonPriest(sender: AnyObject) {
let entity = NSEntityDescription.entityForName("ClassSelection", inManagedObjectContext: classMOC!)
let newObject = ClassSelection(entity: entity!,insertIntoManagedObjectContext: classMOC)
newObject.classname = "Priest"
var error: NSError?
if let err = error {
println(err)
} else {
classMOC?.save(&error)
self.performSegueWithIdentifier("popOver", sender: newObject)
}
}
This passes the object without problem to the second view controller but this is the one that won't pass any further to the final presenting controller offering the user the final selections for their "Deck":
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showCardSelection" {
let detailVC: CardSelectionViewController = segue.destinationViewController as! CardSelectionViewController
detailVC.passedDeckObject = (sender as! Deck)
}
}
#IBAction func enterButton(sender: AnyObject) {
let entityDescription = NSEntityDescription.entityForName("Deck",inManagedObjectContext: managedObjectContext!)
let storeDeck = Deck(entity: entityDescription!,insertIntoManagedObjectContext: managedObjectContext)
storeDeck.deckname = usersDeckName.text
storeDeck.classSelected = classSave!
var error: NSError?
managedObjectContext?.save(&error)
if let err = error {
status.text = err.localizedFailureReason
} else {
usersDeckName.text = ""
status.text = "Deck Saved"
self.performSegueWithIdentifier("showCardSelection", sender: storeDeck)
}
}
I made passedDeckObject a variable of type Deck? in the final view controller to set the final relationship methods I know I am doing something wrong but I am unsure what! Any help with this would be amazing!
This looks to be a misconfiguration issue where the segue is being triggered directly in the storyboard rather than calling your code. As such the sender is a button rather than the new entity instance you're expecting.
To fix, disconnect the segue in the storyboard and connect (if it isn't already) the button to your action method in the view controller.
Related
I'm learning swift from couple months and one thing I keep screwing up is passing data between ViewControllers. When I PO the path of the forward looking variable from within the prepareForSegue method, the value is intact. But when the new ViewController actually appears and I checked its value, it is nil at that point. if anyone could point me in the right direction I'd be very appreciative.
class LoginViewController: UIViewController {
var user_ID:String = ""
//this below is within another method activated by button
Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
if error != nil {
print ("there was an error signing in")
print (error!.localizedDescription)
return
}
else {
//go to home screen
let userUID = result?.user.uid
print (userUID)
self.user_ID = userUID
self.performSegue(withIdentifier: "MainSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//this is the destination VC
let viewController = segue.destination as! ViewController
viewController.user_ID = self.user_ID
//NOTE: if I break here and PO viewConroller.user_ID the value is intact
let homeViewController = (self.storyboard?.instantiateViewController(withIdentifier: "MainVC"))! as! ViewController
//let mainViewController = ViewController()
//mainViewController.user_ID = userUID
self.present(homeViewController, animated: true, completion: nil)
}
class ViewController: UIViewController {
var persons = [Person]()
let db = Firestore.firestore()
var user_ID:String = ""
//NOTE: WHEN `viewdidload` runs value of user_ID is nil
Phillip in the comments above provided the solution. The present ViewController was the problem. The segue already does that. Thank you!!!
So I am trying to make an Painting app that can store the data that user gives (such as name of the painting, artist of the painting, year of the painting and image of the painting) and shows in a table view. I have 2 view controllers, first one is called ViewController that has a table view for showing the data (only name of the painting for the table view cell) and the second one is called DetailedVC which is for entering and saving the data, also showing the details of the data. In the second view controller I added 3 text fields, 1 image view that enables the user to go the photo library and a save button. I wrote this in my DetailedVC script for when the user taps one of the elements from the table view in my ViewController:
override func viewDidLoad() {
super.viewDidLoad()
if chosenPainting != "" {
savebutton.isHidden = true //Trying to hide the save button here!!
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Paintings")
let idString = chosenPaintingId?.uuidString
fetch.predicate = NSPredicate(format: "id = %#", idString!)
fetch.returnsObjectsAsFaults = false
do {
let results = try context.fetch(fetch)
if results.count > 0 {
for result in results as! [NSManagedObject] {
if let name = result.value(forKey: "name") as? String {
nameText.text = name
}
if let artist = result.value(forKey: "artist") as? String {
artistText.text = artist
}
if let year = result.value(forKey: "year") as? Int {
yearText.text = String(year)
}
if let imageData = result.value(forKey: "image") as? Data {
let imageD = UIImage(data: imageData)
imageView.image = imageD
}
}
}
} catch {
print("error")
}
} else {
savebutton.isEnabled = false
nameText.text = ""
artistText.text = ""
yearText.text = ""
}
I get the information from my ViewController like this:
#objc func AddButtonTapped(){
selectedPainting = ""
performSegue(withIdentifier: "toDetailedVC", sender: self)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedPaintingId = ids[indexPath.row]
selectedPainting = names[indexPath.row]
performSegue(withIdentifier: "toDetailedVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toDetailedVC" {
let destination = segue.destination as! DetailedVC
destination.chosenPainting = selectedPainting
destination.chosenPaintingId = selectedPaintingId
}
}
It works just fine below the savebutton.isHidden = true. I don't understand why the button is not disappearing. Everything else works correctly, text field's texts turn into the information that user gave but, the button is standing still right in the bottom. And even the savebutton.isEnabled = false is working. I thought there must be a problem with connection between my script and my storyboard so I deleted and did it again, but it didn't work again. I must have doing something wrong, can you help me about this?
I have a collection view with some cells representing a contact (their data has a phone number and name) and I am trying to add the contact to the iPhone contacts. I have created a segue from a button called "add contact" that is inside the CollectionViewCell to a navigation controller, and set its identifier as "ADD_CONTACT".
In the storyboard, my segue has a navigation controller with no root view controller.
in prepareToSegue of the view controller that delegates my UICollectionView I wrote this code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == ADD_CONTACT {
let dest = segue.destination as! UINavigationController
if let cell = sender as? SBInstructionCell {
if cell.isContact {
let newContact = CNMutableContact()
if let phone = cell.instructionBean?.contactAttachment?.phoneNumber{
newContact.phoneNumbers.append(CNLabeledValue(label: "home", value: CNPhoneNumber(stringValue: phone)))
}
if let name = cell.instructionBean?.contactAttachment?.contactName {
newContact.givenName.append(name)
}
let contactVC = CNContactViewController(forNewContact: newContact)
contactVC.contactStore = CNContactStore()
contactVC.delegate = self
dest.setViewControllers([contactVC], animated: false)
}
}
}
}
this results with a black screen.
How can this be fixed? I want to see the CNContactViewController
Eventually I solved this in a different approach using Closures.
In my UICollectionViewCell
I added this var:
var closureForContact: (()->())? = nil
Now on my button's action in the same cell I have this func:
#IBAction func addContactTapped(_ sender: UIButton) {
if closureForContact != nil{
closureForContact!()
}
}
Which calls the function.
In my CollectionView in cell for item at index path, I set the closure like this:
cell.closureForContact = {
if cell.isContact {
let newContact = CNMutableContact()
if let phone = cell.instructionBean?.contactAttachment?.phoneNumber{
newContact.phoneNumbers.append(CNLabeledValue(label: "home", value: CNPhoneNumber(stringValue: phone)))
}
if let name = cell.instructionBean?.contactAttachment?.contactName {
newContact.givenName.append(name)
}
let contactVC = CNContactViewController(forNewContact: newContact)
contactVC.contactStore = CNContactStore()
contactVC.delegate = self
contactVC.allowsEditing = true
contactVC.allowsActions = true
if let nav = self.navigationController {
nav.navigationBar.isTranslucent = false
nav.pushViewController(contactVC, animated: true)
}
}
}
This worked perfectly. I learned that for navigating from a cell, it is best to use closures.
I have a carousel and each carousel item contains a unique tableview. When you tap the add button it takes you to a new view controller so that you can give the tableview a name and associate items to display in each cell. Now when I hit the save button it performs the segue back to the carousel's viewcontroller. My prepareforsegue looks like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! BillSplittersViewController
let passedBill: NSManagedObject = bill as NSManagedObject
destinationVC.allBillSplitters = getUpdatedSplitters()
print(2)
print(destinationVC.allBillSplitters)
destinationVC.billName = billName
destinationVC.bill = passedBill
}
When the carousel view controller is displayed it doesn't show the added item in the carousel which loads from the allBillSplitters array. If I try reloading the carousel in viewdidload, viewwillappear or view willlayoutsubviews nothing changes. It does update if I do it in viewdidlayoutsubviews but you can see this happening which isn't great.
To debug I tried printing the array in prepareforsegue and then in viewdidload and it prints the array in prepareforsegue without the added item and the does the same thing in viewdidload -- but then it prints them again with the added item -- but I can't see it in the loaded view.
I'm using iCarousel if that helps. I am new to this and unsure what's going on so I don't know what code to use. I am using storyboards for both viewcontrollers and the segue is attached to the view and not the button itself. Any help would be great. Thanks!
More information:
#IBAction func saveButtonWasPressed() {
let managedContext = bill.managedObjectContext
if let splitter = splitter {
splitter.mutableSetValue(forKey: "items").removeAllObjects()
setBillSplitterValues(splitter)
setSelectedItemsToBillSplitter(splitter)
} else {
let entity = NSEntityDescription.entity(forEntityName: "BillSplitter", in: managedContext!)
let newBillSplitter = NSManagedObject(entity: entity!, insertInto: managedContext)
setBillSplitterValues(newBillSplitter)
setSelectedItemsToBillSplitter(newBillSplitter)
}
do {
try managedContext!.save()
}
catch let error as NSError {
print("Core Data save failed: \(error)")
}
self.performSegue(withIdentifier: "segueToBillSplitters", sender: self)
}
retrieving the new billSplitters:
func getUpdatedSplitters() -> [BillSplitter] {
var allBillSplitters = [BillSplitter]()
let managedContext = bill.managedObjectContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "BillSplitter")
let predicate = NSPredicate(format: "ANY bills == %#", bill)
fetchRequest.predicate = predicate
do {
let results =
try managedContext!.fetch(fetchRequest)
allBillSplitters = results as! [BillSplitter]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
return allBillSplitters
}
So when pressing save, it either updates an existing 'billSplitter' to coredata or adds a new one, then calls performSegue. In prepareForSegue it then fetches all the 'billSplitters' from CoreData and passes them to the destination view controller.
I thought a NSManagedObjectContextDidSave observer might help if it called performSegue once the notification was received but nothing changed.
Update:
I have tried the following in my saveButtonWasPressed method. When the viewcontroller loads, it quickly shows the old carousel then flickers and shows the updated carousel. I'm not sure where to go from here.
#IBAction func saveButtonWasPressed() {
DispatchQueue.global(qos: .background).async { [weak weakSelf = self] in
let managedContext = weakSelf?.bill.managedObjectContext
if let splitter = weakSelf?.splitter {
splitter.mutableSetValue(forKey: "items").removeAllObjects()
weakSelf?.setBillSplitterValues(splitter)
weakSelf?.setSelectedItemsToBillSplitter(splitter)
} else {
let entity = NSEntityDescription.entity(forEntityName: "BillSplitter", in: managedContext!)
let newBillSplitter = NSManagedObject(entity: entity!, insertInto: managedContext)
weakSelf?.setBillSplitterValues(newBillSplitter)
weakSelf?.setSelectedItemsToBillSplitter(newBillSplitter)
}
do {
try managedContext!.save()
}
catch let error as NSError {
print("Core Data save failed: \(error)")
}
DispatchQueue.main.async { [weak weakSelf = self] in
guard let weakSelf = weakSelf else { return }
weakSelf.performSegue(withIdentifier: "segueToBillSplitters", sender: self)
}
}
}
I may not understand what you are asking exactly but my only input is that when I use prepare for segue I always use a segue identifier
var valueToPass
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if (segue.identifier == "yourSegue") {
let viewController = segue.destination as! ViewController
viewController.passedValue = valueToPass as String
}
Your declare your identifier in the storyboard, also make sure your segue is an action segue
I've been struggling with this one for a while. The following code runs fine when build for ios7, but when build for ios8 just crashes. Any of these got deprecated on ios8?
Not sure if I am doing wrong by pushing a segue from a tab bar controller to a navigation controller.
If I comment my var moveVC I can transit to the HobbieFeedViewController, but then I do not pass any values to the next segue.
If I remove the navigation controller from HobbieFeedViewController the segue stops working.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
println("SENDER->\(sender)")
let cell = sender as! HobbiesTableViewCell
var cellTitle = String()
//unwrap safelly
// if let object = cell.textLabel?.text
if let object = cell.cellTitle.text
{
cellTitle = object
println("CELLTITLE->\(cellTitle)")
}
// if not
else
{
println("Could not get cellTitle")
}
// call segue
if (segue.identifier == "hobbieFeed")
{
var selectedRowIndex = self.myTableView.indexPathForSelectedRow()
// var moveVC: HobbieFeedViewController = segue.destinationViewController as! HobbieFeedViewController
//
// moveVC.moveId = selectedRowIndex!.row
// moveVC.selectedHobbie = cellTitle
println("PREPARE ROW->\(selectedRowIndex)")
println("PREPARE HOBBIE->\(cellTitle)")
}
}