-1

In my project, I've set a view as the root view of my app.

In the controller of this root view, I have got such a hierachy:

rootview --[addsubview]-- foregroundView --[addsubview]-- textfield

I've done that in code instead of xib.

In that case, the textfield can not be focused. It seems that nothing happened when I click the textfield on the screen. But if I modified like that :

rootview --[addsubview]-- foregroundView

rootview --[addsubview]-- textfield

Everything worked fine.

Since I wanna make foregroundView and textfield as a group, I prefer to use the former style. Do any have idea of this? Thanks very much.

Here is the core source code for my trouble:

//it's a viewController.m , it's view has been set as the root view for the app.
self.foregroundView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 140, 290, 250)]; 
UIImage *foregroundImage = [XYZImageProcessor resizeImage:[UIImage imageNamed:@"Texture0203.jpg"] toSize:CGSizeMake(275, 250)];
[self.view addSubview:self.foregroundView ];
self.nickNameTextField = [self.class customTextFieldMake:CGRectMake(80, 200, 150, 30)];
[self.view addSubview:self.nickNameTextField];
//if I change it to    [self.foregroundView addSubview:self.nickNameTextField],the textfield can not be focused.
biaobiaoqi
  • 1,098
  • 12
  • 18

2 Answers2

2

This sound as if your foregroundView has userInteractionEnabled set to NO. This doesn't just mean that foregroundView does not receive touch events - none of its subviews do either. See How to get touches when parent view has userInteractionEnabled set to NO in iOS. (Short answer: you can't.)

So, try setting [foregroundView setUserInteractionEnabled:YES] when building your views.

If foregroundView implements its own hitTest:withEvent: it could also prevent interaction with its subviews.

Community
  • 1
  • 1
Cowirrie
  • 7,218
  • 1
  • 29
  • 42
0

Try doing this in viewWillAppear:

[desiredField becomeFirstResponder];

By making the field the first responder, it have focus and the keyboard will be displayed.

Hiren
  • 12,720
  • 7
  • 52
  • 72
freelancer
  • 1,658
  • 1
  • 16
  • 37
  • It's not a good idea to call -becomeFirstResponder. The documentation for the method specifically states "Use the NSWindow makeFirstResponder: method, not this method, to make an object the first responder. Never invoke this method directly." https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html#//apple_ref/occ/cl/NSResponder – Vervious Apr 11 '12 at 03:59
  • Thanks:). becomeFirstResponder will get the keyboard, but it appears at the beginning, without my control. I still can not focus on it after quitting from the keyboard. makeFirstResponder should be invoked in a NSWindow, that may not suite my case. – biaobiaoqi Apr 11 '12 at 04:35