0

the title says it all, i usually open ViewControllers like this

ListingViewController *listingView = [[ListingViewController alloc]init];
[self.navigationController pushViewController:listingView animated:YES];

but on this particular class thats a custom UICollectionView cell i want to launch a new controller based on the cell thats clicked. how can i do that ?

Elgert
  • 470
  • 2
  • 5
  • 19

7 Answers7

2

You are looking to add detail views to a cell try this link on Adding Detail Views.Hopefully this Helps

v1shnu.mee
  • 122
  • 1
  • 1
  • 8
  • Basicallly its that but im not using segue and storyboard my whole app is coded so i create everything programatically – Elgert Jul 10 '14 at 09:01
1

The usual way is to implement the UICollectionViewDelegate method – collectionView:didSelectItemAtIndexPath: in the view controller that contains the UICollectionView. Then you can present the new view controller in the normal way.

For example:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    MyModel * model = self.listItems[indexPath.row]; //retrieve the object that is displayed by the cell at the selected index

    MyViewerViewController * vc = [[MyViewerViewController alloc] initWithMyModel:model];
    [self.navigationController pushViewController:vc animated:YES];

}
Weaverfish
  • 930
  • 8
  • 17
  • i was just trying this and when clicking cells it doesnt do anything (I tried to NSLOG indexpath). Im using PSCollectionView i dont think its that different from UICollectionView. – Elgert Jul 10 '14 at 09:01
  • I think the equivalent delegate method for PSCollectionView is - (void)collectionView:(PSCollectionView *)collectionView didSelectCell:(PSCollectionViewCell *)cell atIndex:(NSInteger)index;. Make sure you have set your view controller to be the delegate of the collectionView as well as the dataSource. – Weaverfish Jul 10 '14 at 09:07
  • i did it with this - (void)collectionView:(PSCollectionView *)collectionView didSelectCell:(PSCollectionViewCell *)cell atIndex:(NSInteger)index but now the problem is it is not called everywhere in the cell. I have UIViews all over the cell (its complicated) how to oveeride click ? – Elgert Jul 10 '14 at 10:22
  • 1
    Do you mean when you press some areas it doesn't respond? If a view in the cell is consuming the touch you can disable user interactions with setUserInteractionEnabled: on any UIView subclass. – Weaverfish Jul 10 '14 at 13:38
1

Use the UICollectionViewDelegate method didSelectItemAtIndexPath for doing this and make sure that this delegate method is present in the view controller class implementing the UICollectionView.

Bug Hunter Zoro
  • 1,743
  • 18
  • 21
0

You have to notify the ViewController, on cell click, on which UICollectionView exist. and on this controller you can push the desired controller

Ritu
  • 661
  • 3
  • 8
0

You need a view controller of some sort for pushing or presenting, there's no way around that.

You shouldn't have a problem accomplishing that and there are a few ways you can do so. First off, you should be able to write this logic in the UICollectionViewDelegate method collectionView:didSelectItemAtIndexPath:. Your delegate is most likely the containing view controller so that may be all you need to do. If however it isn't, just add a view controller property to your delegate that you can use for presenting things. Make it a weak one to avoid a possible retain cycle (where the view controller retains the delegate and the delegate retains the view controller).

Dima
  • 23,484
  • 6
  • 56
  • 83
0

self.navigationController can be accessed via only UIViewController's subclass. You want to access UINavigationController from not UIViewcontroller class. Is it what you are asking?

If you have property of UINavigationController at AppDelegate like

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navigationController;
@end

You can get a pointer from wherever you want as below

#import "AppDelegate.h"
AppDelegate *delegate = (AppDelegate *)[[[UIApplication sharedApplication] delegate];
UINavigationController *navigation = [delegate navigationController];

Now you have it.

Ryan
  • 4,799
  • 1
  • 29
  • 56
0

You need to get the current UIViewController, there are multiple ways to do it. You can parse the VC in your UICollectionViewCell's header file. You can also use self.window.rootviewcontroller.

After that you can just use [VC pushViewController:listingView animated:YES]; or [self.window.rootviewcontroller pushViewController:listingView animated:YES];

lennartk
  • 570
  • 1
  • 4
  • 15