简体   繁体   中英

ComboBox doesn't work when in a UIScrollView

I have set up this combo box and it works great. Now I have a scene that needs to scroll down and I have implemented a scroll view that takes up the entire scene. I have 4 of these combo boxes on the scene and I can't get them working properly. In my ViewController.m if I change the "self.view addSubview" to "self.theScroller addSubView" I can get the ComboBox to load on the scroller, when clicked it throws up the normal keyboard and doesn't load the UIPickerView.

 NSMutableArray* fieldTeamsArray = [[NSMutableArray alloc] init];
[fieldTeamsArray addObject:@"Field Team 1"];
[fieldTeamsArray addObject:@"Field Team 2"];
[fieldTeamsArray addObject:@"Field Team 3"];

fieldTeams = [[ComboBox alloc] init];
[fieldTeams setComboData:fieldTeamsArray];          
[self.view addSubview:fieldTeams.view];  //UIPicker works but not on the Scroll View
fieldTeams.view.frame = CGRectMake(20, 135, 275, 30);      //ComboBox location 


NSMutableArray* typesArray = [[NSMutableArray alloc] init];
[typesArray addObject:@"type 1"];
[typesArray addObject:@"type 2"];
[typesArray addObject:@"type 3"];

types = [[ComboBox alloc] init];
[types setComboData:typesArray];          
[self.theScroller addSubview:types.view];   //ComboBox on Scroll View but no UIPicker
types.view.frame = CGRectMake(20, 187, 275, 30); 

I don't know how to get the "types" Combo box to be able to access and properly load the UIPicker while in the scroller.

It was a (small) challenge but I think I've found a solution.

Go in the ComboBox.h and make it conforms to UITextFieldDelegate.

@interface ComboBox : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>

Then go in the ComboBox.xib and make the File's Owner the delegate property of the textField.

And finally, add in the ComboBox.m the delegate method following :

- (BOOL)textFieldShouldBeginEditing:(UITextField *)aTextField
{
    [self showPicker:aTextField];
    return YES;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM