Unable to segue using an accessory indicator - ios

I have a segue with three transitions (see code below). The first from a button. That works perfectly. The second by tapping on the cell in a table view. That one works perfectly too. The third is an accessory button in the tableview cell. This one opens the proper view controller, but does not pass a reference to the patient as I have coded. I have verified this by an NSLog statement in the viewDidLoad of the new view controller and it shows patient as being null.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"addPatient"]) {
// to add a new patient (selection segue)
UINavigationController *navigationController = segue.destinationViewController;
RXAddPatientViewController *addPatientViewController = (RXAddPatientViewController*)navigationController.topViewController;
Patient *addPatient = [NSEntityDescription insertNewObjectForEntityForName:#"Patient" inManagedObjectContext:[self managedObjectContext]];
addPatientViewController.addPatient = addPatient;
}
if ([[segue identifier] isEqualToString:#"toPrescriptions"]) {
// to view prescriptions for the selected patient (selection segue)
RXPrescriptionsViewController *prescriptionViewController = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Patient *selectedPatient = (Patient*) [self.fetchedResultsController objectAtIndexPath:indexPath];
prescriptionViewController.selectedPatient = selectedPatient;
NSLog(#"Selected Patient is %#", selectedPatient.patientFirstName);
}
if ([[segue identifier] isEqualToString:#"editPatient"]) {
// to edit the selected patient (accessory action)
UINavigationController *navigationController = segue.destinationViewController;
RXEditPatientViewController *editPatientViewController = (RXEditPatientViewController*)navigationController.topViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Patient *editPatient = (Patient*) [self.fetchedResultsController objectAtIndexPath:indexPath];
// passing a reference to the editPatientViewController
editPatientViewController.editPatient = editPatient;
NSLog(#"Selected patient is %#", editPatient.patientFirstName);
}
}

The indexPathForSelectedRow will be null when you click on the accessory button because you're not selecting the row. However, the sender argument in prepareForSegue:sender: will be the cell that the accessory button is contained in. So, you should use the following method to get the indexPath (notice that I changed the typing of the argument to UITableViewCell from id):
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender {
if ([[segue identifier] isEqualToString:#"editPatient"]) {
// to edit the selected patient (accessory action)
UINavigationController *navigationController = segue.destinationViewController;
RXEditPatientViewController *editPatientViewController = (RXEditPatientViewController*)navigationController.topViewController;
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
Patient *editPatient = (Patient*) [self.fetchedResultsController objectAtIndexPath:indexPath];
// passing a reference to the editPatientViewController
editPatientViewController.editPatient = editPatient;
NSLog(#"Selected patient is %#", editPatient.patientFirstName);
}

Related

NSMutableArray prepareForSegue

I have a small problem with an app where a debug I made there must be a conflict in a prepareForSegue: Let me explain
I have a UITableView with an array of data written by me.
Then I have another array with data of a xcdatamodel that the user must edit and save.
if I run with this screen prepareForSegue me when I call a cell crashes.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"DetailSchedule"]) {
NSManagedObjectModel *selectedSche = [schedule objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
DetailMainHallTableViewController *destViewController = segue.destinationViewController;
DetailMainHallTableViewController *dc = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
dc.mainhall = self.myArraySchedule1[indexPath.row];
destViewController.sche = selectedSche;
}
}
but if I run with this screen of the app runs prepareForSegue okay but I did not save the entered data
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"DetailSchedule"]) {
//NSManagedObjectModel *selectedSche = [schedule objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
DetailMainHallTableViewController *destViewController = segue.destinationViewController;
DetailMainHallTableViewController *dc = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
dc.mainhall = self.myArraySchedule1[indexPath.row];
//destViewController.sche = selectedSche;
}
}
It is strange, however, that if we take off // and I run again the data I had entered before appearing saved me and I can edit them or add them.
Can these two lines to understand the problem?
If you are using a UITableView you should move the NSManagedObjectModel code to the tableView:didSelectRowAtIndexPath method and then send that object as the sender for your segue.

segue not passing data over

im still having problems with my segue
i have at the moment
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Check that a new transition has been requested to the DetailViewController and prepares for it
if ([segue.identifier isEqualToString:#"WeddingDetail"]){
// Capture the object (e.g. exam) the user has selected from the list
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
// Set destination view controller to DetailViewController to avoid the NavigationViewController in the middle (if you have it embedded into a navigation controller, if not ignore that part)
UINavigationController *nav = [segue destinationViewController];
DMKWeddingDetailViewController *detailViewController = (DMKWeddingDetailViewController *) nav.nextResponder;
detailViewController.exam = object;
NSLog(#"object details: %#",object);
}
}
i was hoping that exam would hold the data but it isnt the data is held in object. but not sure how to pass object over to detail view controller.
in my detailview controller i have a list if textlabels
self.name.text = [self.exam objectForKey:#"name"];
but they are not populating
in case that the destination ViewController is a UINavigationController, I think the problem is in
DMKWeddingDetailViewController *detailViewController = (DMKWeddingDetailViewController *) nav.nextResponder;
you should use this instead
DMKWeddingDetailViewController *detailViewController = (DMKWeddingDetailViewController *) [nav topViewController];
and if the destination is your target ViewController you should use
DMKWeddingDetailViewController *detailViewController = (DMKWeddingDetailViewController *) [segue destinationViewController];

how to push subtitle from uitableview to detail view controller

i have a project which is basically a uitableview populated with a mutable array and has a search bar
now the uitableview has title and subtitle which are populated with different mutable arrays ..
when you tap on a cell (from the view or from the search view) it pushes the title to a new view controller.
i need the subtitle to be pushed also to the new view controller
i tried every thing i know but i had no success
i have attached the project so you can see how its coded and possibly tell me how to fix it
http://ezzati.net/KuwaitFood.zip
please help
Please replace the function below.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowDetail"]) {
NSString *object = nil;
NSIndexPath *indexPath = nil;
if (self.searchDisplayController.isActive) {
indexPath = [[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
object = self.results[indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
object = self.objects[indexPath.row];
}
[[segue destinationViewController] setDetailLabelContents:object];
[[segue destinationViewController] setNumberdetailLabelContents:[sender detailTextLabel].text];
}
}
You have to assign a identifier to the segue (the arrow from your table view controller to the new controller in your storyboard) and then use this function to grab the destination view controller and set it's property with the data you want to send:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"identifier"])
{
SecondViewController *vc = [segue destinationViewController];
// get the selected item index
NSInteger index = [self.tableView indexPathForCell:sender].row;
// set the data property for the second view (pass data)
vc.theProperty = [self.data objectAtIndex:index];
}
Now you can find theProperty on your SecondViewController filled with data from the table view controller.
EDIT: I've reviewed your code. for your case do this:
in -tableView: didSelectRowAtIndexPath: change self to indexPath like this:
[self performSegueWithIdentifier:#"ShowDetail" sender:indexPath];
and in prepareForSegue do this:
DetailViewController *vc = [segue destinationViewController];
// get the selected item index
NSInteger index = ((NSIndexPath *)sender).row;
// index of searched object in self.objects
NSInteger trueIndex = [self.objects indexOfObject:[self.results objectAtIndex:index]];
// set the data property for the second view (pass data)
vc.detailLabelContents = [self.objects objectAtIndex:trueIndex];
vc.numberdetailLabelContents = [self.numbers objectAtIndex:trueIndex];
the problem was because you were using index of self.results and applying it to self.objects which was 0, so you were always getting the first object "You Tube"
change the last line of
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
as below
[[segue destinationViewController] setDetailLabelContents:[sender detailTextLabel].text];

Segue is blank (black) , can't view the data

(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"DetailPlacePage"]) {
NSIndexPath *indexPath = [self.Allplace indexPathForSelectedRow];
NomadPlaceDetailViewController *placedetailcontroller = segue.destinationViewController;
placedetailcontroller.myDictionary = [self.placeArray objectAtIndex:indexPath.row];
}
}
This is the code I use to prepare segue for UIviewcontroller.
Can't see the next view controller , screen gets blank and just navigation bar is shown .
Try casting your NomadPlaceDetailViewController:
NomadPlaceDetailViewController *placedetailcontroller = (NomadPlaceDetailViewController *)segue.destinationViewController;

Show EditViewController while editing, show DetailViewController when not editing

I do have a UITableViewController which can be edited.
I do have 2 Segues in my Storyboard, coming from the tableView going to an EditViewController (via modal) and the second one via Push to the DetailViewController.
I want, when the tableview isEditing, that the segue to editviewcontroller is executed, and when is not editing, than to DetailViewController.
I tried different ways, with the prepareforsegue method. then several tries with the didselectrowatindexpath method, where I stand now.
Now, when the tableView is in editing and I touch one cell, the detailviewcontroller pushes and on top of this the editviewcontroller comes in as modal view controller.
Here is my code.
Can you please give me hints:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"didSelectRowAtIndexPath %i", indexPath.row);
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (tableView.isEditing == FALSE) {
NSLog(#"isediting");
[self performSegueWithIdentifier:#"showDetail" sender:cell];
} else {
[self performSegueWithIdentifier:#"showEditPerson" sender:cell];
}
}
and here is what i have tried with the prepare for segue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDictionary *theChild = [myAppDelegate.myPersonsArray objectAtIndex:indexPath.row];
[[segue destinationViewController] setDetailItem:theChild];
}
if ([[segue identifier] isEqualToString:#"showEditPerson"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDictionary *theChild = [myAppDelegate.myPersonsArray objectAtIndex:indexPath.row];
[[segue destinationViewController] setDetailItem:theChild];
}
}
From my comment above:
It seems you have the prototype cell hooked up to a segue in addition to the delegate method. Fix the segue connections and you should be all set.

Resources