![](/img/trans.png)
[英]Go straight to another view controller (without generating the floating button options)on clicking the floating button(ActionButton)
[英]Go to another view by clicking a button in Custom Cell
我创建了自定义单元格(XIB)作为UICollectionViewCell
的子类,并且该单元格中有一个按钮。 当我单击一个按钮时,我想转到另一个包含一些数据的视图,也可以通过单击一个按钮返回到原始视图。 我已经搜索过了,找到了“ segue”或“ modal”之类的东西,但最初无法从我的自定义单元中进行。
有什么办法吗? 任何帮助将非常感激。
因此,由于UICollectionView的工作原理与UITableView相同,因此您想要做的是使UICollectionViewCell的子类包含一个协议 ,该协议可将操作(如按按钮)从另一个视图发送到视图控制器。 在这种情况下,另一个视图是UICollectionViewCell。
添加一个新的可可触摸类称为UICustomCollectionViewCell与UICollectionViewCell的子类。 并包含界面生成器文件
头文件UICustomCollectionViewCell.h
@protocol UICustomCollectionViewCellDelegate;
@interface UICustomCollectionViewCell : UICollectionViewCell
@property ( nonatomic, retain) IBOutlet UIButton *button;
- (IBAction)pressButton:(id)sender;
@property ( assign) id< UICustomCollectionViewCellDelegate> delegate;
@end
@protocol UICustomCollectionViewCellDelegate <NSObject>
@optional
- (void)customCollectionViewCell:(UICustomCollectionViewCell *)cell pressedButton:(UIButton *)button;
@end
实现文件UICustomCollectionViewCell.m
@implementation UICustomCollectionViewCell
@synthesize delegate;
- (IBAction)pressButton:(id)sender {
if ([delegate respondsToSelector: @selector( customCollectionViewCell:pressedButton:)])
[delegate customCollectionViewCell: self pressedButton: sender];
}
@end
xib文件UICustomCollectionViewCell.xib
确保UICustomCollectionViewCell的连接已连接到Connections检查器中的按钮:
最后,在您的项目中使用此类
导入类和委托:
#import "UICustomCollectionViewCell.h"
@interface ViewController () < UICustomCollectionViewCellDelegate>
@end
在下面的代码中,您将使用UICustomCollectionViewCell类而不是UICollectionViewCell :
UICustomCollectionViewCell *cell;
...
[cell setDelegate: self];
...
return cell;
现在按下按钮时将调用的动作或方法:
- (void)customCollectionViewCell:(UICustomCollectionViewCell *)cell pressedButton:(UIButton *)button {
//action will be here when the button is pressed
}
如果要查找此单元格来自哪个indexPath:
[collectionView indexPathForCell: cell];
您不能/不应该在单元格中执行导航作业,导航不在单元格域中。
您可以尝试的是
1)使用一个委托,设置一个委托并将其连接到按钮操作,主持tableview / collection视图的控制器可以将自己设置为委托并侦听任何事件。 该控制器应负责使用所需的任何方法将新视图推入堆栈。
2)如果您讨厌代表但喜欢阻止,则可以在单元格上设置回调块,可以在控制器的cellForRowAtIndex:方法中设置其动作。
注意到这里的模式吗? 以上两种方法都将任务从单元委派给控制器。
如果全部失败,只需实现didSelectItemAtIndexPath:
并坚持下去即可。
您是否尝试过didSelect方法?
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
YourNewViewControllerClass *someViewController = [storyboard instantiateViewControllerWithIdentifier:@"YourNewVCID"];
[self presentViewController:someViewController
animated:YES
completion:nil];
}
最简单的方法是实现cellForRow..
方法,为单元格/按钮设置一个标签,然后根据该标签做出反应(例如indexPath.row
)。
1.自定义您的按钮
NouMapButton.h
#import <Foundation/Foundation.h>
@interface NouMapButton : UIButton
@property (nonatomic, readwrite, retain) NSObject *dataObj;
@end
NouMapButton.m
#import "NouMapButton.h"
@implementation NouMapButton
@end
设置您的按钮数据并定位到
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath btn01.dataObj = YOUR_DATA; [btn01 addTarget:self action:@selector(map:) forControlEvents:UIControlEventTouchUpInside];
那么您可以在sender.dataObj中获得按钮自定义dataObj
-(void)map:(NouMapButton *)sender{ MapViewController *nextView = [[MapViewController alloc] init]; nextView.dataObj = sender.dataObj; //TODO.... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.