Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 导航到按钮上的视图控制器从collectionview单元格中单击_Objective C_Uiviewcontroller_Uibutton_Uicollectionviewcell - Fatal编程技术网

Objective c 导航到按钮上的视图控制器从collectionview单元格中单击

Objective c 导航到按钮上的视图控制器从collectionview单元格中单击,objective-c,uiviewcontroller,uibutton,uicollectionviewcell,Objective C,Uiviewcontroller,Uibutton,Uicollectionviewcell,我有一个由集合视图组成的viewcontroller。此集合视图有一个UIbutton块。我想在单击此按钮时导航到另一个视图控制器并传递一些数据。请注意,它是一个UIButton块 非常感谢您的帮助 首先,确保包含集合视图的viewcontroller中嵌入了NavigationController 其次,有两种方法可以将某些数据传递给另一个视图控制器: 第一种方式: 让我们设置按钮选择器 [button addTarget:select action:@selector(buttonTouch

我有一个由集合视图组成的viewcontroller。此集合视图有一个UIbutton块。我想在单击此按钮时导航到另一个视图控制器并传递一些数据。请注意,它是一个UIButton块
非常感谢您的帮助

首先,确保包含集合视图的viewcontroller中嵌入了NavigationController

其次,有两种方法可以将某些数据传递给另一个视图控制器:

  • 第一种方式:

    让我们设置按钮选择器

    [button addTarget:select action:@selector(buttonTouched:)
        forControlEvents:UIControlEventTouchUpInside];
    
    然后让我们实现选择器,如下所示

    - (void)buttonTouched:(id)sender {
    // Find tableViewCell
    UIView *view = field;
    while (view && ![view isKindOfClass:[UICollectionViewCell class]]){ 
      view = view.superview;
    }
    UICollectionViewCell *cell = (UICollectionViewCell *)view;
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    // TODO: get data for above indexPath
    UIViewController *nextViewControler = [[UIViewController alloc] init];
    [self.navigationController pushViewController:nextViewControler animated:YES];
    }
    
  • 第二种方式(用于自定义UICollectionViewCell):

    为该按钮连接iAction,然后定义一个类似于cell属性的块(注意:该块的属性应该是copy

    例如:
    @property(复制,非原子)dispatch\u block\u t button touchedblock

    然后让我们在
    cellForItemAtIndexPath
    方法中实现它

    __弱类型(自我)弱自我=自我; cell.buttonTouchedBlock=^{ [weakSelf buttonotuchedat:indexPath]; }