-1

I am trying to pass user input gathered using a text field in a ViewController class to a UIView class so as to plot user inputted points using the drawRect method. I have experimented with creating a method that passes the input from the textField to a method called findXValue, but am not sure how to share that data between ViewController and graphView. Perhaps an interface covering both classes would work, but as I am unexperienced in using Xcode I have no idea of how I might do that. Here is my code:

UIView Interface:

#import <UIKit/UIKit.h>

@interface graphView : UIView

@end

graphView implementation:

#import "graphView.h"

@implementation graphView

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

- (void)drawRect:(CGRect)rect
{
    CGContextRef c = UIGraphicsGetCurrentContext();

    CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 30,30);
    CGContextAddLineToPoint(c, 30.0f, 260.0f);
    CGContextStrokePath(c);

    CGContextRef d = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColor(d, red);
    CGContextBeginPath(d);
    CGContextMoveToPoint(d, 30,260);
    CGContextAddLineToPoint(d, 400.0f, 260.0f);
    CGContextStrokePath(d);
}

@end

ViewController interface:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *xTextField;

- (NSString *)findXValue:(NSString*)xValue;

@end

NSString *xValue;

ViewController implementation:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%@",self.xTextField.text);

    xValue = self.xTextField.text;
    [self findXValue:xValue];

    [self.xTextField resignFirstResponder];
    return YES; 
}

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

- (NSString *)findXValue:(NSString*)xValue{
    NSLog(@"%@",xValue);
    return xValue;
}


@end

Thanks.

AMEND AFTER ANSWER FROM ASA: Seemed to compile at first, but broke on runtime. Here is the code:

ViewController.h:

#import <UIKit/UIKit.h>
#import "graphView.h"

@interface ViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *xTextField;
- (NSString *)findXValue:(NSString*)xValue;

@property (nonatomic, strong) graphView* graphView1; 

@end

NSString *xValue;
NSString *finalXValue;

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%@",self.xTextField.text);

    xValue = self.xTextField.text;
    [self findXValue:xValue];

    [self.xTextField resignFirstResponder];
    return YES; 
}

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

- (NSString *)findXValue:(NSString*)xValue{
    NSLog(@"%@",xValue);
    self.graphView1 = [[graphView alloc] init];
    self.graphView1.xNum = xValue;
    NSLog(@"%@",self.graphView1.xNum);
    [self.graphView1 findX:xValue];
    /*
    graphView *graphViewPlz = [[graphView alloc] init];
    graphView.xNum.viewDidLoad;
    [self.navigationController pushViewController:graphViewPlz animated:YES];
     */
    finalXValue = xValue;
    return xValue;
}


@end

graphView.h:

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface graphView : UIView
@property(nonatomic) BOOL *isSomethingEnabled;
@property(nonatomic, strong) NSString *xNum;
@property (nonatomic, strong) UIViewController* viewController;
- (void) findX:(NSString*)xValue;
@end

NSString *xValue;

graphView.m:

#import "graphView.h"

@implementation graphView

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

- (void)drawRect:(CGRect)rect
{
    NSLog(@"%@",self.xNum);
    CGContextRef c = UIGraphicsGetCurrentContext();

    CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 30,30);
    CGContextAddLineToPoint(c, 30.0f, 260.0f);
    CGContextStrokePath(c);

    CGContextRef d = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColor(d, red);
    CGContextBeginPath(d);
    CGContextMoveToPoint(d, 30,260);
    CGContextAddLineToPoint(d, 400.0f, 260.0f);
    CGContextStrokePath(d);
}

- (void) findX:(NSString*)xValue{
    NSLog(@"%@",xValue);
}

@end

THANKS!

  • Adding to Asa Dickens answer, to update your view, when you have set your coordinates you could call setNeedsDisplay –  Sep 27 '13 at 20:43
  • "Broke on runtime" is not an adequate description of an error. – Hot Licks Sep 30 '13 at 00:25

1 Answers1

0

I would make a separate class GraphView.h (which you did) and make that a property of the viewcontroller aka with in the viewcontroller.h file put this

@property (nonatomic, strong) GraphView* graphView;

and remember to put this at the top

#import "GraphView.h"

from there you could have properties for the graphView. You could allocate the graphview when ever you need it

self.graphView = [[GraphView alloc] init];

and then call methods on that object. if you do not know when to do that, just put it with in the

- (void)viewDidLoad {}

method. Now when you want to pass data you can call methods like

[self.graphView configureWithXPoints:xpoints];

or set properties

self.graphView.data = newData;

and that would be the gist of passing information around... between a view and a view controller. Remember the drawRect method is not called until it's time to draw so when you want it to update on the screen, you have to either manually call that or re-add that view to the viewController.

A'sa Dickens
  • 2,045
  • 1
  • 16
  • 21
  • Thanks for your response! Your answer seemed to compile at first, but then when I attempted to run it I received the error message **unknown type graphView** which is weird because I have a class graphView that is imported. I'll amend the question. – MisterCurious Sep 29 '13 at 22:53
  • hmm, try restarting xCode, if that does not work, check the case sensitivity, those are my first instincts when i get those errors... simply because recently xCode has told me it couldn't find stuff... when i knew it was there.... – A'sa Dickens Sep 29 '13 at 23:13