I have a view controller with two collectionviews being displayed. I now want to segue from both collection views to different destination controllers. I have this code below but no luck.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
JSTRCollectionViewCell *selectedCell = (JSTRCollectionViewCell *)sender;
JSTRBrowserViewController *targetVC = (JSTRBrowserViewController *) [segue destinationViewController];
targetVC.ndesc = selectedCell.description.text;
targetVC.ntitle = selectedCell.title.text;
targetVC.nimage = selectedCell.image.image;
targetVC.ncat = selectedCell.category.text;
targetVC.ndate = selectedCell.date.text;
SliderCollectionViewCell *selectedCell1 = (SliderCollectionViewCell *)sender;
SliderBrowserViewController *targetVC1 = (SliderBrowserViewController *) [segue destinationViewController];
targetVC1.sldesc = selectedCell1.description.text;
targetVC1.sldate = selectedCell1.date.text;
targetVC1.slcat = selectedCell1.category.text;
targetVC1.sltitle = selectedCell1.title.text;
targetVC1.slimage = selectedCell1.image.image;
}
I added this to the collectionview cells
[self performSegueWithIdentifier:#"String" sender:indexPath];
and added an if statement to the segue and problem solved.
Related
I have UITableViewController in UINavigationController which I want to show in popover view and populate the fields in viewController.
Storyboard looks like the pic bellow. Left VC calls editIdeaSeque on navigation button press.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"editIdeaSegue"]) {
UINavigationController *nvc = segue.destinationViewController;
if (!isIpad) {
EditIdeaTVC* edi = (EditIdeaTVC*)[nvc.viewControllers objectAtIndex:0];
if (edi != nil)
edi.editIdea = sender;
}
else {
UINavigationController* nc = (UINavigationController*)[[segue.destinationViewController popoverPresentationController] presentedViewController];
EditIdeaTVC* edi = (EditIdeaTVC*)[nc.viewControllers objectAtIndex:0];
if (edi != nil)
edi.editIdea = sender;
}
}
}
My prepare for segue works for iphone which is similar storyboard layout. Ipad part crashes cause edi is some garbage pointer.
-[EditIssueTVC setEditIdea:]: unrecognized selector sent to instance 0x157d6c7a0
I was expecting that presentedViewController will be UINavigationController ...
UINavigationController* nc = (UINavigationController*)[[segue.destinationViewController popoverPresentationController] presentedViewController];
You're over-thinking this. The navigation controller is the destination view controller. The situation is identical to your earlier code:
UINavigationController *nvc = segue.destinationViewController;
There is nothing about the popover situation that would change what view controller is at the far end of the destination arrow.
I have a HistoryTableViewController with 2 sections hooked up to a detail view controller. I want to be able to display the text and subtitle text from the HistoryTableViewController in the details view, but I want to make sure that it detects the right section. I know somewhere along the line I'll have this:
if (section == 0)
{
labelCellNum.text = Whatever was the main text in the selected table view cell
labelDueDate.text = Whatever was in the subtitle text in the selected table view cell
}
else if (section == 1)
{
labelCellNum.text = Whatever was the main text in the selected table view cell
labelDueDate.text = Whatever was in the subtitle text in the selected table view cell
}
Since I'm displaying this on a details view controller, I don't know how to get the section from the TableViewController I'm getting displayed from. I pretty much need to know how to get the section, and the main text + subtitle text.
OK Perhaps I wasn't clear enough. This code is going in my ViewDidLoad of my DetailsViewController. NOT in my tableviewcontroller.
Here is the HistoryTableviewController code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// HistoryDetailsViewController *HDV = [self.storyboard instantiateViewControllerWithIdentifier:#"HistoryDetails"];
// [self.navigationController pushViewController:HDV animated:YES];
NSLog(#"index path section--%i",[indexPath section]);
[self performSegueWithIdentifier:#"Push" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"Push"]) {
HistoryDetailsViewController *detailVC=(HistoryDetailsViewController*)segue.destinationViewController;
detailVC.labelCellNum.text = #"Cell test";
detailVC.labelDueDate.text = #"Date Test";
}
}
And below is the code of my detailsViewController:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
labelCellNum.text = #"This isnt even getting called.";
}
check this , its simple
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"index path section--%i",[indexPath section]);
[self performSegueWithIdentifier:#"Push" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"Push"]) {
DetailViewController *detailVC=(DetailViewController*)segue.destinationViewController;
detailVC.title=#"value to pass";
}
}
There you go!
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Selected item infos
//
int section = indexPath.section;
int row = indexPath.row;
NSLog(#"section: %d / row: %d", section, row);
}
I assume you have a segue from the HistoryVC to the details VC. So you can pass the data across to the details VC in the prepareForSegue method in the HistoryVC. In the code fragment below, I am passing the cell title over into the new VC to set it's title. You could set other properties in the target VC, to suit your needs.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// get selected row's cell
//
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
// set desitination VC title
//
UIViewController *vc = [segue destinationViewController];
vc.navigationItem.title = cell.textLabel.text;
}
So In my project I have one HistoryTableViewController which contains 2 sections. One section is the PeopleYouOwe and the other section is a group of PeopleWhoOweYou. How would I be able to hook up this one tableviewcontroller and link it to a detail view that will display different data according to my sections?
Try below approach using prepareForSegue -
creat tableView outlet named myTableView
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
NSIndexPath *path = [self.myTableView indexPathForSelectedRow];
if (path.section == 0)
{
if ([segue.identifier isEqualToString:#"PeopleYouOwe"])
{
PeopleYouOwe *peopleYouOweVC = segue.destinationViewController;
// you can get data of cell as array[path.row]
}
}
else if (path.section == 1)
{
if ([segue.identifier isEqualToString:#"PeopleWhoOweYou"])
{
PeopleWhoOweYou *peopleWhoOweYou = segue.destinationViewController;
// you can get data of cell as array[path.row]
}
}
}
You use the delegate methods provided with UITableViews like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = indexPath.row;
NSInteger section = indexPath.section;
if(section == 0) // example
{
// do whatever
}
else if (section == 1)
{
// do something else
}
}
If this is not what you're looking for you will need to provide more details and ideally some code to show what you already have. Your question is vague.
If you prefer prepareForSegue, you can use something like:
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
then take action based on section and row.
I am trying to pass existing attribute values for a managed object, to a modal segue view for editing. From "editing mode," I'm going from a UITableViewController subclass (BreadRackTableViewController), using a modal segue to a UIViewController subclass (EditBreadRackTableViewController) by means of an embedded UINavigationController. Normal TableViewCell selection pushes to another UITableViewController subclass, but that's not important for the time being.
I've added properties to my editing view controller header (and #synthesized), to function as getters:
#property (nonatomic, strong) id existingBreadRackDataName;
#property (nonatomic, strong) id existingBreadRackDataDetails;
What I'm trying to do is populate the edit view's fields with the existing Core Data Entity Attributes, which would have been previously set by the user (in theory). I can get the data to show up if I pass along a literal string value, but I need to retrieve and pass the existing data.
Here's what I have currently:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"editBreadRackSegue"])
{
EditBreadRackTableViewController *ebrtvc = (EditBreadRackTableViewController *)[segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:indexPath];
// [ebrtvc setBreadRackClass:breadRackClass];
ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:#"breadRackDataDetails"];
}
}
These last two lines of code...
ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:#"breadRackDataDetails"];
...they both return nil. If I specify a literal string though, that literal string shows up where it needs to.
All of that said, my question is:
How do I retrieve the attribute's key value for that selected row (or just the textLabel and detailTextLabels), and assign it to my setter?
Solution:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"editBreadRackSegue"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:indexPath];
[segue.destinationViewController setExistingBreadRackDataName:[breadRackClass valueForKey:#"breadRackDataName"]];
[segue.destinationViewController setExistingBreadRackDataDetails:[breadRackClass valueForKey:#"breadRackDataDetails"]];
}
// code for other segues here...
}
I ended up removing the ebrtv variable, and cleaned up the code a little bit. What worked though, was using the indexPathForCell:sender instead of indexPathForSelectedRow when declaring my indexPath for breadRackClass.
I think the indexPathForSelectedRow could be a problem.
You can just pass the indexPath from the didSelectRowAtIndexPath to the prepareForSegue , like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"editBreadRackSegue" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"editBreadRackSegue"])
{
NSIndexPath *clickedIndexPath = (NSIndexPath*)sender;
if( [clickedIndexPath isKindOfClass:[NSIndexPath class]] ){
BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:clickedIndexPath];
EditBreadRackTableViewController *ebrtvc = (EditBreadRackTableViewController *)[segue destinationViewController];
ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:#"breadRackDataDetails"];
}
}
}
Let me know if it works for you.
If I have many Annotation in my map view… how to understand what CallOut I've pressed? In tableview for example we have "didSelectRowAtIndexPath"... but here?
-(void)button:(id)sender {
[self performSegueWithIdentifier: #"dettaglio"sender: self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if (![[segue identifier] isEqualToString:#"dettaglio"]&&![[segue identifier]
isEqualToString:#"cerca"])
{
[(paginaOpzioni*)segue.destinationViewController setDelegate:self];
}
if ([[segue identifier] isEqualToString:#"dettaglio"])
{
paginaDettaglio *ibcVC = [segue destinationViewController];
Annotazione *annotazioneDaPassare=[mapAnnotations objectAtIndex:2];
ibcVC.immagine = annotazioneDaPassare.immagine;
ibcVC.navigationItem.title = annotazioneDaPassare.title;
}
}
I need to change the value (2) of [mapAnnotations objectAtIndex:2]; because I want to send to the DestinationViewController the right image and text! Thanks
Your MKMapViewDelegate will tell you what was selected when it calls -mapView:didSelectAnnotationView:
http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/occ/intfm/MKMapViewDelegate/mapView:didSelectAnnotationView:
Unless of course you were referring to the accessory being tapped. That would be -mapView:annotationView:calloutAccessoryTapped:
http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/occ/intfm/MKMapViewDelegate/mapView:annotationView:calloutAccessoryControlTapped: