【问题标题】:Passing float value from ViewController to some Class将浮点值从 ViewController 传递给某个类
【发布时间】:2012-02-01 23:29:44
【问题描述】:

我是 Objective-c 编程的新手,但我卡住了。 我的视图控制器中有uislider,视图控制器的头文件中有浮动变量。 我还有一个新类,它改变了我的第二个视图的外观,我覆盖了 drawRect。

我只是获取 slider.value 的值并将其保存在我的 float 变量中,然后我尝试在我的类中访问 float 变量,因为这个变量定义了我要绘制的线条的宽度。

我的视图控制器的头文件

@interface ViewController : UIViewController 
{
float lineWidth;
}

@property float lineWidth;
-(float)changingline;
@end

viecontroller.m //sliderForLine = UISlider

- (IBAction)valuechangedForLine:(UISlider *)sender 
{

float sliderValue = sliderForLine.value;
lineWidth = sliderValue;
[self changingline];

}
-(float)changingline
{   
NSLog(@"%f", lineWidth);

return lineWidth;
} 

NSLog 说 lineWidth 有一些值

指定第二个 UIView 外观的类

#import "ViewController.h"

ViewController *oldView = [[ViewController alloc] init];
CGContextSetLineWidth(ctx, oldView.lineWidth);
NSLog(@"%f",oldView.lineWidth);

在这里,在第二类中,NSLog 总是说 0。

我很困惑,我现在什至不知道我为什么要使用 -(void)changeling :)

我只知道我错过了如何在类之间传递值的要点,有人可以解释我做错了什么吗?

提前致谢:]

【问题讨论】:

  • 您能否更具体地说明如何将浮点值从一个类传递到另一个类。我似乎无法理解您的设置。
  • 您正在创建一个全新的ViewController 对象,因此从未为该实例设置浮点值。您需要保留对现有视图控制器的引用或创建协议和相应的方法以将值传递给委托。

标签: iphone objective-c xcode ipad


【解决方案1】:

为此,您需要(不想要)一个代表。试试这样的:

我的视图控制器的头文件

@protocol ViewControllerDelegate <NSObject>

@required

-(void)valueChanged:(float)value;

@end

@interface ViewController : UIViewController 
{
float lineWidth;
id<ViewControllerDelegate> _delegate;
}

@property float lineWidth;
@property (nonatomic, assign) id<ViewControllerDelegate> delegate;
-(float)changingline;
@end

//.m

@synthesize delegate = _delegate;
...

- (IBAction)valuechangedForLine:(UISlider *)sender 
{

float sliderValue = sliderForLine.value;
lineWidth = sliderValue;
[self.delegate valueChanged:lineWidth];

}

那么你需要做的就是用一对这些符合协议并实现所需的方法。

另外,使用下划线重命名您的 iVar(因此,使用 _lineWidth 代替 lineWidth)然后 @synthesize lineWidth = _lineWidth

【讨论】:

  • //.h #import "firstViewController.h" @interface SecondView : UIView &lt;ViewControllerDelegate&gt; { float line; } @property float line; //.m #import "firstViewController.h" @synthesize line; -(void)valueChanged:(float)value { line = value; } - (void)drawRect:(CGRect)rect { CGContextSetLineWidth(ctx, line); NSLog(@"%f",line); } nslog 说 0,我按照您的建议更改了我的 firstViewController,但似乎仍然无法正常工作。为什么我的 iVar 应该使用下划线?我没有回复,因为我必须找出协议是什么,因为我从 Objective-c 开始。
  • 添加[查看setNeedsDisplay];在你的 valuechanged 方法中。
【解决方案2】:

在您的问题中,您描述了一个类ViewController,它是 UIViewController 的子类。您显示接口 (.h) 并显示来自实现 (.m) 的两个方法。您将显示分配和初始化类的新对象 (oldView) 的代码。但是,您没有展示一个新的类对象是如何初始化的(您没有展示它的 init 方法)。

lineWidth 是对象 oldView 的一个属性,在这些行中初始化 oldView 后立即引用它:

ViewController *oldView = [[ViewController alloc] init]; 
CGContextSetLineWidth(ctx, oldView.lineWidth); 
NSLog(@"%f",oldView.lineWidth); 

然而,你没有在哪里显示 lineWidth 的值是在哪里初始化的。也许你没有初始化它。也许- (IBAction)valuechangedForLine:(UISlider *)sender 没有被调用。会不会是你的问题?

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 2010-12-11
    • 1970-01-01
    相关资源
    最近更新 更多