我正在尝试将使用 ViewController 类中的文本字段收集的用户输入传递给 UIView 类,以便使用 drawRect 方法绘制用户输入的点。我已经尝试创建一个方法,将输入从 textField 传递到一个名为 的方法findXValue
,但我不确定如何在ViewController
和之间共享该数据graphView
。也许涵盖这两个类的接口可以工作,但是由于我没有使用 Xcode 的经验,所以我不知道如何做到这一点。这是我的代码:
UIView 界面:
#import <UIKit/UIKit.h>
@interface graphView : UIView
@end
图视图实现:
#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
视图控制器接口:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *xTextField;
- (NSString *)findXValue:(NSString*)xValue;
@end
NSString *xValue;
视图控制器实现:
#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 回答后修改:起初似乎可以编译,但在运行时中断。这是代码:
视图控制器.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;
视图控制器.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
图视图.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;
图视图.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
谢谢!