获取不同视图中存在的选择器视图的选定值,并在其他视图中使用该字符串 [英] Get selected value of a picker view that is present in different view and use the string in some other view

查看:79
本文介绍了获取不同视图中存在的选择器视图的选定值,并在其他视图中使用该字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图控制器页面中有一个选择器视图,比如视图控制器1.现在我需要从另一个视图控制器中的选择器视图访问所选字符串值,例如视图控制器2.



这是因为,我有一个表格视图,显示对应4组的保存数据,即我的应用程序是提醒应用程序,用户保存提醒,在视图提醒页面中可见。提醒由以下内容保存:各个组中的用户,比如朋友,家人,办公室等...所以我早先在一个糟糕的编程环境中检索过,即



我用4个视图控制器进行显示对应于4组的提醒,即说friendsViewController用于显示使用查询对应于朋友的提醒

  NSString * getQuery = [NSString stringWithFormat:@ SELECT * FROM提醒WHERE Grp ='朋友'GROUP BY名称,事件,日期]; 

对于团体家庭,办公室和熟人来说都是明智的。将糟糕的编程环境转移到良好的状态编程习惯,我已经知道使用查询:

  NSString * getQuery = [NSString stringWithFormat:@SELECT * FROM提醒WHERE Grp ='%@'GROUP BY名称,事件,日期,**从前一个视图中的选择器中选择的字符串值**]; 

请说明我要做的改变,即。 Grp ='%@' - 该字符串是选择器视图中行组件的选定值,位于上一个视图中。



我知道有两种可能的访问方式,

  1.subclassing视图控制器即

@interface viewController2:viewController1,但我已经有了子类ie:

@interface viewController2:addReminderController,所以我不能两次子类化,我也不知道!

2.创建viewController1的实例对象,即

viewController1 * vC1我尝试访问groupPicker,如[vC1.groupPicker selectedRowInComponent:row],



但我们可以期待它抛出一个错误,即行未声明



我试图访问数组,因为我已经为groupPicker分配的行数为[ self.groupArray count],所以我试图在viewController2中使策略工作,即:

  [vC1.groupArray objectAtIndex:row ],正如预期的那样错误:**行未声明** 

那么我怎样才能访问在上一个视图中出现的选择器视图中选择的行(字符串),这样我就可以表现出良好的编程习惯,这对于像我这样的新人来说是一个好兆头:)



<请帮助我提供一些有价值的建议



提前全部谢谢:)

解决方案

第1步:在appDelegate中获取NSString变量。



第2步:在两个视图中创建appDelegate实例您希望设置值并获得以下值。

  yourApplicationDelegate * appDelegate; 

步骤3:在实施文件中初始化appDelgate,如下所示

  appDelegate = [[UIApplication sharedApplication] delegate]; 

在此语句之后,您可以使用您在应用程序的appDelegate中声明的两个视图控制器中的字符串变量。



就是这样。现在您可以从第一个视图中分配值并在第二个视图中访问它。


I have a picker view in a view controller page,say view controller1.Now I have a requirement of accessing the selected string value from that picker view in another view controller say view controller 2.

This is because,I have a table view which displays the saved data corresponding to 4 groups i.e. my application is reminder application which user saves the reminder,that is visible in view reminder page.The reminders are saved by the user in respective groups,say friends,family,office etc... So I have earlier retrieved in a bad programming context,i.e.

I took 4 view controllers for displaying reminders corresponding to 4 groups i.e. say friendsViewController for displaying reminders corresponding to friends using the query

NSString *getQuery = [NSString stringWithFormat:@"SELECT * FROM reminders WHERE Grp = 'Friends' GROUP BY Name,Event,Date"];

Like wise for group family,office and acquaintances.Hence for shifting the context of bad programming to a good programming habit,I have got an idea of using the query:

NSString *getQuery = [NSString stringWithFormat:@"SELECT * FROM reminders WHERE Grp = '%@' GROUP BY Name,Event,Date",**selected string value from the picker in previous view**];

Please identify the change I am trying to do,i.e. Grp = '%@'-The string is selected value from row component in picker view which is in previous view.

I am aware of 2 possible ways for getting access,

1.subclassing the view controller i.e. 

@interface viewController2:viewController1,but I already have subclassed i.e.:

@interface viewController2:addReminderController ,so I cant subclass twice,I am not aware too!

2. Creating an instance object of the viewController1 i.e. 

viewController1 *vC1 and I tried to access the groupPicker like [vC1.groupPicker selectedRowInComponent:row],

but as we can expect it throws an error i.e. row undeclared ,

I tried to access the array as I have assigned for number of rows in component for groupPicker as [self.groupArray count],so I tried to make the tactic work in viewController2 too i.e.:

[vC1.groupArray objectAtIndex:row],as expected the same error: **row undeclared**

So how can I get access to row(string) selected in picker view which is present in previous view,so that I can exhibit good programming habit which is a good sign in future for a fresher like me :)

Please help me with some valuable suggestions

Thanks all in advance :)

解决方案

Step 1: Take a NSString variable in your appDelegate.

Step 2: create instance of appDelegate in your both view were your want to set value and get value as following.

yourApplicationDelegate *appDelegate;

Step 3: init appDelgate in implementation file as following

appDelegate = [[UIApplication sharedApplication] delegate];

after this statement you can use the string variable from both view controller you declared in appDelegate of you application.

That's it. now you can assign value from first view and access it in second view.

这篇关于获取不同视图中存在的选择器视图的选定值,并在其他视图中使用该字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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