0

I'm developing a tabbar application.In the first tab a text is displaying.There is an option for saving that text.The bookmarked(saved)text can viewed in third tab.Third tab bar view is a table view.The book marked text are displayed in that tableview.So when i click each row it should return to first tab and the corresponding text is to be viewed there.i pushed the first tab using ReminderAppDelegate *theDelegate = (ReminderAppDelegate *)[[UIApplication sharedApplication] delegate]; UITabBarController *tabController = theDelegate.tabBarController;

tabController.selectedIndex = 0; But how can i view that particulat text in first view.can any one help me plz.Thanks in advance.

Senorina
  • 231
  • 6
  • 18

3 Answers3

1

When you first enter a text in the first tabbar and you want to save the text, you simply store the text in a NSString and add it to a NSMutableArray. This array could be placed in your appDelegate, so in order to save the text and store it in the array you could do:

NSString *text = [[NSString alloc] initWithString:textField.text];

ReminderAppDelegate *theDelegate = (ReminderAppDelegate *)[[UIApplication sharedApplication] delegate]; 
[theDelegate.textArray addObject:text];

Now when you want to display the array in the third tabbar view you simply use the array as a dataProvider for the table. When you press a table row, you could then get the corresponding index in your array and set the text in the first tabbar to display the text of that array index.

Man of One Way
  • 3,904
  • 1
  • 26
  • 41
  • .thanks a lot.so here we have to save and retrieve the textArray using NSUserdefaults? – Senorina Jul 29 '11 at 12:28
  • I don't understand why you want to use NSUserdefaults to anything regarding this? – Man of One Way Jul 29 '11 at 12:33
  • Yes we can use that array as source for the table. But how could i set the text in view of first tab bar from the third tab bar? – Senorina Jul 30 '11 at 06:57
  • Then you use the delegate method `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath` and see which row in the table the user selected by looking at `indexPath.row`. Then you get the text from the textArray that you want to display on the first tabbar by saying `NSString *text = [theDelegate.textArray objectAtIndex:indexPath.row];` , now you have the text, then just set the first tabbars textfield with that text. – Man of One Way Jul 30 '11 at 07:46
  • yes i understood all those and got the text in Nsstring *text.But how could i access the text view in the first tab to set this text in it from third tab.The text view is set as an outlet in first view. – Senorina Jul 30 '11 at 08:12
  • So you access the first tabbar this way `UIViewController *firstTabbar = [theDelegate.tabBarController.viewControllers objectAtIndex:0]`. Then you can cast it to the class that the first tabbar is and then access the property from there. – Man of One Way Jul 30 '11 at 09:12
0

The same way you are sharing your tab bar from the app delegate, you can have an NSString to share among the tab through the app delegate.

Or, you can use NSUserDefaults to set a property for the shared string. Trigger read/write of the property when viewDidAppear and viewDidDisappear.

Using static var is another way said by Mannan.

karim
  • 15,408
  • 7
  • 58
  • 96
  • ,We have already trid using the NSUserDefaults but in vain.Can u plz give code details of how to share a NSString among the tab. – Senorina Jul 29 '11 at 12:24
  • Are u updating the text, in view did appear and disappear of the corresponging tab bar and their view controller? did u debug that the methods are called and update the text? – karim Jul 29 '11 at 12:26
  • Senorina, please see my answer, you need to store your strings in an array in order to show all the entered strings. Then, to view one in particular, you select one from the array, by pressing a row in your table view. – Man of One Way Jul 29 '11 at 12:29
0

Try making a singleton .. something like ApplicationData ... and there you ccan make a property for every thing you want to save and pass around .

For example in tab 1. you will go [ApplicationData instance].stringToSave = @"something" and in tab 3 if you call [ApplicationData instance].stringToSave it will return @"something"

Toncean Cosmin
  • 535
  • 4
  • 19