如何初始化一个UITextField的内容,让其默认显示一些文字

我创建了一个single View的工程,然后在xib上拖了一个UITextField,我想在这个视图显示时,文本框里能够默认显示些文字,不知道该怎么办。我是这样做的,但是就是文本框里面什么也不显示,以下是我的代码,不知道哪里出问题了,我也做了控件关联
// TestViewController.h
#import

@interface TestViewController : UIViewController
{
__weak IBOutlet UITextField *field;
}

  • (void)setField:(NSString *)str; @end

// TestViewController.m

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

  • (void)viewDidLoad
    {
    [super viewDidLoad];

    [self setField:@"hhhh"];
    }

  • (void)setField:(NSString *)str
    {
    field.text = str;
    }
    @end

// TestAppDelegate.m
#import "TestAppDelegate.h"
#import "TestViewController.h"
@implementation TestAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    TestViewController *tvc = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];

    [tvc setField:@"hhhh"];

    self.window.rootViewController = tvc;

    [self.window makeKeyAndVisible];

    return YES;
    }

我是一个菜鸟, 不过对于你的问题我个人理解是这样的,说的不好别骂街,别托.

1,UITextFileld一般都是用强引用或者不声明__weak, __strong让他默认装态.
2.如果单纯想给textField一个默认值的话可以使用textField.placeholder = @"默认显示字符";(这种默认值编辑时候消失) 或者 textField.text=@"显示字符";

我按照你的代码实验 了下:
1,如果使用__weak IBOutlet UITextField *field; 无法然后使用set函数赋值, 也不能在delegate里边调用并赋值,

2,若果使用@property (weak,nonatomic)IBOutlet UITextField *textField;(不能使用set函数)
可以在代理里边调用, 你的那个写法赋值可定会失败的, 因为你写到了rootViewController上边,也就是你的View还没生命
self.window.rootViewController = tvc;
[self.window makeKeyAndVisible];
[tvc setField:@"hhhh"];这样就会显示了;