0

I need to launch a ViewController from my custom UICollectionViewCell but i cant access my navigationController there so I don't know how to launch it because I know only one way.

Only like this for example

LoginViewController *loginView = [[LoginViewController alloc]init];
[self.navigationController pushViewController:loginView animated:YES];

Any idea how can I do it ?

This is how I normally call cell click

- (void)collectionView:(PSCollectionView *)collectionView didSelectCell:(PSCollectionViewCell *)cell atIndex:(NSInteger)index
{
[NjoftimeManager setSelectedListing:[[NjoftimeManager getMainListings] objectAtIndex:index]];
ListingViewController *listingView = [[ListingViewController alloc]init];
[self.navigationController pushViewController:listingView animated:YES];
}

But what i want to do is call somethign different from here

- (void) profileTapped:(UIGestureRecognizer *)gesture{
//some code

NSLog(@"PROFILI %li",(long)gesture.view.tag);
}

I have given a tag to the footer view of the cell and want to do something different when that is clicked and I can only get the tap event here in Cell class.

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
Elgert
  • 470
  • 2
  • 5
  • 19
  • You need to your UINavigationController as a rootViewController in main window. then you can able to launch it. – ASHISHT Jul 22 '14 at 08:29
  • If you are using story board, you can bind the collection view cell and new controller (and select push). so that on tapping the collection view cell, it will push new view controller. Or else explicity call "performSegueWithIdentifier". Will it solve your problem? – Satyam Jul 22 '14 at 08:52
  • I dont use story board nor xib, only code – Elgert Jul 22 '14 at 08:53
  • So if you are using only code, define block on the cell. Assign them to cell and call them on desired action. Or you can write your custom extension of ViewController adding navigation controller. – Radim Halfar Jul 22 '14 at 08:54
  • @Elgert i edited the code please check it – Shankar BS Jul 22 '14 at 10:21

3 Answers3

2

if i understand your question completely, u want t launch a view controller from collection view cell, as a root view controller in that u can push and pop of other view controller

u can do like this,

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
   LoginViewController *loginView = [[LoginViewController alloc]init];
   UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:loginController]; //add this as a root view controller
   [self presentViewController:navController animated:YES completion:nil]; 
} 

EDIT define a custom delegate in your custom cell for example i took an example CustomCollectionVIewCell as my custom cell in CustomCollectionVIewCell.h i defined a custom delegate as below

 CustomCollectionVIewCell.h

 #import <UIKit/UIKit.h>
 @protocol CellFooterDelegate <NSObject>
  - (void)cellFooterViewTapped; //this this is the custom delegate method
 @end

 @interface CustomCollectionVIewCell : UICollectionViewCell
 @property (weak, nonatomic) IBOutlet UIView *footerCellView;
 @property (nonatomic, assign) id<CellFooterDelegate> myCustomdelegate;//define a delegate
 @end

in CustomCollectionVIewCell.m

in this method u are getting the call to this method

 - (void)profileTapped:(UITapGestureRecognizer *)gesture
  {
    //some code
    if([_myCustomdelegate respondsToSelector:@selector(cellFooterViewTapped)])
    {
       [_myCustomdelegate cellFooterViewTapped]; //call the delegate method 
    }

    NSLog(@"PROFILI %li",(long)gesture.view.tag);
  }

in the view controller where u are using the collection view u do something like this in confirmas to custom delegate like other delegates ViewController.h

#import "CustomCollectionVIewCell.h" //import the file
@interface ViewController :  UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,CellFooterDelegate>//hear i am confirming to custom delegate 
//.... other code

in ViewController.m file

 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {   
     CustomCollectionVIewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
     cell.myCustomdelegate = self; //important as u set self call back to this as u tap the cells footer view

     //....other code
     return cell;

 }

 //if delegate is set correctly u are getting call to this method
 - (void)cellFooterViewTapped
 {
    //hear u can push the controller
    NSLog(@"push hear");

 }

rest of the code as it is no need to change,

hope this helps u .. :)

END EDIT

Shankar BS
  • 8,394
  • 6
  • 41
  • 53
  • this a good idea but doesnt work. I can't use `self` because I'm not calling from a class that inherits ViewController, so I need a way to get my current NavigationController and push over it – Elgert Jul 22 '14 at 08:51
  • sorry now i got u are using collection view cell to present view controller rite ,show the code of entire `- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath` – Shankar BS Jul 22 '14 at 08:56
  • put a custom delegate from cell to view controller and push the view controller – Shankar BS Jul 22 '14 at 09:09
  • first time i heard of custom delegate, any link where I might consult ? – Elgert Jul 22 '14 at 09:12
  • hear is the link http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/ – Shankar BS Jul 22 '14 at 09:17
  • where do you live ? I want to kiss you :p – Elgert Jul 22 '14 at 10:52
  • 1
    ohh thats great .. i am happy t help ... :) more important to know how to create custom delegates .. like above example .. :) – Shankar BS Jul 22 '14 at 11:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57751/discussion-between-shan-and-elgert). – Shankar BS Jul 22 '14 at 11:51
0

You need to do this in

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

method of your collection view.Its like table view didselectrow here you will get navigation controller of the view controller you can access it by self.

Edit

If you have used some other structure and you need to pass controll use delegate or use block to achieve it.

@property(nonatomic,copy)void(^cellSelect)(NSIndexPath*);

set your block and call it in the above mentioned method

self.cellSelect(indexPath);

now you will get call in block when you select item use it to push

amar
  • 4,285
  • 8
  • 40
  • 52
  • Sorry I didn't mention (still haven't drank coffee), I already have implemented that, It is just i want to open different view controller for different parts of the cell, and know i want to launch something from the footer, which i can only access inside the Custom CollectionViewCell method – Elgert Jul 22 '14 at 08:34
0

I answered a similar question here. If you implement that, you can access the current view controller by doing:

[UIViewController currentViewController];
Community
  • 1
  • 1
jjv360
  • 4,120
  • 3
  • 23
  • 37
  • nice recursive function but I get an error at `findBestViewController` it doesnt recognize it, any idea ? – Elgert Jul 22 '14 at 09:27