views:

155

answers:

1
#import <UIKit/UIKit.h>
@class SearchDetailViewController;

@interface SearchTableViewController : UITableViewController 
<UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>{

 IBOutlet UITableView *myTableView;
 NSMutableArray *tableData;//will be storing data that will be displayed in table. //Search array den buna aktarma yapcaz ilerde görceksin.
 NSMutableArray *searchedData;//will be storing data matching with the search string
 UISearchBar *sBar;//search bar

 NSMutableArray *searchArray; // It holds the medicines that are shown in tableview
 SearchDetailViewController * searchDetailViewController;

 NSMutableArray *deneme;


}

@property(nonatomic,retain)UISearchBar *sBar;
@property(nonatomic,retain)IBOutlet UITableView *myTableView;
@property(nonatomic,retain)NSMutableArray *tableData;
@property(nonatomic,retain)NSMutableArray *searchedData;
@property (nonatomic, retain) NSMutableArray *searchArray;
@property (nonatomic, retain) SearchDetailViewController *searchDetailViewController;


@property (nonatomic, copy) NSMutableArray *deneme;
@end

SearchTableViewController.m


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
 // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
 // [self.navigationController pushViewController:anotherViewController];
 // [anotherViewController release];


 **deneme= [[NSMutableArray alloc]init];
 deneme=[tableData objectAtIndex:indexPath.row];**

 ****NSLog(@"my row = %@", deneme);**// I holded one of the selected cells here**

HistoryTableViewController.m


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
 // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
 // [self.navigationController pushViewController:anotherViewController];
 // [anotherViewController release];

 **SearchTableViewController *obj= [[SearchTableViewController alloc]init];**
 **NSLog(@"my 2nd row= %@", [obj deneme]); //it prints nil**


} 

My project is TabBar. There are two buttons on it- Search and History. I want to display selected items in a table in History tab. But i can not bring the selected item from SearchTableViewController.m to the class (HistoryTableViewController.m)

The problem is : I can hold one of the selected items in an array (named deneme)from table in SearchTableViewController.m but i can not take it to HistoryTableViewController.m. It prints nil in console screen.... If I can make it visible in History class, I display those selected items on table. Please help me !!!

A: 

Right now in HistoryTableViewController in the didSelectRowAtIndexPath method, it is creating a new instance of SearchTableViewController so the array will be empty. To reference the one in the tab bar, try this:

SearchTableViewController *obj = (SearchTableViewController *)[self.tabBarController.viewControllers objectAtIndex:0];
NSLog(@"my 2nd row= %@", [obj deneme]);

Above code assumes SearchTableViewController is at index 0 in your tab bar. If not, change it accordingly.

Even with the correct index, the array might still be nil if deneme has not yet been set by the SearchTableViewController.


Edit: Some advice...

  1. Please read (or re-read) the docs on Tab Bar Controllers:
  2. Although the original code suggestion should work, consider using better approaches to sharing data across controllers:

Those aren't the only two alternates for sharing data but are relatively easy to implement.

DyingCactus
I can catch the row touched by the user in SearchTableViewController.m (already NSLog proves it, i can see that selected item name in console in SearchTableViewController.m) How can i take that value to HistoryTableViewController.m. I think this is more explanatory ... thanx
Replace how you are setting obj in didSelectRowAtIndexPath in HistoryTableViewController with the example I provided. Don't alloc+init it, instead get a reference to it from the tabBarController.
DyingCactus
if i send you my project, could you help me ??
@ahmet732: What happens when you change the code in HistoryTableViewController as shown above?
DyingCactus
It throws an exception when I click on History tab button .(I put the code you wrote in ViewDidLoad to see the change) and I could not specify the number in objectAtIndex. How can I find that? Is it ObjectID in Inspector? I want to sum up my question : Is there any way to access any item in Table View from seperate class?
No not the ObjectID. Will edit answer shortly with more advice.
DyingCactus
ı did read your advices thank you very much.. I think objectAtIndex:0 is fine but it throws exception :
2010-04-08 17:13:20.120 MaSystemGui[2216:20b] *** -[SearchNavController deneme]: unrecognized selector sent to instance 0x3d1d6b02010-04-08 17:13:20.121 MaSystemGui[2216:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[SearchNavController deneme]: unrecognized selector sent to instance 0x3d1d6b0'2010-04-08 17:13:20.123 MaSystemGui[2216:20b] Stack: (
Why does it say SearchNavController? Isn't it supposed to be SearchTableViewController?
DyingCactus
When I clicked on the item in Tableview, it navigates to another view and turn back to table view when i clicked on search button on navigation controller.. I replaced SearchTableViewController with SearchNavController (while creating obj thing) but it throws the same exception.. I am indeed going crazy :) it is my senior project. It must be completed till last day of April.. You helped me lot thanx for that..
The error means that SearchNavController does not have the property deneme like SearchTableViewController does.
DyingCactus
I will take "deneme" property to SearchNavController.h .. What will happen next we will see... But I CAN'T. I am holding the value of on cell in SearchTableViewController.m. It is useless if I take it inside SearchNavController.m.
I suggest you use one of the alternate methods listed in the edited answer. But first, clearly plan out your app design rather than making random changes.
DyingCactus