2013-09-27 35 views
-1

我想將使用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 

視圖控制器接口:

#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 

謝謝。

AMEND從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 

謝謝!

+0

添加到Asa Dickens的答案,以更新您的視圖,當您設置座標時,您可以調用setNeedsDisplay – 2013-09-27 20:43:50

+0

「Broke on runtime」不是對錯誤的充分描述。 –

回答

0

我會做一個獨立的類GraphView.h(你沒有),使該視圖 - 控制又名同在viewcontroller.h文件的屬性把這個

@property (nonatomic, strong) GraphView* graphView; 

,記得把這個在頂部

#import "GraphView.h" 

從那裏你可以有graphView的屬性。您可以在需要時分配圖形視圖

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

然後調用該對象上的方法。 ,如果你不知道什麼時候才能做到這一點,只要把它用在

- (void)viewDidLoad {} 

方法。現在,當你想通過數據可以調用的方法,如

[self.graphView configureWithXPoints:xpoints]; 

或設置屬性

self.graphView.data = newData; 

,這將是各地傳遞信息......一個視圖,視圖控制器之間的要點。記住drawRect方法直到需要在屏幕上更新時纔會調用,您必須手動調用該方法或將該視圖重新添加到viewController。

+0

感謝您的回覆!你的答案似乎首先編譯,但後來當我試圖運行它時,我收到了錯誤消息**未知類型graphView **這很奇怪,因爲我有一個導入的類graphView。我會修改這個問題。 – MisterCurious

+0

嗯,嘗試重新啓動xCode,如果這不起作用,請檢查區分大小寫,這些是我第一次直覺,當我得到這些錯誤...只是因爲最近xCode告訴我它找不到東西......當我知道它在那裏.... –

相關問題