如何在两个类之间传递数据?

时间:2013-09-27 18:09:23

标签: ios objective-c uiview

我试图将使用ViewController类中的文本字段收集的用户输入传递给UIView类,以便使用drawRect方法绘制用户输入的点。我已经尝试创建一个方法,将textField中的输入传递给名为findXValue的方法,但我不确定如何在ViewControllergraphView之间共享该数据。也许覆盖这两个类的界面都可以工作,但由于我没有使用Xcode的经验,我不知道如何做到这一点。 这是我的代码:

UIView界面:

#import <UIKit/UIKit.h>

@interface graphView : UIView

@end

graphView实现:

#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界面:

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

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

@end

NSString *xValue;

ViewController实现:

#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

感谢。

来自ASA的回复后的声明: 似乎首先编译,但在运行时打破。这是代码:

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

谢谢!

1 个答案:

答案 0 :(得分:0)

我会创建一个单独的类GraphView.h(你做了)并使viewController的属性也在viewcontroller.h文件中放入此

@property (nonatomic, strong) GraphView* graphView;

并记得把它放在顶部

#import "GraphView.h"

从那里你可以拥有graphView的属性。您可以在需要时分配graphview

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

然后调用该对象的方法。 如果您不知道何时这样做,请将其放在

- (void)viewDidLoad {}

方法。现在,当您想要传递数据时,可以调用

之类的方法
[self.graphView configureWithXPoints:xpoints];

或设置属性

self.graphView.data = newData;

这将是在视图和视图控制器之间传递信息的要点。请记住,在绘制时需要绘制drawRect方法,所以当您希望它在屏幕上更新时,您必须手动调用它或将该视图重新添加到viewController。