【发布时间】:2017-03-12 14:40:19
【问题描述】:
我有一个由集合视图组成的视图控制器。这个集合视图有一个 UIbutton 块。单击此按钮并传递一些数据时,我想导航到另一个视图控制器。请注意,它是一个 UIButton 块 将不胜感激任何帮助。
【问题讨论】:
标签: objective-c uiviewcontroller uibutton uicollectionviewcell
我有一个由集合视图组成的视图控制器。这个集合视图有一个 UIbutton 块。单击此按钮并传递一些数据时,我想导航到另一个视图控制器。请注意,它是一个 UIButton 块 将不胜感激任何帮助。
【问题讨论】:
标签: objective-c uiviewcontroller uibutton uicollectionviewcell
首先,确保包含集合视图的视图控制器中嵌入了 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):
你为那个按钮连接IBAction,然后你定义一个像单元格属性的块(注意:那个块的属性应该是copy)
例如:@property(copy, nonatomic) dispatch_block_t buttonTouchedBlock;
那我们在cellForItemAtIndexPath方法中实现吧。
__weak typeof(self) weakSelf = self; cell.buttonTouchedBlock = ^{ [weakSelf buttonTouchedAt:indexPath]; }
【讨论】: