Question

I have a bizarre problem.

I have a UITableView with a UISearchBar embedded at the top. I have changed the "returnKeyType" to say Done instead of Search using the following code in the viewDidLoad of my UITableViewController:

[(UITextField *)self.timelineSearchBar setReturnKeyType: UIReturnKeyDone];
[(UITextField *)self.timelineSearchBar setEnablesReturnKeyAutomatically:NO];

That code works perfectly for my iPhone 5s. However, if I plug my iPhone 4s (running iOS 7 also), it crashes on those two lines. I put in an exception breakpoint and it lead me to that and true enough, if I comment those two out, it works on the 4s again.

With being relatively new to development, how can I go about understanding/fixing what's causing the issues here?

My 4s Storyboard is made to be identical to the 5 storyboard and this TableView and SearchBar is all created in Storyboard. I have checked and both are hooked up to the same property (timelineSearchBar).

Any thoughts would be really great.

Was it helpful?

Solution

If timelineSearchBar is indeed a property of type UISearchBar then these calls should actually fail on both devices. UISearchBar does not inherit from UITextField, and so does not implement the two methods you are trying to call. Your cast to UITextField is unsafe and is probably the source of your errors.

If you really want to change these properties of your search bar, you can look through the subviews until your find the actual UITextField, and set your properties on that. The following method will find the text field from the subviews:

-(UITextField*)findTextFieldInSubviewsRecursively:(UIView*)view
{
    if([view isKindOfClass:[UITextField class]]){
        return (UITextField*)view;
    }

    for (UIView *subView in view.subviews){
        UITextField* field = [self findTextFieldInSubviewsRecursively:subView];
        if(field != nil){
            return field;
        }
    }

    return nil;
}

Then you just need to set your values:

UITextField* searchBarTextField = [self findTextFieldInSubviewsRecursively:self.timelineSearchBar];
// Set your stuff on the text field

But this approach is probably not recommended and may not even work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top