UITableViewController をサブクラス化し、テーブル内にカスタムセルを持っています。そして、このカスタム セルは内部で UIView をサブクラス化しています。したがって、この UIView は独自のクラスで記述されます。私のコードでは、UITableViewController クラスの名前は MainViewController.h/.m で、UIView のクラスの名前は ContentView.h/.m なので、ContentView に画像と tapGestureRecognizer を追加しました。画像がタップされると、何らかの日付 (この場合は数字) が 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);
}