我已经分类了uitaiteViewController,并且在表中我有自定义单元格。并且该自定义单元格在内部进行了uiview。因此,这个Uiview以其自己的班级编写。在我的代码中,uitaiteViewController类命名为mainViewController.h/.m,并且Uiview的类命名为ContentView.h/.m,因此在ContentView中我添加了一个图像和TapgestureRerecognizer。当图像点击时(在这种情况下为Digit)时,将发送到MainViewController。第一个问题是委托方法未被调用。而且,如果我使用NotificationCenter调用它,则将其记录为0.00000,有人可以帮助我从电视机内的视图传递到ViewController。
这是我的代码:
contentview.h:
@class ContentView;
@protocol ContentViewDelegate
- (void)passDigit:(float)someDigit;
@end
#import <UIKit/UIKit.h>
#import "MainViewController.h"
@interface ContentView : UIView
{
id <ContentViewDelegate> delegate;
float someDigit;
}
@property float someDigit;
@property (assign) id <ContentViewDelegate> delegate;
@end
contentview.m
#import "ContentView.h"
@implementation ContentView
@synthesize someDigit;
@synthesize delegate;
- (void)handleContentTouch:(UIGestureRecognizer *)gesture
{
someDigit = 134;
[self.delegate passDigit:someDigit];
}
- (void)setupView
{
CGRect frame = self.frame;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleContentTouch:)];
UIImageView *fifthBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[self addSubview:fifthBackground];
[fifthBackground setUserInteractionEnabled:YES];
[fifthBackground addGestureRecognizer:tap];
}
mainViewController.h
#import <UIKit/UIKit.h>
#import "ContentView.h"
@interface MainViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, ContentViewDelegate>
@end
mainviewContorller.m
#import "MainViewController.h"
@implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
ContentView *contentView = [[ContentView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
contentView.delegate = self;
}
- (void) passDigit:(float)someDigit
{
NSLog(@"%f",someDigit);
}
不确定您要做什么,可能是您是新手并学习一些东西。尝试执行以下操作:
在mainviewController中更改您的方法
- (void) showDetailViewControllerWithDigit:(float)someDigit
{
NSLog(@"%f",someDigit);
}
to
- (void)passDigit:(float)someDigit
{
NSLog(@"%f",someDigit);
}
它应该起作用。在这里也不是很重要,但是您在两个不同的地方拼写了代表和Delegete。请注意,它们俩都将被视为两个不同的变量。尽管没有必要具有具有相同名称的实例变量,但我绝对不会有一个稍微错字,因为以后会造成很多问题。
定义代表的协议时,您已定义的方法应在代表类中实现。
在您的代码中,显然您错过了一些零件,这些部分显示了您在主视图控制器中添加ContentView的位置。我假设您有一些
[self.view addSubview:contentView];
在ViewDidload或某些地方,没有它,您甚至找不到ContentView,而在那里则无法点击它。
快乐的编码。