开发者

I want to get the value from one class (SearchTableViewController.m) to another class (HistoryTableViewController.m)

#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 !!!


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:
    • the Overview
    • the Class Reference
  2. Although the original code suggestion should work, consider using better approaches to sharing data across controllers:
    • properties in the AppDelegate, see this and this (both are answers by @TechZen)
    • Singleton class

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜