如何从非ViewController类启动ViewController? [英] How to launch a ViewController from a Non ViewController class?

查看:72
本文介绍了如何从非ViewController类启动ViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了一切,我通常会这样打开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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆