2012-01-23 32 views
2

我已經搜索了類似問題的stackoverflow並找到了一些,但他們沒有解決我的問題。我有3個文本框(姓名,地址,電話),我想從中獲取文本。我已經在(.h)文件中聲明瞭它們,也是@property,然後在(.m)文件中將它們合成。我有一個在(.h)文件中聲明的按鈕的IBAction並正確連接。現在,當我按下這個按鈕時,我想從文本框中獲取值,但NSLog甚至在我對文本框執行任何操作之前都顯示它們全部爲空(空)。它非常簡單的代碼,我不明白爲什麼它返回null。textfield return(null)不管是什麼

//CoreDataViewController.h 
@interface coreDataViewController : UIViewController { 
    UITextField *name; 
    UITextField *address; 
    UITextField *phone; 
    UILabel *status; 
} 

@property (strong, retain) IBOutlet UITextField *name; 
@property (strong, retain) IBOutlet UITextField *address; 
@property (strong, retain) IBOutlet UITextField *phone; 
@property (strong, retain) IBOutlet UILabel *status; 

- (IBAction) saveData; 
@end 

//CoreDataViewController.m 
#import "coreDataViewController.h" 
#import "AppDelegate.h" 

@implementation coreDataViewController 
@synthesize name=_name; 
@synthesize phone=_phone; 
@synthesize address=_address; 
@synthesize status=_status; 

- (void) saveData 
{ 
    NSLog(@"Name %@ Address %@ Phone %@",self.name.text,self.address.text,self.phone.text); 

...some more code (commented out).... 
} 
+3

你能檢查 '姓名', '電話', '地址' 有任何物體或者是零? –

+1

你可以展示你如何實例化視圖控制器。 –

+1

首先如果你使用可可你應該使用NSTextField而不是UITextField。可可用於OSX而不是iOS。如果你使用NStextField你可以通過調用NSString * myString = [theTextField stringValue];來獲得值。 –

回答

0

IMO您錯誤地合成了高德。
您使用的語法:

synthesize name=_name; 

但在.h文件中您有:

@interface coreDataViewController : UIViewController { 
    UITextField *name; 
    UITextField *address; 
    UITextField *phone; 
    UILabel *status; 
} 

更改此部分:

@interface coreDataViewController : UIViewController { 
    UITextField *_name; 
    UITextField *_address; 
    UITextField *_phone; 
    UILabel *_status; 
} 
+0

將修復並報告回來。 thnx –

+0

如果我只是像你說的那樣改變(.h)文件,它會破壞其他所有內容。 –

+0

你是什麼意思,說:「打破一切」? –

0

只是下面的代碼更改

寫像這樣合成

@synthesize name; 
@synthesize phone; 
@synthesize address; 
@synthesize status; 

ratherthen

@synthesize name=_name; 
@synthesize phone=_phone; 
@synthesize address=_address; 
@synthesize status=_status; 
+0

就是這樣,當它也沒有工作。將再次嘗試壽。 –

+0

這兩組合成指令都是有效的,後一組的優點是可以防止您在指定屬性時意外引用實例變量。 –

+0

好的,感謝您的指導。 –

相關問題