gpt4 book ai didi

ios - 将 double 传递给 Viewcontroller,然后传递给 UIView

转载 作者:行者123 更新时间:2023-11-28 22:10:30 25 4
gpt4 key购买 nike

完整代码

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewControllerImage : UIViewController {
IBOutlet UILabel *datum;
IBOutlet UILabel *debug;
}

@property (nonatomic, assign) double thicknessValue1;
@property (nonatomic, assign) double capWidthValue1;
@end

ViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[datum setTransform:CGAffineTransformMakeRotation(M_PI / 2)];

NSString *value = [[NSString alloc] initWithFormat:@"%f", self.thicknessValue1];

debug.text = value;

draw2D *myView = [[draw2D alloc]initWithThickness: self.thicknessValue1 andCapWidth: self.capWidthValue1];

NSLog(@"First Logs");
NSLog(@"%f",myView.thicknessValue2);
NSLog(@"%f",myView.capWidthValue2);
NSLog(@"END FIRST LOGS");
}

draw2D.h #导入

@interface draw2D : UIView 

- (id)initWithThickness:(double)thickness andCapWidth:(double)capWith;


@property (nonatomic, assign) double thicknessValue2;
@property (nonatomic, assign) double capWidthValue2;

@end

draw2D.m

#import <UIKit/UIKit.h>

@interface draw2D : UIView

- (id)initWithThickness:(double)thickness andCapWidth:(double)capWith
{
self = [super init];
if(self)
{
thicknessValue2 = self.thicknessValue2;
capWidthValue2 = self.capWidthValue2;
NSLog(@"Second Logs");
NSLog(@"%f",thicknessValue2);
NSLog(@"%f",capWidthValue2);
NSLog(@"END Second LOGS");
}
return self;
}



@end

我已经为您提供了第二个 View Controller 和 UIView 的完整代码,我希望您能看到我哪里出错了。或者向我解释我做错了什么。

最佳答案

我正在添加另一个答案,因为之前的答案已经太长了。

伙计,我 body 上不能做更多的事情,它一点也不复杂。很抱歉,没有在网上找到有关创建对象的信息是不正确的,它几乎无处不在。我在哪里学的?

2个注意事项。

  1. 命名约定表示类以大写字母开头,例如draw2d 需要是 Draw2d

  2. 您不应该将变量命名为 xxx1、xxx2 等。如果它们是同一事物,请将它们命名为同一事物。如果他们是不同的类(class)也没关系。

代码:

在 View Controller 中:

- (void)viewDidLoad
{
[super viewDidLoad];

NSLog(@"Before init");
Draw2d *myView = [[Draw2d alloc] initWithFrame:CGRectMake(0, 0, 320, 320) thickness:15.0 capwidth:17.5];
NSLog(@"After init");
[self.view addSubview:myView];
NSLog(@"After placing on superview at x= 0, y = 0, width = 320, height = 320");
}

Draw2d.h

#import <UIKit/UIKit.h>

@interface Draw2d : UIView

@property (nonatomic, assign) double thickness;
@property (nonatomic, assign) double capWidth;

- (id)initWithFrame:(CGRect)frame thickness:(double)thickness capwidth:(double)capWidth;

@end

Draw2d.m

#import "Draw2d.h"

@implementation Draw2d

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}

- (id)initWithFrame:(CGRect)frame thickness:(double)thickness capwidth:(double)capWidth
{
self = [super initWithFrame:frame];
if(self)
{
// Left side is property in .h
// right side is the parameters passed into method
self.thickness = thickness;
self.capWidth = capWidth;
}
return self;
}

- (void)drawRect:(CGRect)rect
{
NSLog(@"Passed in thickness: %f", self.thickness);
NSLog(@"Passed in capWidth: %f", self.capWidth);
}


@end

控制台:

2014-04-16 09:23:23.613 Before init
2014-04-16 09:23:23.613 After init
2014-04-16 09:23:23.613 After placing on superview at x= 0, y = 0, width = 320, height = 320
2014-04-16 09:23:23.615 Passed in thickness: 15.000000
2014-04-16 09:23:23.616 Passed in capWidth: 17.500000

关于ios - 将 double 传递给 Viewcontroller,然后传递给 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23056991/

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