Get selected value of a picker view that is presen

2019-01-20 19:18发布

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 :)

2条回答
ゆ 、 Hurt°
2楼-- · 2019-01-20 19:37

You can declare NSString in Secondview globally and it is better to take a button and in that action You have to write

Take a NSString *selectedString globally in firest view in Pickerview delegate method

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   selectedString=    [arrayNo objectAtIndex:row];

//here array is the array used for pickerview 
}

-(IBAction)btnAction:(id)sender
{
SecondView *sec=[[SecondView alloc]initWithNibname:@"SecondView" bundle:nil];
sec.stringFromPrevView=selectedString;
[self presentModalViewController:sec animated:YES];
[sec release];
}

stringFromPrevView this string will be use in Your SecondView as per Your requirement

I hope this will helps You

查看更多
我命由我不由天
3楼-- · 2019-01-20 19:57

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.

查看更多
登录 后发表回答