问题
Okay, this is a very simple question but it has been frustrating me for so long.
I create a viewpoint in the Storyboard and put a search bar and search display controller on the top. How do I take the results of the search bar and store it in an object and pass that object into a different view? I tried making another class for UIViewController and messing with the "prepareforsegue" method, but that resulted in a black screen.
I kind of want a search bar like this: http://www.appcoda.com/how-to-add-search-bar-uitableview/
But instead of searching through a table view, I want to use the search results that the user types and pass it to another view after using it in another object (to get results from the internet).
Can anyone please help me out and give me some guidance on this matter? Thank you very much.
回答1:
You can user NSUserDefaults to store the data and use it in another view, the ideal way would be to create it in a separate class so that you would be able to reuse it, for eg: Create a class called Reusables,
In Reusables.h
#import <Foundation/Foundation.h>
@interface Reusables : NSObject
+(void)storeDataToDefaults :(NSString *)keyName objectToAdd:(id)data;
+(NSString *)getDefaultValue:(NSString *)key;
+(void)removeDataFromDefaultsWithKey:(NSString *)keyName;
@end
In Reusables.m
#import "Reusables.h"
@implementation Reusables
+(void)storeDataToDefaults :(NSString *)keyName objectToAdd:(id)data
{
if (![data isKindOfClass:[NSNull class]])
{
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setValue:data forKey:keyName];
[defaults synchronize];
}
}
+(NSObject *)getDefaultValue:(NSString *)key
{
NSObject *value=[[NSUserDefaults standardUserDefaults] valueForKey:key];
return value;
}
+(void)removeDataFromDefaultsWithKey:(NSString *)keyName
{
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
if ([defaults valueForKey:keyName])
[defaults removeObjectForKey:keyName];
[defaults synchronize];
}
Now in the view controller where u want to store results import Reusables.h and just do this to store the value of search results
[Reusables storeDataToDefaults:@"SearchResults" objectToAdd:yourSearchResults];
Note that yourSearchResults is the data that contain your search result it can be a single value or array
Now in the view controller where u want to use the data import Reusables.h just do this
someVariableToStoreData=[Reusables getDefaultValue:@"SearchResults"];
And where u are finished with it you can always use
[Reusables removeDataFromDefaultsWithKey:@"SearchResults"]; to clear the value if needed.
回答2:
You need to set your ViewController as the delegate of the searchBar, and then implement the delegate methods to actually do the search/pass the information onward. Have a look at the apple documentation here.
The methods you want to implement are:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; // return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar; // called when text starts editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; // return NO to not resign first responder
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; // called when text ends editing
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText; // called when text changes (including clear)
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0); // called before text changes
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; // called when keyboard search button pressed
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar; // called when bookmark button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar; // called when cancel button pressed
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2); // called when search results button pressed
by using searchBar.text you can get the current user input.
来源:https://stackoverflow.com/questions/20040488/how-do-you-take-the-results-of-the-uisearchbar-in-storyboard-and-use-those-resul