【问题标题】:How to pass digit or data from subclassed UIView to UIViewController?如何将子类 UIView 中的数字或数据传递给 UIViewController?
【发布时间】: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


【解决方案1】:

不确定您要做什么,可能是您是新手并且正在学习一些东西。尝试执行以下操作:

在 mainViewController 中改变你的方法

- (void) showDetailViewControllerWithDigit:(float)someDigit
{
  NSLog(@"%f",someDigit);
}

- (void)passDigit:(float)someDigit
{
  NSLog(@"%f",someDigit);
}

它应该可以工作。在这里也不是很相关,但是您在两个不同的地方拼写了委托和委托。请注意,它们都将被视为两个不同的变量。虽然没有必要有一个同名的实例变量,但我绝对不会有一个轻微的错字,因为它会导致很多问题。

当您为委托定义协议时,您在那里定义的方法应该在委托类中实现。

同样在您的代码中,显然您错过了一些部分,这些部分显示了您在主视图控制器中添加 contentView 的位置。我假设你有一些地方

[self.view addSubview:contentView];

在 viewDidLoad 或某些地方,没有它你甚至看不到 contentView,因此无法点击它。

编码愉快。

【讨论】:

  • 抱歉我的错误。在我的课堂上我有正确的拼写,当我在这里写的时候我犯了错误。我检查名称和 passDigit: 方法没有被调用。问题出在哪里。我将视图添加到自定义 cellView 中。我想从 UITableViewController 的子类 MainViewController 访问它。而且我可以看到视图,并且可以识别触摸,我对其进行了测试,所以委托肯定有问题。谁能告诉我你将如何从 cellView 访问数据视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多