我在UIView
文件的UILabel
中放置了UISlider
,UIVeiwController
和ViewController
。我还以编程方式将UILabel
添加到UIView
作为调试工具。
是的,我可以像连接其他UILabel
一样连接到UIView
中的UILabel
,但是一旦解决了这个问题,我将在图形计算中传递该值。我乐于接受任何有关将此变量传递给我的UIView
的建议,但宁愿避免创建全局变量。我正在将我的UISlider
值传递到应放置在UILabel
中的UIVeiwController
上,但是当我尝试通过“引用”我的GraphView.m
实现文件中的滑块值来发送该值时,它永远不会成功,并且我的值为零。
我花了两天时间搜寻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];
时正在创建一个新的视图控制器,该控件与屏幕上显示的控制器不同。该新控制器的滑块不是您要操纵的滑块,因此它永远不会更改值。
另一个问题是,每次调用GraphView
时,您的drawRect:
都会添加一个新标签。每次需要重新绘制视图时都会调用该方法。而是将标签添加为-initWithFrame:
方法中的子视图,并保留对它的引用作为实例变量或属性。
最好是完全不了解视图控制器的GraphView
。相反,GraphView
应该仅具有setter方法来设置应显示的值。每当滑块更改时,控制器应负责告知图形视图更新。它看起来像这样:
@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/