我需要从我的自定义 UICollectionViewCell
启动一个 ViewController
但我无法在那里访问我的 navigationController
所以我不知道如何启动因为我只知道一种方法。
仅举个例子
LoginViewController *loginView = [[LoginViewController alloc]init];
[self.navigationController pushViewController:loginView animated:YES];
知道我该怎么做吗?
这就是我通常调用 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];
}
但我想做的是调用与这里不同的东西
- (void) profileTapped:(UIGestureRecognizer *)gesture{
//some code
NSLog(@"PROFILI %li",(long)gesture.view.tag);
}
我已经为 cell
的页脚 View 提供了一个标签,并希望在单击它时做一些不同的事情,我只能在 Cell
中获取点击事件类。
最佳答案
如果我完全理解你的问题,你想从 Collection View 单元启动一个 View Controller ,作为一个 Root View Controller ,你可以推送和弹出其他 View Controller
你可以这样做,
- (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];
}
编辑
在您的自定义单元格中定义一个自定义委托(delegate),例如我举了一个例子
CustomCollectionVIewCell
作为我在 CustomCollectionVIewCell.h
中的自定义单元格,我定义了一个自定义委托(delegate),如下所示
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
在CustomCollectionVIewCell.m
在这个方法中你得到了这个方法的调用
- (void)profileTapped:(UITapGestureRecognizer *)gesture
{
//some code
if([_myCustomdelegate respondsToSelector:@selector(cellFooterViewTapped)])
{
[_myCustomdelegate cellFooterViewTapped]; //call the delegate method
}
NSLog(@"PROFILI %li",(long)gesture.view.tag);
}
在你使用 Collection View 的 View Controller 中你做这样的事情
在 confirmas 中像其他代表一样自定义代表
ViewController.h
#import "CustomCollectionVIewCell.h" //import the file
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,CellFooterDelegate>//hear i am confirming to custom delegate
//.... other code
在ViewController.m
文件中
-(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");
}
其余代码无需更改,
希望这对你有帮助.. :)
结束编辑
关于ios - 如何从自定义 UICollectionViewCell 启动 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24882335/