将数据从uiview传递到uiviewcontroller

时间:2012-11-06 15:01:26

标签: ios uiview uiviewcontroller

尝试了2天之后我现在失去理智......主要问题很简单..我在uiview中按钮,我想在uiviewcontroller上调用方法...这里是代码

Uiview调用headerView.h

@class HeaderView;
@protocol headerViewDelegate
- (void)tes:(HeaderView *)tes ratingDidChange:(float)rating;
@end
@interface HeaderView : UIView {
    UIButton *test;


}
@property (nonatomic,strong) UIButton *test;
@property (assign) id <headerViewDelegate> delegate;
@end

和Headerview.M

#import "HeaderView.h"
#import <QuartzCore/QuartzCore.h>

@implementation HeaderView

@synthesize test;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame]; // not needed - thanks ddickison
    if (self) {
        test=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        test.frame=CGRectMake(30, 10, 100,40);
        [test setTitle:@"Kampret" forState:UIControlStateNormal];
        [test addTarget:self action:@selector(testdata:) forControlEvents:UIControlEventTouchUpInside];

    }
    [self addSubview:test];
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {

    }
    return self;
}
- (void)dealloc {

    [super dealloc];
}
-(void)layoutSubviews{
    self.backgroundColor=[UIColor whiteColor];

    [self release]; // release object before reassignment to avoid leak - thanks ddickison
    //self = [nib objectAtIndex:0];

    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOffset = CGSizeMake(1.0, 1.0);
    self.layer.shadowOpacity = 0.40;


}
-(void)testdata:(id)sender{
    [self.delegate tes:self ratingDidChange:1.1f];

}
@end

viewcontroller.h

   #import <UIKit/UIKit.h>
    #import "HeaderView.h"

    @interface ReusableTableHeadersViewController : UITableViewController <headerViewDelegate> {
        HeaderView *header;
    }


    @property(nonatomic,strong) HeaderView *header;

    @end

viewcontroller.m

#import "viewcontroller.h"


@implementation ReusableTableHeadersViewController
@synthesize header;
@synthesize tblData, data;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    header = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
    [self.view addSubview:header];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [super dealloc];
}
-(void)tes:(HeaderView *)tes ratingDidChange:(float)rating{
    NSLog(@"passing data==========%f",rating);
}
@end

我想记录评分:     NSLog(@“传递数据==========%f”,评级);

来自方法:

-(void)testdata:(id)sender{
        [self.delegate tes:self ratingDidChange:1.1f];

    }

但它没有显示......为什么?因为我使用initwithframe?

不要责怪我......我在这里完全是新手:)

1 个答案:

答案 0 :(得分:3)

You have not set delegate of headerview as view controller.

- (void)viewDidLoad {
    [super viewDidLoad];
    header = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
    header.delegate = self;
    [self.view addSubview:header];
}