NSMutableArray prepareForSegue - ios

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.

Related

How pass core data value from tableview to another view

I'm trying to pass data from one table view to another view. Both is the same core data. I just need to make another view to show the correct data according the selected table. Here is the code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetailTwo"]) {
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
[self.tableView deselectRowAtIndexPath:path animated:YES];
DetailViewController *moviewDetailVC = [segue destinationViewController];
NSManagedObject *movies=[self.moviesArray objectAtIndex:path.row];
// NSLog(#"%#", movies);
Movies *movieL =[NSKeyedUnarchiver unarchiveObjectWithData:[movies valueForKey:#"posterImage"]];
[moviewDetailVC.movieSelected setPosterImage: movieL];
}
}
But when I go to next screen the data is null
2016-08-11 23:12:02.020 OMBDSearch[20175:1682899] (null)
I'm new at core data so I need a little help. I've searched a lot for this and didn't solve my problem.
I was trying to do in a difficult way. This way solve my question:
DetailViewController *destination = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
destination.movieSelected = [self.moviesArray objectAtIndex:indexPath.row];

SWRevealViewController does not recognize sectors for destinationViewController

I am using John Lluch's SWRevealViewController which works great, except I cannot figure out how to set selectors in the destination view controller as I do in segues for other UIViewControllers.
Has anyone else encountered this problem?
Here is my code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Set the title of navigation bar by using the menu items
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
if ([segue.identifier isEqualToString:#"goalSegue"]) {
if ([segue.destinationViewController respondsToSelector:#selector(setTitleForViewController:)]) {
// use performSelector:withObject: to send without compiler checking
// (which is acceptable here because we used introspection to be sure this is okay)
NSString * titleText = #"Athlete Goals";
[segue.destinationViewController performSelector:#selector(setTitleForViewController:) withObject:titleText];
} else destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];
} else destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];
}
I have used this code in prepareForSegue in other view controllers. The selector exists in the destination TableViewController, but SWRevealViewController passes over if-statement without executing.
I think the problem lies in the fact that you are trying to run a selector method on a UINavigationController and not the actual TableViewController in which the selector resides. Notice that destViewController is a UINavigationController, which has your TableViewController inside it.
So actually, you can try the following code and see if it works (I've tried this in a sample project using SWRevealViewController and it works)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Set the title of navigation bar by using the menu items
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
YourTableViewController *tableController = (YourTableViewController *)[destViewController childViewControllers].firstObject;
if ([segue.identifier isEqualToString:#"goalSegue"]) {
if ([tableController respondsToSelector:#selector(setTitleForViewController:)]) {
NSString * titleText = #"Athlete Goals";
[tableController performSelector:#selector(setTitleForViewController:) withObject:titleText];
} else destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];
} else destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];
}
As far as I understand, this may not exactly be a problem with SWRevealViewController.
Hope this helps.

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;

Unable to segue using an accessory indicator

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);
}

iOS - UIStoryboardSegue detecting table cell and opening a viewController?

So I have a UITableView, and the cells are connected to a UIStoryboardSegue in order to open a new UIViewController. After clicking a cell prepareForSegue get's called and everything works fine.
QUESTION: In the follwoing method how can i know what is the indexPath of the selected cell in my TableView?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ObjectDetailViewController *mvc = [segue destinationViewController];
// PassingObject *obj = [self.array objectAtIndex: ? ];
mvc.passingObject = obj;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Assume self.view is the table view
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
DetailObject *detail = [self detailForIndexPath:path];
[segue.destinationViewController setDetail:detail];
}
Instead of [self.tableView indexPathForSelectedRow] (look at the DJPlayer answer) you can use [self.tableView indexPathForCell:sender], since sender is a selected cell in this case.

Resources