我有子類UITableViewController和表內我有自定義單元格。而這個自定義單元格里面已經有了子類UIView。所以這個UIView是用自己的類寫的。在我的代碼中,UITableViewController類被命名爲MainViewController.h/.m,而UIView的類被命名爲ContentView.h/.m。因此,在ContentView中,我添加了一個圖像和tapGestureRecognizer。要點擊圖像時,會將某個日期(本例中爲數字)發送到MainViewController。第一個問題是委託方法沒有被調用。如果我使用notificationCenter調用它,它會將其記錄爲0.00000有人可以幫助我將數據從單元內的視圖傳遞到ViewController。如何將數字或數據從子類UIView傳遞給UIViewController?
這是我的代碼:
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);
}
你爲什麼不更新最後一個問題? – rmaddy
因爲我改變了它。這裏我不使用通知中心。 –
你沒有真正改變它;你只是刪除了一些文本... – pasawaya