完整代码

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


我已经从第二个视图控制器以及UIView向您提供了完整的代码,希望您能看到我要去哪里。或向我解释我做错了什么。

最佳答案

我要添加另一个答案,因为先前的答案已经太长了。

杜德(Dude)我在身体上无能为力,一点也不复杂。我很抱歉,但没有在线找到有关创建对象的信息,这是不正确的,实际上它遍地都是。我在哪里学的?

2件事要注意。


命名约定表示,类以大写开头,例如draw2d必须为Draw2d
您不应该为变量xxx1,xxx2等命名。如果它们是同一事物,则将它们命名为同一事物。它们是否是不同的类无关紧要。


码:

在viewController中:

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

10-09 02:39