如何在AppDelegate方法中访问UITextField

时间:2013-07-04 15:38:29

标签: ios uitextview appdelegate

您好我不知道如何从AppDelegate方法访问我的UITextField。

我试图添加

@implementation TestAppDelegate

@synthesize textField;

但我仍然收到错误。我只是想知道是否可以从AppDelegate方法访问它。

1 个答案:

答案 0 :(得分:0)

如果您的项目中有UIViewController课程,并且您正在使用界面构建器,并且想要从AppDelegate访问UITextField,那么您的项目应如下所示:

MyViewController.h

@interface MyViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *textField;
@end

MyAppDelegate.h

#include "MyViewController.h"
@interface MyAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

MyAppDelegate.m

@implementation MyAppDelegate 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    MyViewController *viewController = (MyViewController *)[_window rootViewController];
    [[viewController textField] setText:@"It should work!"]
}
@end