【发布时间】:2012-11-08 23:35:12
【问题描述】:
我有子类 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
内容视图.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)passDigit:(float)someDigit;的内容贴出来 -
我很谨慎地使用我的方法的名称。现在它被编辑了。 concole 什么都没有。所以 passDigit: 不会被调用。
标签: iphone objective-c ios xcode cocoa-touch