I have a UIView
, UILabel
, and UISlider
placed in my UIVeiwController
in my ViewController
files. I have also programmatically added a UILabel
to the UIView
as a debugging tool.
Yes, I could connect to the UILabel
within the UIView
the same as I have for the other UILabel
but once I solve this problem I will be passing this value in the graph calculation. I'm open to any suggestion for passing this variable to my UIView
but would prefer to avoid creating global variables. I'm passing my UISlider
value to the UILabel
placed in my UIVeiwController
as it should but when I try to send the value by "referencing" the slider value in my GraphView.m
implementation file it never makes it and I'm getting a value of zero.
I've spent two days scouring stackoverflow forums, reviewing documentation, and re-reading books trying to figure out my issue and while I've learned a great deal (and fixed other problems) I've still not figured this one out. I'm sure its something so simple and I'm sure gonna kick myself. I welcome your references to other documentation (I've probably already read it) but please offer some constructive feedback and direction as well. My code is below:
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