1

尝试 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

视图控制器.h

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

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


    @property(nonatomic,strong) HeaderView *header;

    @end

视图控制器.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(@"passing data==========%f",rating);

从方法:

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

    }

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

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

4

1 回答 1

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];
}
于 2012-11-06T15:05:42.210 回答