I have created gallery view in my app. In that add segment controller , first index is table view and second index is collection view. First index (table view) is proper work in my app, But second index (collection view) show array stored pictures but I want to click on collection view cell and move to another view and pass my UID to another view controller. I tried but could not pass my id. Please help. Thank you
Segue method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Tableview"]) {
NSIndexPath *indexPath = [self.table_view indexPathForSelectedRow];
Detail_view *destViewController = segue.destinationViewController;
destViewController.uid_value = [id_ary objectAtIndex:indexPath.row];
}
else if ([segue.identifier isEqualToString:#"Collectionview"])
{
NSArray *indexPaths = [self.collection_view indexPathsForSelectedItems];
collection *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
destViewController.uid_value = [id_ary[indexPath.section] objectAtIndex:indexPath.row];
[self.collection_view deselectItemAtIndexPath:indexPath animated:NO];
}
}
I think you need to improve your code rather then doing split arrays better to user NSObject class manage the array with Object Class for your solution use below code
else if ([segue.identifier isEqualToString:#"Collectionview"])
{
NSArray *indexPaths = [self.collection_view indexPathsForSelectedItems];
collection *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [indexPaths firstObject]; // you can also use firstObject rather then ObjectAtIndex:0
destViewController.uid_value = [id_ary objectAtIndex:indexPath.row];
[self.collection_view deselectItemAtIndexPath:indexPath animated:NO];
}
try this-
take a global indexPath for ex. NSIndexPath *selectedIndexPath; Now add this line in didSelectItemAtIndexPath i.e.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
selectedIndexPath=indexPath;
[self performSegueWithIdentifier:#"Collectionview" sender:self];
}
now in your prepareForSegue method-
else if ([segue.identifier isEqualToString:#"Collectionview"])
{
collection *destViewController = segue.destinationViewController;
destViewController.uid_value = [id_ary objectAtIndex:selectedIndexPath.row];
// or destViewController.uid_value = [id_ary[selectedIndexPath.section] objectAtIndex:selectedIndexPath.row];
[self.collection_view deselectItemAtIndexPath:selectedIndexPath animated:NO];
}
hope this will solve your issue..:)
Related
I will try to pass data between two controller using segue method but it's give me error like
used of undeclared identifier "indexPath"
Check My Code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
NSIndexPath *index = (NSIndexPath *)sender;
NameViewController *destViewController = [segue destinationViewController];
destViewController.receiptName = [self.nameListArray objectAtIndex:indexPath.row]; // here display the error.
}
}
According to the thrown exception the sender is obviously the table view cell rather than the index path.
Then you have to write
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell: (UITableViewCell*)sender];
NameViewController *destViewController = [segue destinationViewController];
destViewController.receiptName = self.nameListArray[indexPath.row];
}
}
Here..
destViewController.receiptName = [self.nameListArray objectAtIndex:indexPath.row]; // here display the error.
You are using indexpath and you declare index so you need to change var name like...
NSIndexPath *indexPath = (NSIndexPath *)sender;
and then use it as
destViewController.receiptName = [self.nameListArray objectAtIndex:indexPath.row];
Please make sure that sender is kind of NSIndexPath or not.
Rename the variable name as follows,
NSIndexPath *indexPath = (NSIndexPath *)sender;
instead of
NSIndexPath *index = (NSIndexPath *)sender;
This would have solved the undeclared identifier issue.
Now coming to your second set of problem,
As you mentioned in our conversation that you are not using the didSelect as per the guide you mentioned, Please read the documentation little in depth because they have mentioned to use indexPathForSelectedRow as below,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NameViewController *destViewController = [segue destinationViewController];
destViewController.receiptName = self.nameListArray[indexPath.row];
}
}
FYI: #vadian answer is also a good way to achieve this.
I am newbie to iOS and learning things in iOS. I am making a demo of UITableView and Segue. I have successfully implemented table-view and now I want to pass the value to another View Controller using segue. I have tried as below, but my problem is I am getting same record every time on detail button clicked from UITableView. I don't have any idea what is wrong in my code.
Can anybody help me to figure it out?
I am posting my code here:
code
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
//NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
// NSString *name = [dict valueForKey:#"name"];
[self performSegueWithIdentifier:#"detail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//NSLog(#"selected index path==%#",indexPath);
DetailView *destViewController = segue.destinationViewController;
destViewController.details = [myArray objectAtIndex:indexPath.row];
}
}
Change your code as below, you don't need to send self as the sender. Send indexPath
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:#"detail" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detail"]) {
NSIndexPath *indexPath = sender;
DetailView *destViewController = segue.destinationViewController;
destViewController.details = [myArray objectAtIndex:indexPath.row];
}
}
I have searched answers for this question but I didn't find any relevant answers. In my table view when I touch one cell, a Detail View is pushed. Source code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"trajetDetail" sender:tableView];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"trajetDetail"]) {
RechercheDetailViewController *detailViewController = segue.destinationViewController;
if (sender == self.searchDisplayController.searchResultsTableView)
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *destinationTitle = [[self.trajets objectAtIndex:[indexPath row]] name];
[detailViewController setTitle:destinationTitle];
}
}
}
The question is that when users make a search with the UISearchBar (and UISearchDisplayController) that they could also touch the cell and be pushed to the detail view.
Sorry for my english and if it's not clear, just tell me I'll try to make it clearer.
Kindest regards
what wrong i see in your code is in your prepareforsegue method, you are selecting indexpath of standard tableView not the search tableview, this below line needs to be changed
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
the prepareForSegue would become something like this after the change
you should use this code instead
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"trajetDetail"]) {
RechercheDetailViewController *detailViewController = segue.destinationViewController;
if (sender == self.searchDisplayController.searchResultsTableView)
{
NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
NSString *destinationTitle = [[self.trajets objectAtIndex:[indexPath row]] name];
[detailViewController setTitle:destinationTitle];
}
}
}
in my app I need to run 2 "Storyboard Follows" in practice, when the user selects the cell is pushed into a specific view controller based on a boolean value.
The Tableview contains a list of exam ... I wish that:
when the user selects the exam Not Completed is pushed into the ViewController 1
when the user selects the exam Complete is pushed into the ViewController 2
Until now I always used
- (Void) prepareForSegue: (UIStoryboardSegue *) sender follows: (id) sender
but of course when I connect two follows from the cell, the app crashes
I show you the code
P.S. in the storyboard I connected the cell to two different viewcontroller .. If this is wrong you can tell me how to fix this?
Thanks for everything and sorry if the question is not really a problem :)
I'm used Parse.com
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier]isEqualToString:#"DettaglioOggetto"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject *rowObject = [self.objects objectAtIndex:indexPath.row];
if([[rowObject objectForKey:FF_ESAMI_STATUS] boolValue])
{
//DATO CONVALIDATO
PFObject *object = [self.objects objectAtIndex:indexPath.row];
FFDettagliEsami *FFDestinationDetails = [segue destinationViewController];
FFDestinationDetails.FFObjectForDetails = object;
} else if ([[segue identifier] isEqualToString:#"Dettaglio"]){
// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
FFSchedaEsameConvalidato *FFDestinationDetails = [segue destinationViewController];
FFDestinationDetails.FFObjectForDetails1 = object;
}
} }
Solved ... I connected follows directly from viewcontroller containing the TableView with CTRL + drag the view controller target, then
- (Void) prepareForSegue: (UIStoryboardSegue *) sender follows: (id) sender
{
if ([[segue identifier] isEqualToString: # "SegueDaConvalidare"]) {
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
PFObject * object = [self.objects objectAtIndex: indexPath.row];
FFDettagliEsami * = FFDestinationDetails [more destinationViewController];
FFDestinationDetails.FFObjectForDetails = object;}
if ([[segue identifier] isEqualToString: # "SegueConvalidato"]) {
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
PFObject * object = [self.objects objectAtIndex: indexPath.row];
FFSchedaEsameConvalidato * = FFDestinationDetails [more destinationViewController];
FFDestinationDetails.FFObjectForDetails1 = object;}
}
Then I implemented the method didSelectedRowAtIndexPath and now everything is working correctly.
I'm having big problems learning how to pass data from the cell accessory button to the detail view controller with segues. If I tap the cell the data is passed to the detail view controller in a NSString *selectedItem; and the label on the other view updates with the word that was in the cell -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"toDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}
And i connect a segue on the storyboard from the cell to the detail view with a name "toDeatil" It works when i click the cell it goes to the detail screen, but if i put
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"toDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}
Almost exactly the same but from the accessorybutton and I connect a segue on the storyboard from the cell accessorybutton to the detail view with a name "toDetail" when I click the cell it goes to the detail screen, but the information is not there, the label dosent get filled out or just uses the first cell detail all the time.
I've tried heaps of combinations. I've tried making my own accessory button, a UIButton in a custom cell but it just uses the first cells details all the time.
The goal is to get didSelectRowAtIndexPath going to one view and accessoryButtonTappedForRowWithIndexPath going to a different view but I need to tell accessoryButtonTappedForRowWithIndexPath what cell was pressed.
Hope this makes sence.
Thank you
You can pass the indexPath as sender to the performSegueWithIdentifier call, like this:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"toDetail" sender:indexPath];//IndexPath as sender
}
And in your prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"]) {
//Detect sender class and act accordingly
NSIndexPath *indexPath = [sender isKindOfClass:[NSIndexPath class]] ? (NSIndexPath*)sender : [self.tableView indexPathForSelectedRow];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}
You don't need to implement accessoryButtonTappedForRowWithIndexPath.
When prepareForSegue is fired from an accessory view, the sender:(id)sender parameter is set to the UITableViewCell which contains the accessory view. You can then ask the table view for the index of that cell (not the selected index).
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"] && [sender isKindOfClass:[UITableViewCell class]]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}
That is because [self.tableView indexPathForSelectedRow] will return a NSIndexPath only if the ROW is tapped, not when the accessory button is tapped.
In accessoryButtonTappedForRowWithIndexPath, you will need to pass indexPath.row to the prepareForSegue method manually. Try this:
NSInteger selectedRow;
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
selectedRow = indexPath.row;
[self performSegueWithIdentifier:#"toDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toDetail"])
{
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:selectedRow] name];
}
}
What about, if you drive state of what is selected on application delegate! Answer of Raul Juarez do the work, but the target of sender parameter is other!
I have an app that use UITableView, and i need to know what item was touched on accessory button in above screen!
To manage this scenery, i have one property on application delegate, that i update when i touch table view accessory button; so in the next screen, i can retrieve the item touched reading the property of application delegate.
In general, i use de application delegate to code model application (dividing necessary class), so views instances only interact with this model to update necessary states! what i do is focus view work independent of model work!
Maybe some like this:
// Property on Application Delegate
#propety (atomic,readwrite) NSIndexPath* indexTapped;
// Method on UITableViewDelegate instance
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UIMyAppDelegate* model = (UIMyAppDelegate*)[[UIApplication shared] delegate];
model.indexTapped = indexPath;
[self performSegueWithIdentifier:#"toDetail" sender:self];
}
// Method on another View Controller
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
UIMyAppDelegate* model = (UIMyAppDelegate*)[[UIApplication shared] delegate];
if ([segue.identifier isEqualToString:#"toDetail"]) {
NSIndexPath *indexTapped = model.indexSelected;
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}