我有一个UIView
, UILabel
, 并UISlider
放在UIVeiwController
我的ViewController
文件中。我还以编程方式添加了一个UILabel
作为UIView
调试工具。
是的,我可以连接到与另一个相同的UILabel
内部,但是一旦我解决了这个问题,我将在图形计算中传递这个值。我愿意接受将这个变量传递给我的任何建议,但更愿意避免创建全局变量。我将我的值传递给我应该放置的值,但是当我尝试通过“引用”我的实现文件中的滑块值来发送值时,它永远不会成功,我得到的值为零。UIView
UILabel
UIView
UISlider
UILabel
UIVeiwController
GraphView.m
我花了两天时间搜索 stackoverflow 论坛、查看文档和重新阅读书籍,试图找出我的问题,虽然我学到了很多东西(并解决了其他问题),但我仍然没有弄清楚这一点。我敢肯定它是如此简单,我肯定会踢自己。我欢迎您参考其他文档(我可能已经阅读过),但也请提供一些建设性的反馈和指导。我的代码如下:
视图控制器.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
视图控制器.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
图形视图.h
#import <UIKit/UIKit.h>
@interface GraphView : UIView
@end
图形视图.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