Cocoa touch 从外部类调用pushViewController?

Cocoa touch 从外部类调用pushViewController?,cocoa-touch,ios7,Cocoa Touch,Ios7,我是IOS7新手,正在努力调用pushViewController 首先,我有UINavigationController作为根视图控制器,我有UICollectionViewController 在这个UICollectionViewController中,我还注册了UICollectionViewCell类 [self.collectionView registerClass:[MYProductCell class] forCellWithReuseIdentifier:@"prod

我是IOS7新手,正在努力调用
pushViewController

首先,我有
UINavigationController
作为根视图控制器,我有
UICollectionViewController

在这个
UICollectionViewController
中,我还注册了
UICollectionViewCell

[self.collectionView registerClass:[MYProductCell class]
   forCellWithReuseIdentifier:@"product"];
我正在尝试根据单元格中的用户操作推送视图控制器宽度详细视图

我使用它没有问题

-(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];

}  
但是我想根据用户操作从
UICollectionViewCell
类调用pushViewController:detailView

有人能给我指路吗?
提前谢谢

您可以发送一条消息

在viewDidLoad中,您可以设置消息侦听器:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushDetailView:) name:@"pushDetailView" object:nil];
然后添加一个方法:

    -(void) pushDetailView:(id)sender
    {
        // do your pushViewController
    }
然后在您的
UICollectionViewCell
中,当您需要推送视图时,请执行以下操作:

    NSNotification* notification = [NSNotification notificationWithName:@"pushDetailView" object:self];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
侦听器应该收到该通知并调用将推送视图的
pushDetailView
。您可能还需要在那里进行一些错误检查

您可能需要将信息传递给该方法,以便知道要推送什么。您可以将信息放入对象中,并将其与消息一起发送。比如:

    NSNumber *indexPathRow = [NSNumber numberWithInt: indexPath.row];
    NSNotification* notification = [NSNotification notificationWithName:@"pushDetailView" indexPathRow object:indexPathRow];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
然后在receiver类上,从通知的对象中提取该信息:

    -(void) pushDetailView:(NSNotification*)note
    {
        NSNumber indexPathRow = [note object];
        NSDictionary *product = self.products[[indexPathRow intValue]];
        // and on with your pushViewController code
    }

再次在那里检查时出错。

请稍候。。我正在编辑我的答案以使其更好。实际上,我不确定NSNumber是否可以作为通知对象,或者是否必须将其转换为NSString。NSIndexPath可能直接成为对象,而无需对话。我想先试试。上帝保佑你!它起作用了!光滑如丝。当我开始研究如何使用NSNotification传递参数时,您的编辑来得正是时候。但现在你的答案是完整的!再次感谢。那么您是否能够直接将NSIndexPath作为对象发送?是的。直接从单元格:NSNotification*notification=[NSNotification notificationWithName:@“pushDetailView”对象:self.product];