我已經搜索了類似問題的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)....
}
你能檢查 '姓名', '電話', '地址' 有任何物體或者是零? –
你可以展示你如何實例化視圖控制器。 –
首先如果你使用可可你應該使用NSTextField而不是UITextField。可可用於OSX而不是iOS。如果你使用NStextField你可以通過調用NSString * myString = [theTextField stringValue];來獲得值。 –