【发布时间】:2012-01-23 08:20:28
【问题描述】:
我在 stackoverflow 上搜索过类似的问题并找到了一些,但它们并没有解决我的问题。我有 3 个文本字段(姓名、地址、电话),我想从中获取文本。我已经在 (.h) 文件以及@property 中声明了它们,然后在 (.m) 文件中 @synthesized 它们。我有一个在 (.h) 文件中声明并正确链接的按钮的 IBAction。现在,当我按下此按钮时,我想从文本字段中获取值,但 NSLog 显示它们都是(null),甚至在我对文本字段进行任何操作之前。它的代码非常简单,我不明白为什么它返回 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)....
}
【问题讨论】:
-
你能检查一下'name'、'phone'、'address'有什么对象还是没有?
-
你能展示你如何实例化视图控制器吗?
-
首先如果你使用 cocoa 你应该使用 NSTextField 而不是 UITextField。可可适用于 OSX 而不是 iOS。如果你使用 NStextField 你可以通过调用 NSString *myString = [theTextField stringValue];
-
@GirishKolari 我该如何检查?
-
而且你不需要self.name你可以调用name,因为你用_name合成,所以你的NSLog应该是这样的: NSLog(@"Name %@ Address %@ Phone %@",姓名.文本,地址.文本,电话.文本);
标签: objective-c cocoa-touch uikit null uitextfield