从外部类调用pushViewController?

时间:2014-05-27 10:44:42

标签: cocoa-touch ios7

我是iOS 7新手。我很难打电话给pushViewController

我有MyCollectionViewController类,它是UINavigationControllerCollectionViewCell类的根控制器,用于自定义集合单元格。

在这种情况下,我pushViewController使用MyCollectionViewController时没有任何问题:

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

NSDictionary *product = self.products[indexPath.row];

TXDetailProductController *detailView = [[TXDetailProductController alloc] init];
detailView.product = product;

[self.navigationController pushViewController:detailView animated:YES];

}

但我想从pushViewController:detailView中包含的CollectionViewCell类致电MyCollectionViewController

1 个答案:

答案 0 :(得分:2)

您可以轻松地创建委托方法并将其定义到类中。您可以轻松创建委托方法并将其定义到您具有didSelectItemAtIndexPath的类中,并从CollectionViewCell类调用委托方法。

@protocol CollectionViewDelegate <NSObject>
-(void)pushMethod;

@end
@interface CollectionViewController : UIViewController
{
}
@property(nonatomic,retain)id<CollectionViewDelegate>delegate;

并在你有CollectionView的同一个类中实现push方法:

-(void)pushMethod
{
TXDetailProductController *detailView = [[TXDetailProductController alloc] init];
detailView.product = product;

[self.navigationController pushViewController:detailView animated:YES];
}

并从CollectionViewCell类中轻松调用此方法。