如何从非ViewController类启动ViewController? [英] How to launch a ViewController from a Non ViewController class?
问题描述
标题说明了一切,我通常会这样打开ViewControllers
the title says it all, i usually open ViewControllers like this
ListingViewController *listingView = [[ListingViewController alloc]init];
[self.navigationController pushViewController:listingView animated:YES];
但是在这个特殊的类上,这是一个自定义的UICollectionView单元格,我想基于单击的单元格启动一个新的控制器.我该怎么办?
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 ?
推荐答案
通常的方法是在包含UICollectionView的视图控制器中实现UICollectionViewDelegate
方法– collectionView:didSelectItemAtIndexPath:
.然后,您可以按常规方式显示新的视图控制器.
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.
例如:
- (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];
}
这篇关于如何从非ViewController类启动ViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!