ios - 尝试使用自定义类将值传递给 UIView 时,UISlider 值为零

标签 ios objective-c cocoa-touch uiview

我在 ViewController 的 UIVeiwController 中放置了 UIViewUILabelUISlider 文件。我还以编程方式将 UILabel 添加到 UIView 作为调试工具。

是的,我可以连接到 UIView 中的 UILabel,就像连接其他 UILabel 一样,但是一旦我解决了这个问题我将在图形计算中传递该值。我愿意接受任何将此变量传递给我的 UIView 的建议,但更愿意避免创建全局变量。我将我的 UISlider 值传递给放置在我的 UIVeiwController 中的 UILabel,但当我尝试通过 发送该值时在我的 GraphView.m 实现文件中“引用” slider 值,它永远不会成功,并且我得到的值为零。

我花了两天时间搜索 stackoverflow 论坛、查看文档并重新阅读书籍,试图找出我的问题,虽然我学到了很多东西(并解决了其他问题),但我仍然没有弄清楚这个问题出去。我确信事情是如此简单,我肯定会踢自己一脚。我欢迎您引用其他文档(我可能已经阅读过),但也请提供一些建设性的反馈和方向。我的代码如下:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSString *viewControllerSliderValue;
}
- (NSString *) viewConrtollerSliderValue;

@property (weak, nonatomic) IBOutlet UILabel *sliderValueLabel;

@property (weak, nonatomic) IBOutlet UISlider *sliderValue;

- (IBAction)sliderChange:(UISlider *)sender;

@end

ViewController.m

#import "ViewController.h"
#import "GraphView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    float sliderVal = self.sliderValue.value;
    self.sliderValueLabel.text = [NSString stringWithFormat:@"%.1f",sliderVal];
    NSLog(@"viewDidLoad: sliderVal = %f", sliderVal);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sliderChange:(UISlider *)sender
{
    float sliderVal = sender.value;
    self.sliderValueLabel.text = [NSString stringWithFormat:@"%.1f", sliderVal];
    NSLog(@"sliderChange: sliderVal = %f", sliderVal);

}

- (NSString *)viewConrtollerSliderValue
{
    float sliderVal = self.sliderValue.value;
    viewControllerSliderValue = [NSString stringWithFormat:@"%f",[self sliderValue].value];
    NSLog(@"sliderChange: sliderVal = %f", sliderVal);
    return viewControllerSliderValue;
}

@end

GraphView.h

#import <UIKit/UIKit.h>

@interface GraphView : UIView

@end

GraphView.m

#import "GraphView.h"
#import "ViewController.h"

@implementation GraphView

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

- (void)drawRect:(CGRect)rect
{
    ViewController *viewController = [[ViewController alloc] init];

    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 90, 140, 20)];
    NSLog(@"GraphView: drawRect: vierContrllerSliderValue = %@", [viewController viewConrtollerSliderValue]);
    myLabel.text = [viewController viewConrtollerSliderValue];
    myLabel.backgroundColor = [UIColor lightGrayColor];
    myLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:myLabel];
}

@end

最佳答案

问题是你的GraphView在调用ViewController *viewController = [[ViewController alloc] init];时正在创建一个新的 View Controller ,它与屏幕上显示一个。新 Controller 的 slider 不是您正在操作的 slider ,因此它永远不会更改值。

另一个问题是,每次调用 drawRect: 时,GraphView 都会添加一个新标签。每次需要重新绘制 View 时都会调用该方法。相反,请将标签添加为 -initWithFrame: 方法中的 subview ,并将对其的引用保留为实例变量或属性。

GraphView 最好永远不知道 View Controller 。相反,GraphView 应该只具有一个 setter 方法来设置应显示的值。 Controller 应该负责告诉图形 View 在 slider 发生变化时进行更新。它看起来像这样:

@interface GraphView
@property (nonatomic) float graphValue;
@end

@implementation GraphView {
    UILabel *graphLabel;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        graphLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 90, 140, 20)];
        graphLabel.backgroundColor = [UIColor lightGrayColor];
        graphLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:graphLabel];
    }
    return self;
}


- (void)setGraphValue:(float)theValue {
    _graphValue = theValue;
    graphLabel.text = [NSString stringWithFormat:@"%f", theValue]
}

// Note; no need for -drawRect, because it will all be drawn by the label, which is a subview.

@end

关于ios - 尝试使用自定义类将值传递给 UIView 时,UISlider 值为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16662197/

相关文章:

ios - UINavigationBar 在 presentViewController 上消失 :animated:completion

ios - 在 iOS 中单击 Detail Button 从 UITableView 获取相同的记录

ios - Swift iOS 10 的推送通知

objective-c - DST 的时区问题(神秘的缺失两个小时)

iPhone App如何以编程方式检测 ScrollView 的滚动?

ios - 自动调整 UITableView 的 header

ios - 存储自定义类 iOS 8 核心数据

iphone - 如何构建类似 iPad App 的终端

objective-c - iOS UIViews 的交替颜色(如在表格行中使用时)

ios - iOS 6 上被 EventKit 框架欺负