无法在xcode的文本字段中设置焦点?

时间:2012-10-26 06:35:38

标签: iphone xcode4 uitextfield

我是Objective-C的新手并且在某一点上受到了打击..

我创建了一个基于View的应用程序,并将tabbarController(拖放)放到mainwindow.xib上...因为它是一个ViewBasedApplication所以我的ViewController直接从AppDelegate启动。我在ViewController中有一个UItextField ..现在当app启动我可以看到带有两个tabbar项的UItextField,TabBar。但是当我触摸UItextField并尝试输入一些字符时,我无法看到UITextField上的光标,而我无法输入任何字符。 字符..所以当我触摸它时,我希望UItextField响应..

我该怎么办?

2 个答案:

答案 0 :(得分:0)

App委托文件:

 // Create instance of UINavigationController
    UINavigationController *myNavigationController;
    // Create initialized instance of UITabBarController
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    // Create initialized instance of NSMutableArray to hold our UINavigationControllers
    NSMutableArray *tabs = [[NSMutableArray alloc] init];

    // Create first UIViewController
    UIViewController *myFirstViewController = [[UIViewController alloc] init];
    [myFirstViewController setTitle:@"First"];
    // Initialize the UINavigationController
    myNavigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
    // Add UINavigationController to you tabs
    [tabs addObject:myNavigationController];
    // Release UIViewController and UINavigationController
    [myFirstViewController release], [myNavigationController release];

    // Create second UIViewController
    UIViewController *mySecondViewController = [[UIViewController alloc] init];
    [mySecondViewController setTitle:@"Second"];
    // Initialize the UINavigationController
    myNavigationController = [[UINavigationController alloc] initWithRootViewController:mySecondViewController];
    // Add UINavigationController to you tabs
    [tabs addObject:myNavigationController];
    // Release UIViewController and UINavigationController
    [mySecondViewController release], [myNavigationController release];

    // Add the tabs to the UITabBarController
    [tabBarController setViewControllers:tabs];
    // Add the view of the UITabBarController to the window
    self.window.rootViewController = tabBarController;

[self.window makeKeyAndVisible];

ViewController.h文件:

  • 在.h档案中
  

UITextField * textfld;

  • 在.m文件中:

viewDidLoad方法:

    textfld = [[UITextField alloc] initWithFrame:<Frame>]; 
    textfld.delegate = self; 
    [textfld becameFirstResponder];

使用

创建.h文件textfielddelegate
 @interface YourClassName:UIViewController <UITextFieldDelegate>.

希望它适合你。

答案 1 :(得分:0)

尝试使用此

[textfld setUserInteractionEnabled:YES];