【问题标题】:how to access UITextField in AppDelegate method [closed]如何在 AppDelegate 方法中访问 UITextField [关闭]
【发布时间】:2013-07-04 15:38:28
【问题描述】:

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

我已经尝试添加

@implementation TestAppDelegate

@synthesize textField;

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

【问题讨论】:

  • ...你得到什么错误?
  • 您不应该在 AppDelegate 中访问类似的内容。
  • 如果你努力写出更好的问题,你可以希望得到答案并解决你的问题。

标签: ios uitextview appdelegate


【解决方案1】:

如果您的项目中有 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 2017-11-25
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    相关资源
    最近更新 更多