为UIView创建子类以实现可重用性
我有一个DiscoverViewController.h和.m文件,它们有一个函数,可以替换要发现的位置的视图。我也想在输出视图控制器中重用此视图。具有的DicoverViewController.m的代码如下:为UIView创建子类以实现可重用性,uiview,ios,objective-c,subclass,uitapgesturerecognizer,Uiview,Ios,Objective C,Subclass,Uitapgesturerecognizer,我有一个DiscoverViewController.h和.m文件,它们有一个函数,可以替换要发现的位置的视图。我也想在输出视图控制器中重用此视图。具有的DicoverViewController.m的代码如下: for (PFObject *views in objects) { // Get the discoverr view setup CGRect frame = CGRectMake(5.0, _viewStart, 310.0, viewHeight); Discov
for (PFObject *views in objects)
{
// Get the discoverr view setup
CGRect frame = CGRectMake(5.0, _viewStart, 310.0, viewHeight);
DiscoverView *parent = [[DiscoverView alloc] init];
[parent buildDiscoverViewWithFrame:frame andObjects:replies];
// UIView *parent = [[UIView alloc] initWithFrame:CGRectMake(5.0, _viewStart, 310.0, viewHeight)];
parent.backgroundColor = [UIColor whiteColor];
// parent.layer.cornerRadius = 2.0;
parent.layer.borderColor = [UIColor regularColor].CGColor;
parent.layer.borderWidth = 1.0f;
parent.tag = 1000 + _step;
// Add the label counter
// Add discover id for testing (is unique id)
UILabel *placeholder = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 10.0, 310.0, 12.0)];
placeholder.backgroundColor = [UIColor clearColor];
placeholder.textAlignment = NSTextAlignmentCenter;
placeholder.textColor = [UIColor bestTextColor];
placeholder.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:(12.0)];
placeholder.Text = [NSString stringWithFormat:@"%@", views.objectId];
[parent addSubview:placeholder];
// Increase size of content view
CGRect newContentView = _contentView.frame;
newContentView.size.width = _contentView.frame.size.width;
newContentView.size.height = _contentView.frame.size.height + bestHeight;
[_contentView setFrame:newContentView];
[_contentView addSubview:parent];
scrollView.contentSize = _contentView.frame.size;
// Adjust the postions
_viewStart = _viewStart + viewHeight - 1.0;
_step = _step + 1;
}
这个类名为DiscoverView.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface DiscoverView : UIView
- (UIView *) buildDiscoverViewWithFrame:(CGRect) frame andObjects:(PFObject *) objects;
@end
不知何故,这是不正确的工作。框架不会显示在正确的空间中,水龙头根本不工作。我是否需要将其更改为类引用与实例?如何重用此类(DiscoveryView)并使其正常工作
谢谢。你的想法很好,我认为你走对了。。。你刚才犯了一些错误:
- (UIView *)buildDiscoverViewWithFrame:(CGRect) frame andObjects:(PFObject *) objects
{
//Your method name is buildDiscoverView, but you are making a view
//that is not a DiscoverView class.
UIView *discover = [[UIView alloc] initWithFrame:frame];
//Tap gesture stuff...
return discover;
}
以及:
我很确定这不是你真正想要的。我认为您正在尝试构建一个DiscoveryView实例,对吗?您可以这样做:
关于您的发现视图.m
- (id)initWithFrame:(CGRect)frame andObjects:(PFObject *)objects {
//You can call initWithFrame: method here as our base constructor
self = [super initWithFrame:frame];
if (self) {
//Add tap gesture here
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(discoverTapPressed)];
[self addGestureRecognizer:tap];
//and maybe do some other stuff as changing the background color
//or making the border stuff that you are doing outside...
}
return self;
}
DiscoverView *parent = [[DiscoverView alloc] initWithFrame:frame andObjects:replies];
//If you do this on init, you don't need this anymore...
//parent.backgroundColor = [UIColor whiteColor];
//parent.layer.cornerRadius = 2.0;
//parent.layer.borderColor = [UIColor regularColor].CGColor;
//parent.layer.borderWidth = 1.0f;
现在,您可以按如下方式初始化实例:
在您的DiscoveryViewController.m上
- (id)initWithFrame:(CGRect)frame andObjects:(PFObject *)objects {
//You can call initWithFrame: method here as our base constructor
self = [super initWithFrame:frame];
if (self) {
//Add tap gesture here
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(discoverTapPressed)];
[self addGestureRecognizer:tap];
//and maybe do some other stuff as changing the background color
//or making the border stuff that you are doing outside...
}
return self;
}
DiscoverView *parent = [[DiscoverView alloc] initWithFrame:frame andObjects:replies];
//If you do this on init, you don't need this anymore...
//parent.backgroundColor = [UIColor whiteColor];
//parent.layer.cornerRadius = 2.0;
//parent.layer.borderColor = [UIColor regularColor].CGColor;
//parent.layer.borderWidth = 1.0f;
仅供参考:我还建议您检查新的IBDesignable
功能,它允许我们直接从故事板可视化结果(但这不是您在本线程中要求的)
更新:
正如@abhishekkharwar所说,有许多其他方法可以更好地做到这一点,比如使用
UICollectionView
或UITableView
,但您必须决定哪些方法更适合您的应用程序需求。不要重新发明轮子。将名为DiscoverView.h的UIView类更改为
- (id)initWithFrame:(CGRect)frame andObjects:(PFObject *)objects;
将实现文件DiscoverView.m更改为:
@interface DiscoverView ()
@property (nonatomic, strong) id photoObject;
@end
@implementation DiscoverView
- (id)initWithFrame:(CGRect)frame andObjects:(PFObject *)objects {
//You can call initWithFrame: method here as our base constructor
self = [super initWithFrame:frame];
if (self) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(discoverTapPressed)];
[self addGestureRecognizer:tap];
_photoObject = objects.objectId;
self.backgroundColor = [UIColor whiteColor];
self.layer.cornerRadius = 2.0;
self.layer.borderColor = [UIColor redColor].CGColor;
self.layer.borderWidth = 1.0f;
}
return self;
}
- (void) discoverTapPressed
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The id is:"
message:_photoObject
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
替换旧代码中的代码:
DiscoverView *parent = [[DiscoverView alloc] init];
[parent buildDiscoverViewWithFrame:frame andObjects:replies];
// UIView *parent = [[UIView alloc] initWithFrame:CGRectMake(5.0, _viewStart, 310.0, viewHeight)];
parent.backgroundColor = [UIColor whiteColor];
// parent.layer.cornerRadius = 2.0;
parent.layer.borderColor = [UIColor regularColor].CGColor;
parent.layer.borderWidth = 1.0f;
parent.tag = 1000 + _step;
到
为什么不使用UITableView或UICollectionView?创建manual infinite scrollview背后有什么具体原因吗?请参阅下面我的评论(几乎90%的工作都是由表视图完成的,但我希望我的应用程序的最后10%的UI很难实现)。但我确实在其他地方使用表视图和集合视图,而整个页面本身就是一个集合或表视图。你能发布UI的屏幕截图吗?是的,我在应用程序中有集合视图和表视图。正是因为这些ceratin页面/控制器,页面上有更多不同的UI类型,使得这些视图“不太适合”。但我确实在其他地方使用了它们(tableview让我在那里得到了90%,但最后10%证明使用tableview很难实现)。我同意不重新创造遗嘱是件好事。@chris>好的,所以只要按照我上面写的方式修改代码就可以了,而且要开心。。。我相信这会解决你的问题。
DiscoverView *parent = [[DiscoverView alloc] initWithFrame:frame andObjects:replies];
parent.tag = 1000 + _step;