How can I prevent the view controller from starting from the top of the feed when a user leaves and goes back?
Basically, I have the main VC and a detailed VC. When the user selects a cell, it should jump to the detailed VC. If she/he goes back, it should leave her back to where she/he was.
I get that my code is calling "reload Data" every time the VC loads, but what other options do I have then if I don't call that method?
Here's an image of my main storyboard if it helps. Main VC(left) is the feed tableView where the user can tap on the cell. When he/she taps on the cell, it "segues" to the comment table VC (right). When he/she is done commenting she/he can go back to the main VC and continuing going down the feed. (ideally, except it keeps loading from the newest post, rather than segueing the user back to where she/he was down in the feed)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostCell
let post: PostModel
post = postList[indexPath.row]
func set(post: PostModel) {
ImageService.downloadImage(withURL: post.author.patthToImage) { image in
cell.profileImage.image = image
}
}
set(post: postList[indexPath.row])
cell.descriptionLabel.numberOfLines = 0 // line wrap
cell.descriptionLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
cell.descriptionLabel.text = post.message
cell.authorLabel.text = post.author.username
cell.timeLabel.text = post.createdAt.calendarTimeSinceNow()
//takes care of post image hidding and showing
if self.postList[indexPath.row].pathToImage != "" {
cell.postImage.isHidden = false
cell.postImage?.downloadImage(from: self.postList[indexPath.row].pathToImage)
} else {
cell.postImage.isHidden = true
}
if cell.postImage.isHidden == true {
cell.postImage.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let post: PostModel
post = postList[indexPath.row]
myIndex = indexPath.row
myPost = post.postID!
performSegue(withIdentifier: "segue", sender: self)
print(myIndex)
print(post.postID)
}
override func viewDidLoad() {
super.viewDidLoad()
beginBatchFetch()
}
func beginBatchFetch() {
fetchingMore = true
fetchPosts { newPosts in
self.postList.append(contentsOf: newPosts)
self.endReached = newPosts.count == 0
self.fetchingMore = false
self.tableViewPost.reloadData()
}
}
func fetchPosts(completion: #escaping(_ postList:[PostModel])->()) {
ref = Database.database().reference().child("posts")
var queryRef:DatabaseQuery
let lastPost = self.postList.last
if lastPost != nil {
let lastTimestamp = lastPost!.createdAt.timeIntervalSince1970 * 1000
queryRef = ref.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp).queryLimited(toLast:20)
} else {
queryRef = ref.queryOrdered(byChild: "timestamp").queryLimited(toLast:20)
}
queryRef.observeSingleEvent(of: .value, with: { snapshot in
var tempPosts = [PostModel]()
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let author = dict["author"] as? [String:Any],
let uid = author["uid"] as? String,
let username = author["username"] as? String,
let fullname = author["fullname"] as? String,
let patthToImage = author["patthToImage"] as? String,
let url = URL(string:patthToImage),
let pathToImage = dict["pathToImage"] as? String,
let likes = dict["likes"] as? Int,
let postID = dict["postID"] as? String,
let message = dict["message"] as? String,
let genre = dict["genre"] as? String,
let timestamp = dict["timestamp"] as? Double {
let userProfile = UserProfile(uid: uid, fullname: fullname, username: username, patthToImage: url)
let post = PostModel(genre: genre, likes: likes, message: message, pathToImage: pathToImage, postID: postID, userID: pathToImage, timestamp: timestamp, id: childSnapshot.key, author: userProfile)
tempPosts.insert(post, at: 0)
}
}
//first two
self.postList = tempPosts
self.tableViewPost.reloadData()
// return completion(tempPosts)
})
The issue, as Matt pointed out, is that you are segueing to the detailVC and then segueing back to the original VC. This is creating a new instance of the original VC.
What you should do, in your VC with the table view, is instantiate and present the destination view controller when a cell is selected. So you should replace performSegue(withIdentifier: "segue", sender: self) with something like:
let storyboard = UIStoryboard(name: "Main", bundle: .main)
var destinationVC = (storyboard.instantiateViewController(withIdentifier: "DestinationVC") as! DestinationViewController)
present(destinationVC!, animated: ture, completion: nil)
NOTE: View Controller storyboard identifiers can be set in the interface builder. So, if you are wanting to use this line: var destinationVC = (storyboard.instantiateViewController(withIdentifier: "DestinationVC") as! DestinationViewController), you will first have to set the Storyboard ID in the interface builder:
Now, within your destination view controller, instead of segueing when your done button is pressed, you want to use the dismiss method to dismiss the presented view controller.
class DestinationViewController: UIViewController {
#IBAction func backButtonPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
Now, the original VC with the table view stays in memory while the destination VC is presented over it. When the user presses the "back" button, it will dismiss the destination VC and the original VC should reappear as you left it.
"I added a "back" button to segue back" That's the problem. The way to go back is to call dismiss — not use a second segue (unless it is a special "unwind" segue, but you don't know how to do that).
Related
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 would like to pass data from EditPostViewController to NewsfeedTableViewController using delegates, but func remove(mediaItem:_) is never called in the adopting class NewsfeedTableViewController. What am I doing wrong?
NewsfeedTableViewController: UITableViewController, EditPostViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//set ourselves as the delegate
let editPostVC = storyboard?.instantiateViewController(withIdentifier: "EditPostViewController") as! EditPostViewController
editPostVC.delegate = self
}
//remove the row so that we can load a new one with the updated data
func remove(mediaItem: Media) {
print("media is received heeeee")
// it does't print anything
}
}
extension NewsfeedTableViewController {
//when edit button is touched, send the corresponding Media to EditPostViewController
func editPost(cell: MediaTableViewCell) {
let editPostVC = storyboard?.instantiateViewController(withIdentifier: "EditPostViewController") as? EditPostViewController
guard let indexPath = tableView.indexPath(for: cell) else {
print("indexpath was not received")
return}
editPostVC?.currentUser = currentUser
editPostVC?.mediaReceived = cell.mediaObject
self.navigationController?.pushViewController(editPostVC!, animated: true)
}
protocol EditPostViewControllerDelegate: class {
func remove(mediaItem: Media)
}
class EditPostViewController: UITableViewController {
weak var delegate: EditPostViewControllerDelegate?
#IBAction func uploadDidTap(_ sender: Any) {
let mediaReceived = Media()
delegate?.remove(mediaItem: mediaReceived)
}
}
The objects instantiating in viewDidLoad(:) and on edit button click event are not the same objects. Make a variable
var editPostVC: EditPostViewController?
instantiate in in viewDidLoad(:) with delegate
editPostVC = storyboard?.instantiateViewController(withIdentifier: "EditPostViewController") as! EditPostViewController
editPostVC.delegate = self
and then present it on click event
navigationController?.pushViewController(editPostVC, animated: true)
or
present(editPostVC, animated: true, completion: nil)
you can pass data from presenter to presented VC before or after presenting the VC.
editPostVC.data = self.data
I suggest having a property in NewsfeedTableViewController
var editPostViewController: EditPostViewController?
and then assigning to that when you instantiate the EditPostViewController.
The idea is that it stops the class being autoreleased when NewsfeedTableViewController.viewDidLoad returns.
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.
Hey guys so I'm working on an application where I have imported a contacts list from my device and I am given the option to "add" the contact but it really doesn't do much when it comes to functionality. I'm not the best coder so try to hear me out. what I am trying to do is take the data/ selected table view cell and display it on another page. I "think" that this is what I should do because I have tried to display the data on another page but get an error when I move my OVERRIDE function. that makes me believe that I need to take the data, which I believe is newContact? and set that as a variable and then display it on a new page where I can create a new view controller and add the code without error.
I essentially need to figure out what my JSON data is saved as, then set that equivalent to a string if that is possible, so I can send it to my new view controller or and send it to my database with code I already have created.
I am just not sure where to enter the statements because of errors that I am getting and what the exact code would be.
Sorry for the awful description of what I am trying to perform, I have a grasp of what is needed to be done but I am a beginner.
My Master View Controller that takes the contacts from my phone and accesses them.
import UIKit
import Contacts
import ContactsUI
class MainViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var tableView: UITableView!
var store = CNContactStore()
var contacts: [CNContact] = []
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: - User Actions
#IBAction func contactListPressed(_ sender: AnyObject) {
let contactPickerViewController = CNContactPickerViewController()
contactPickerViewController.delegate = self
present(contactPickerViewController, animated: true, completion: nil)
}
#IBAction func addContactPressed(_ sender: AnyObject) {
let newContact = CNMutableContact()
newContact.givenName = "Apps"
newContact.familyName = "Foundations"
newContact.nickname = "AF"
if let image = UIImage(named: "logo-apps-foundation.jpg"),
let data = UIImagePNGRepresentation(image){
newContact.imageData = data
}
let phone = CNLabeledValue(label: CNLabelWork, value: CNPhoneNumber(stringValue: "+441234567890"))
newContact.phoneNumbers = [phone]
let email = "" //Your Input goes here
let Email = CNLabeledValue(label:CNLabelWork, value: email as NSString)
newContact.emailAddresses = [Email]
newContact.jobTitle = "Apps Foundation"
newContact.organizationName = "Apps Foundation"
newContact.departmentName = "IT"
let facebookProfile = CNLabeledValue(label: "Facebook", value: CNSocialProfile(urlString: "https://www.facebook.com/appsfoundation", username: "AppsFoundation", userIdentifier: "appsfoundation", service: CNSocialProfileServiceFacebook))
let twitterProfile = CNLabeledValue(label: "Twitter", value: CNSocialProfile(urlString: "https://twitter.com/AppsFoundation", username: "AppsFoundation", userIdentifier: "appsfoundation", service: CNSocialProfileServiceTwitter))
newContact.socialProfiles = [facebookProfile, twitterProfile]
let skypeProfile = CNLabeledValue(label: "Skype", value: CNInstantMessageAddress(username: "AppsFoundation", service: CNInstantMessageServiceSkype))
newContact.instantMessageAddresses = [skypeProfile]
var birthday = DateComponents()
birthday.year = 1991
birthday.month = 1
birthday.day = 1
newContact.birthday = birthday
let request = CNSaveRequest()
request.add(newContact, toContainerWithIdentifier: nil)
do {
try store.execute(request)
let alert = UIAlertController(title: "Contacts iOS 9", message: "New contact has been created", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
} catch let error{
print(error)
}
}
#IBAction func textFieldValueChanged(_ sender: AnyObject) {
if let query = textField.text {
findContactsWithName(query)
}
}
//MARK: - Private Methods
func findContactsWithName(_ name: String) {
AppDelegate.sharedDelegate().checkAccessStatus({ (accessGranted) -> Void in
if accessGranted {
DispatchQueue.main.async(execute: { () -> Void in
do {
let predicate: NSPredicate = CNContact.predicateForContacts(matchingName: name)
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactBirthdayKey, CNContactViewController.descriptorForRequiredKeys()] as [Any]
self.contacts = try self.store.unifiedContacts(matching: predicate, keysToFetch:keysToFetch as! [CNKeyDescriptor])
self.tableView.reloadData()
}
catch {
print("Unable to refetch the selected contact.")
}
})
}
})
}
func updateContact(_ contactIdentifier: String) {
do {
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactBirthdayKey, CNContactPhoneNumbersKey, CNContactViewController.descriptorForRequiredKeys()] as [Any]
let contact = try store.unifiedContact(withIdentifier: contactIdentifier, keysToFetch:keysToFetch as! [CNKeyDescriptor])
let contactToUpdate = contact.mutableCopy() as! CNMutableContact
contactToUpdate.phoneNumbers = [CNLabeledValue(label: CNLabelWork, value: CNPhoneNumber(stringValue: "+440987654321"))]
let saveRequest = CNSaveRequest()
saveRequest.update(contactToUpdate)
try store.execute(saveRequest)
} catch let error{
print(error)
}
}
}
//MARK: - UITableViewDataSource
extension MainViewController: CNContactPickerDelegate {
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
let selectedContactID = contact.identifier
updateContact(selectedContactID)
}
}
//MARK: - UITableViewDataSource
extension MainViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let CellIdentifier = "MyCell"
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier)
cell!.textLabel!.text = contacts[indexPath.row].givenName + " " + contacts[indexPath.row].familyName
if let birthday = contacts[indexPath.row].birthday {
let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.long
formatter.timeStyle = .none
cell!.detailTextLabel?.text = formatter.string(from: ((birthday as NSDateComponents).date)!)
}
return cell!
}
}
//MARK: - UITableViewDelegate
extension MainViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let controller = CNContactViewController(for: contacts[indexPath.row])
controller.contactStore = self.store
controller.allowsEditing = false
self.navigationController?.pushViewController(controller, animated: true)
}
}
I know I need to incorporate something like this but I am not sure where or how to set the JSON data to a variable or the correct type and then incorporate code of this type
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let controller = segue.destination as! ViewControllerB
controller.selectedName = objects[indexPath.row]
}
}
}
sorry for the awful explanation. any help possible would be appreciated, I have been struggling for quite some time.
First of all, you need to have the other view controller that you are trying to pass data to. It can either be on the Interface Builder or done programmatically (I'll assume it's on the IB for now). Then you'll need to setup a segue between the Main VC and the Details VC and give it an identifier e.g. showDetail.
Next would be to determine the data that Details VC needs for it to work properly. You can have individual variables for each data item (e.g. name, age, phone, email, etc) but usually if there is a lot of info, it's best to use a data model. In your case, since you are trying to display contact info, you can simply reuse CNContact.
So you simply need a CNContact in your Details VC that you'll set before transitioning from Main VC in the prepareForSegue function. And to initiate the segue, all you have to do is call performSegue function.
Hope that at least gives you some direction
I am making an application which has nearly three buttons in its Table View Cell. One for sharing and the other two for other purpose. Before adding the sharing button code everything was working fine for me , the other two buttons were working great but now as soon as I wrote the function for sharing button I am getting an error in the 'Else' part in the IndexPath statement. Here the cell leads to another view controller. Please help me understand where I am going wrong.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showView"
{
let vc = segue.destinationViewController as UIViewController
let controller = vc.popoverPresentationController
//vc.preferredContentSize = CGSizeMake(265, 400)
if controller != nil
{
controller?.delegate = self
}
}
else{
//STotal.removeAll()
let indexpath: NSIndexPath = self.tableViews.indexPathForSelectedRow!
//error in the above line
//let DestViewController = segue.destinationViewController as! CellViewController
var newName : String
var newDetails : String
var newTime : String
var newTitle : String
var newImage : UIImage
newName = names[indexpath.row]
newTitle = breeds[indexpath.row]
newTime = timeDisp[indexpath.row]
newDetails = texty[indexpath.row] as! String
newImage = images[indexpath.row]!
SName = newName
STitle = newTitle
STime = newTime
SDetails = newDetails
SImages = newImage
// DestViewController.SName = newName
self.tableViews.deselectRowAtIndexPath(indexpath, animated: true)
}
}
I think your problem might be that when you click on the shared button, the cell does not go into the "selected" state and so the method you call
self.tableViews.indexPathForSelectedRow!
leads to a crash, because there is not selected row and you force unwrap it.
One possible solution might be something like this:
func didPressShareButton(sender: UITableViewCell) {
let indexPath = tableView.indexPathForCell(sender)
// prepare your DetailViewController now / show segue
}
Check to see if an indexPath is actually selected.
var indexPath = NSIndexPath()
if let path = tableViews.indexPathForSelectedRow {
indexPath = path
} else {
//Do something if there is no indexPath selected
return//end function
}