将 double 传递给 Viewcontroller 然后传递给 UIView [英] Pass double to Viewcontroller then to UIView

查看:31
本文介绍了将 double 传递给 Viewcontroller 然后传递给 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完整代码

视图控制器.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

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#导入

draw2D.h #import

@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

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

I have provided you with my full code from the second view controller as well as the UIView, I hope you can see where I am going wrong. Or explain to me what I'm doing wrong.

推荐答案

我正在添加另一个答案,因为上一个答案已经太长了.

I'm adding another answer as the previous is already too long.

伙计,我在身体上不能做更多的事情,它根本不复杂.很抱歉,但没有在网上找到有关创建对象的信息是不正确的,它几乎无处不在.我在哪里学的?

Dude I physically can not do any more than this, it is not complex at all. I'm sorry but not finding info online about creating an object is just not true, its quite literally everywhere. Where did I learn it?

需要注意的 2 件事.

2 things to note.

  1. 命名约定表示类以大写开头,例如draw2d 必须是 Draw2d

您不应该命名变量 xxx1、xxx2 等.如果它们是相同的,则将它们命名为相同的名称.如果它们是不同的类并不重要.

You shouldn't be naming variables xxx1, xxx2 etc. If they are the same thing, name them the same thing. It doesn't matter if they are different classes.

代码:

在视图控制器中:

- (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

这篇关于将 double 传递给 Viewcontroller 然后传递给 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆