Passing Data with a button - ios

I have an app(my first) that has several view controllers.
VC1 - is an tableView with a name & image. (a list)
VC2 - is a View with name, image and two buttons. (list add/edit view)
Data passing between these two views works fine using protocols
VC3 - is a tableView with another list. (items for first list)
I have a Button on VC2 that segues to VC3.
So a user enters the first TableView and taps on a tableViewCell, it segues to the second View for editing that selected list. While in the second View(not a tableView) the user taps a button which segues to the third TableView to add/edit the items. Then the user taps the done button sending the user back to the second View.
This is my issue - how do I pickup/associate the list name from VC2 to VC3. When the user taps doneButton the items should be associated with that one list that the user was editing.
thank you, newbie

Here is a good tutorial from apple showing you how to do just that. The basic idea is that to add things to a table view. Have an array of objects in the VC1 that contains everything you want to show. Then pass that array to VC2 (which in turn passes it to VC3) using prepareForSegue. Here is a good segue tutorial.
For editing (moving and deleting cells), this had native support so you won't have to roll your own. The bottom of that tutorial link shows how to enable this.
The basic idea for deletion is that you want to remove the element from the array and then redraw the table view (VC1) without that element. Failing to keep the table view and array in sync will result in a crash.

Related

TextField text needs to mirror text from another ViewController’s TextField input

My Current Setup
ViewController3:
This ViewController has 18 TextFields stacked vertically. This is the “settings” screen where the user can add their default text (single words, comments or sentences). This view also has a save button to save using UserDefaults, which is working fine.
To access this view (settings screen) the user taps the settings button located on the main view (ViewController1) via a show segues. Once all editing done to the default texts (usually a once off and reused again and again), they tap the save button and then tap Done button to return to ViewController1.
ViewContoller2:
This ViewController has matching 18 TextFields (again stacked vertically), but each with a selection switch. If switch is active, means that TextField is selected. Out of the 18 TextFields which are stacked vertically, the user can select any given number of them.
The selected ones are then populated as a selected list of texts in the mail body of an email being sent from a Send Email button back on ViewController1.
ViewController1:
This is the main view of the app where the app will send off the email after entering desired text into a number of TextFields and it included the TextView that has the selected list of texts from ViewController2
Note: All transitions between ViewControllers are via show segues.
Question 1:
What code or how to write the code to enable the TextFields in ViewController2 to auto populate (copy or mirror) the text that has been entered it the TextFields in ViewController3?
Question 2:
How do get the selected TextFields from ViewController2 to show up in the body of the email as a list of items but vertically stacked on their individual lines?
Ok, let me see if I undestood.
VC3 is the settings view where you "register" the texts to be used at some point.
VC2 is the view that you select which texts you want to use/send/whatever
VC1 is the main screen, where you are displaying the texts you chose above
If you are using segues, I believe that your VC2 is not instanced when you are registering or changing data on the VC3. So, NotificationCenter is not an option like #dahiya_boy told.
If you are saving all the data on UserDefaults, can't you just load this data and apply on each TextField on VC2 when the user open the VC2 screen (probably on viewDidLoad )
Your second question... whats the problem?
If the problem is passing data between the VC2 and VC1, you can use NotificationCenter, or delegate.
If the problem is how to show it as a stack, so you can pass it as an array from VC2 to VC1 and appending everything using joined(separator:) and passing \n as the separator. (symbol for newline)

reload collection view by press back bar button item

I have a collectionView in my app that when the user taps one cell of it, the collectionView will reload data, in detail, I have an album that it could contain another album when user choose one of them it shows albums inside of that.
Now I want when user click on backBarButton the past data appears on collectionView (super-albums), but now backbarButton shows the past VC.
I use navigationController and show module segues between VCs.
Q: How should I implement that?
If your changing view on collection cell press. you can reload the collection view on viewWillAppear.

Unwind segue doesn't work

i'm doing an app that uses a TableViewController with a system of Adding/Editing items, using the same view. (however when you add an item the view is modal presented and when you edit it is shown)
I followed the great starter tutorial from Apple so basically my Storyboard looks like this
The segue going through the Navigation Controller is for adding and the other one is for editing. (I did everything according to the tutorial).
I did a segue between the Cancel item bar button and the exit icon of the ViewController and it works well when the view is modally presented (when I try to add an item).
However when I click on a cell to reach the view with the segue that shows it (to edit an item), both items in the navigation bar stop working. The prepareForSegue method is not called anymore. So I can't cancel or save.
I tried creating an unwind segue between the ViewController itself and the exit icon and to call it programmatically like this:
#IBAction func testButton(sender: UIBarButtonItem) {
print("we're inside")
self.performSegueWithIdentifier("cancelSegue", sender: self)
print("so what now")
}
and when I try to edit it and tapping the cancel button it results by just showing the two log messages and kind of skipping the performSegueWithIdentifier method. However adding still works fine.
Am I doing something wrong or have I misunderstood some basic notion about unwind segues?
This seems like quite a strange solution to your problem. While I can't comment on specifically why your unwind segue isn't working in the second case, things will start to get quite complicated with dismissing the New Programsegue since you're displaying it two different ways.
The common approach that we use is:
Use two completely different view controllers for creating and updating an assets. May be some duplicate code, but makes it slightly easier for other people to work on.
Use the same view controller for creating and updating buttons. If you're editing an item, you can pass it to the view controller with prepareForSegue. When the view controller loads, if an item is present, you can change the behaviour of the buttons, title etc. If no item is present, you know to create a new item.
For most implementations, it's much simpler to dismiss the views programatically. (i.e [self dismissViewControllerAnimated:NO completion:nil] for modal views and [self.navigationController popViewControllerAnimated:YES] for just returning to a vc in the same navigation controller stack.
Update
Did you link the Cancel button to the exit icon on the first view controller, or the second view controller (the one with the cancel and save button)?

TableView with detailed view

I little confused with configuring detailed view with MMDrawerController and without navigation controller.
I have TableView with custom cells inside ViewController. When users presses cell i want to show another ViewController with details about selected item. For this case i use didSelectRowAtIndexPath method from my tableview.
There are many cells in my table, that is why when user presses "back" button on detailed view i want to navigate to selected cell not to the top of table. What types of segues i should use?

Use case for push versus modal segues?

Let's say, I have a scene (pushed view controller with a navigation bar), which displays some tabular data in a table view.
In the navigation bar of that scene I have a + sign, which should open a new scene, where the user can add a new item (row to a core data table).
In the table view, each row has an arrow on the right side of each cell, which opens a scene where the user can edit that particular item's details.
Should I use a push or modal segue for the +?
Should I use a push or modal segue for the arrow?
What is the "best practise"?
I understand the difference between push and modal segues, but I want to know which is better suited for the above use cases.
If you want to follow Apple's best practices, I would suggest the following :
For the "Add" functionality, use a modal segue.
For example look at the contacts app. Pressing + shows a modal view controller.
What's the logic ? for start, modal view controllers usually have a "cancel" button, as opposed to the "back" button on a pushed vc.
When the user presses "back" - he'd expect a way to come back to the vc. Usually "back" saves your data on iOS (auto-saved).
So by using a modal segue you force the user to submit the form , or cancel. The modal presentation hints that you really need to fill this screen.
For editing - push. but modal could work as well (and you could reuse the same VC).
Reasons for push :
you get a hierarchy of vc's , going back and forward while drilling down.
(you should implement) auto saving when going back (just like other iOS apps)
For adding a new entity to the core data table, on tapping the + button (I assume its a right bar bar button item on the navigation bar), use the modal segue.
The view for adding a new row for the enity has to be presented modally and once the save is completed, dismiss the modal view and reload the table view to display the newly added item.
Also for displaying the details of an entity row, use the push segue. A user expects a push action when he selects a table cell and it is the ideal way to do that.
I hope this quick summary will help you :
When you want to show a detail view of a summary view, use a navigation controller and Push Segues. If the "parent" view doesn't really relate as far as data is concerned to the "child" view, then use a modal. A good example for a modal view would be any entry view. This view doesn't really have any relationship as far as data is concerned to the "parent" view., the entry screen will just take data dat from user & will save & can go away & giving control back to parent

Resources