gpt4 book ai didi

ios - 将数据从 uiview 传递到 uiviewcontroller

转载 作者:行者123 更新时间:2023-12-01 19:16:06 24 4
gpt4 key购买 nike

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

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

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


@property(nonatomic,strong) HeaderView *header;

@end

View Controller .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",rating);

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

}

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

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

最佳答案

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];
}

关于ios - 将数据从 uiview 传递到 uiviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13253712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com