如何在情节提要中获取UISearchbar的结果,并在其他角度使用这些结果? [英] How do you take the results of the UISearchbar in Storyboard and use those results in another viewpoint?

查看:39
本文介绍了如何在情节提要中获取UISearchbar的结果,并在其他角度使用这些结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是一个非常简单的问题,但是很长一段时间以来一直让我感到沮丧.

Okay, this is a very simple question but it has been frustrating me for so long.

我在情节提要中创建一个视点,并将搜索栏和搜索显示控制器放在顶部.如何获取搜索栏的结果并将其存储在一个对象中,然后将该对象传递到另一个视图中?我尝试为UIViewController制作另一个类,并弄乱了"prepareforsegue"方法,但是这导致了黑屏.

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.

我有点想要这样的搜索栏: http://www.appcoda.com/how-to-add-search-bar-uitableview/

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.

推荐答案

您可以使用NSUserDefaults来存储数据并在另一个视图中使用它,理想的方法是在一个单独的类中创建它,以便您能够重用它,例如:创建一个名为Reusables的类,

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,

在Reusables.h

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

在Reusables.m

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];
}

现在在视图控制器中您要存储结果的位置导入Reusables.h,只需执行此操作即可存储搜索结果的值

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];

请注意,yourSearchResults是包含搜索结果的数据,它可以是单个值或数组

现在在视图控制器中您要使用数据导入Reusables.h的地方

Now in the view controller where u want to use the data import Reusables.h just do this

someVariableToStoreData=[Reusables getDefaultValue:@"SearchResults"];

您完成后,您可以随时使用 [可重复使用的removeDataFromDefaultsWithKey:@"SearchResults"]; 以便在需要时清除该值.

And where u are finished with it you can always use [Reusables removeDataFromDefaultsWithKey:@"SearchResults"]; to clear the value if needed.

这篇关于如何在情节提要中获取UISearchbar的结果,并在其他角度使用这些结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆