UItextField within UISearchbar failing after iOS 7 upgrade

前端 未结 4 824
无人及你
无人及你 2020-12-18 16:11

I have a UITextField for the UISearchBar which this was working until iOS 7 upgrade and now it fails at this line: UITextField *textfield=(UITextField*)[[searchBar sub

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-18 17:08

    How about a recursive method that can trick to work in any version

    UITextField *searchBarTextField = [self findTextFieldFromControl:self.placeSearchBar];
    
    
    - (UITextField *) findTextFieldFromControl:(UIView *) view
    {
        for (UIView *subview in view.subviews)
        {
            if ([subview isKindOfClass:[UITextField class]])
            {
                return (UITextField *)subview;
            }
            else if ([subview.subviews count] > 0)
            {
                return [self findTextFieldFromControl:subview];
            }
        }
        return nil;
    }
    

提交回复
热议问题