How to use prepareForSegue for multiple Tableview Controllers - ios

I am having three table views A,B,C . The flow of view is like this:
table A ,click on a row --->table B, click on a row----Table C
I am using the following method to send the data from master view A to detail view B.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showDetailFinal"]) {
NSIndexPath *indexPath = [self->ListView_A indexPathForSelectedRow];
DetailView_B *destViewController = segue.destinationViewController;
destViewController.detailStatements = [dataArray objectAtIndex:indexPath.row];
}
}
and I have the same method again to send the data from detailView_B to moreDetailView_C
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showDetailFinal"]) {
NSIndexPath *indexPath = [self->DetailView_B indexPathForSelectedRow];
More_DetailView_C *destViewController = segue.destinationViewController;
destViewController.detailStatements = [dataArray objectAtIndex:indexPath.row];
}
}
I am using the modal segues instead of push. The problem is that,when I go to table B from table A, I see data in table B.
But when I go back to table B from table C, I do not see any data in table B.
The view did load method in table B is :
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"the name is %#",detailStatements.name);
// here when I come from table A, name is not null
//but when I come from table C,name is null.
}
Is it possible to use common segue identifier for all 3 table views?
Kindly tell me the possible way to handle this?

From your comments you need an unwind segue and not the normal segue you are using for going backwards.
This might help Storyboard: Control-drag from a bar button to the Exit item
Or add an action to the buttons to simply dismiss the view controller.

Related

How to populate a cell in UITableView with an edited content?

Maybe I didn't ask it properly, because I'm not really sure how, so I will explain myself:
I have a simple notes app with two VC's:
table view controller - to list all the notes.
view controller - to create new notes.
In the table vc I have an unwindToList method that create an instance of the create notes page to get the note object that will be passed and if its not nil I will add it to the notes array and reload the data;
- (IBAction) unwindToList: (UIStoryboardSegue *) segue
{
NMCreateNotesViewController *source = [segue sourceViewController];
NMNote *note = source.note;
if (note != nil) {
[self.notes addObject:note];
[self.tableView reloadData];
}
}
and an prepareForSegue method that will identify if this segue is a note segue (this is the segue I JUST want to preform editing in the create note page and when a user taps the save button, to save it in the same cell that the segue came from...):
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender
{
if ([[segue identifier] isEqualToString:#"noteSegue"]) {
NMCreateNotesViewController *destination = [segue destinationViewController];
NSInteger indx = [self.tableView indexPathForCell:sender].row;
NMNote *note = self.notes[indx];
destination.passedInString = note.content;
}
}
In the view controller that create the notes I have a prepareForSegue method that looks like this:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.saveButton) return;
if (self.textField.text.length > 0) {
self.note = [[NMNote alloc] init];
self.note.content = self.textField.text;
}
}
Now, my problem is that when i'm editing a note from the noteSegue i'm creating a new note with the edited content instead of populating the same cell of the segueNote with the new edited content...
this is how the problem looks like:
Please help me to figure this out :/
Appreciate it!
I don't quite understand your explanation.
You have a table view controller that is a master view controller and a note view controller that is a detail view controller.
The usual way to handle communications back from a detail view controller to it's master is to set up a delegate.
Define a delegate property in the detail (notes) VC that will point back to the master.
In the master's prepareForSegue, set destination.delegate = self.
Then, define a protocol for the delegate that the detail view controller will use to tell the master that the contents of a list item has changed.
You will probably need to tell the detail view controller the indexPath of the item that's being edited in the master-to-detail segue, and then before unwinding to the master, invoke a delegate method that gives the edited item to the master, along with it's indexPath so the master can save the changes and redisplay that cell.

Can i use same detailedViewController with different segue variables

this is my problem. I can perfectly show a detailedViewController came from a table view, but i got 2 more tableview that i want to make it seem in detailed.When i gave it same variables - since every things that came same - it still doesn't go to the next view controller.I checked segue identifier aswell and i really don't want to make a new detailed controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
// Get destination view
detailViewController *destViewController = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSInteger tagIndex = [(UIButton *)sender tag];
destViewController.recipeName =[[self.content objectAtIndex:indexPath.row] valueForKey:#"recipeName"];
destViewController.recipeDetail =[[self.content objectAtIndex:indexPath.row] valueForKey:#"recipeDetail"];
this is how i pass the data.They are exactly same with a small difference. The content of the content array.
You can go to the same view controller from multiple other controllers. You may be able to do this by simply creating the segue from the appropriate cells in each table. The way I've done it is to create a segue from each table view controller to the "detail" view controller, and then perform the segue in code when the appropriate cell is selected. Keep in mind that when creating segues in IB, there is a difference between creating them from table view cells and from the view controllers themselves.

UITextLabel only updates on second selection of cell in Navigation Controller

I have a project with a TabBar Controller with a Navigation Controller and other View Controllers on it.
In my Navigation controller i have a sequence of 3 view controller and the last of them, DetailViewController, is being presented using a modal transition. I'm parsing strings between my 2nd and 3rd view controllers, but my labels displaying the information on the 3rd one only display after selecting a cell twice, and the information that shows is the one corresponding to the first selection of the cell.
Heres my 2nd view controller (ViewController) method prepareForSegue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
titulo=[nomeEvent objectAtIndex:indexPath.row];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"nextView"]) {
DetailEventViewController *myVC = [segue destinationViewController];
myVC.tituloEvento = titulo;
}
}
The property tituloEvento is declared in the second view controller's .h and in viewwillappear I set my labels text.
This might be happening because prepareForSegue is getting called before didSelectRowAtIndexPath:. Here is what I would do:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"nextView"]) {
DetailEventViewController *myVC = [segue destinationViewController];
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
myVC.tituloEvento = [nomeEvent objectAtIndex:selectedIndexPath.row];
}
}

UITableview Push Detail

in my application i have a table view push to a view controller but i want to have the table view push to a new table view how would i approach this?
this is how i am performing the segue now
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowDetails"]) {
DetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
int row = [myIndexPath row];
detailViewController.DetailModal = #[_Title[row], _Description[row], _Images[row]];
}
}
this work when pushing to a view controller but not a table view controller how do i make it go to a new table view?
Apparently you're doing the Geeky Lemon Tutorial found here. When you create the second tableView make sure you set the custom identifier to "DetailViewController".
Hope it helps.

Custom segue in storyboard

I have a Master-Detail application.When the user selects a row in the MasterTable, I want to open another Table (not the DetailViewController). It should paper besides the MasterViewController, similar to the Facebook app on iPad.
I have a Custom segue from the MasterViewController to this table (DocumentViewController of type UITableViewController). This DocumentViewController is 'Embedded' in a navigation controller.
In short, the items on my storyboard look like this:
navigationController->MasterViewController->navigationController->DocumentViewController.
The segue is from MasterViewController to navigationController.
Do I have to perform the segue in "didSelectRowAtIndexPath"? Or do I have to use "performSegueWithIdentifier" in didSelectRowAtIndexPath?
If I understand your question correctly, the segue should be from MasterViewController to DocumentViewController. Make sure to give it a unique identifier, and then reference what you want to do in the prepareForSegue in your MasterViewController.
Example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showList"]) {
// gets indexPath for the currently selected row in my UITableView
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
// pull back some useful data from Core Data according to what was selected in my UITableView.
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
// listDataItem is a property of the controller (in this case a UITableViewController)
[[segue destinationViewController] setListDataItem:object];
}
}

Resources