我想从一个类(SearchTableViewController.m)获取值到另一个类(HistoryTableViewController.m)

时间:2010-04-06 12:42:36

标签: objective-c iphone

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


} 

我的项目是TabBar。它上面有两个按钮 - 搜索和历史记录。我想在“历史记录”选项卡中的表格中显示所选项目。但我无法将所选项目从SearchTableViewController.m带到类(HistoryTableViewController.m)

问题是:我可以从SearchTableViewController.m中的表中保存数组中的一个选定项(名为deneme),但我无法将其带到HistoryTableViewController.m。 它在控制台屏幕中打印nil ....如果我可以在History类中显示它,我会在桌面上显示这些选定的项目。请帮帮我!!!

1 个答案:

答案 0 :(得分:0)

现在在didSelectRowAtIndexPath方法的HistoryTableViewController中,它正在创建一个新的SearchTableViewController实例,因此该数组将为空。要引用标签栏中的那个,请尝试以下方法:

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

上面的代码假设SearchTableViewController位于标签栏的索引0处。如果没有,请相应更改。

即使索引正确,如果SearchTableViewController尚未设置deneme,数组仍可能为nil。

<小时/> 编辑:一些建议......

  1. 请阅读(或重新阅读)Tab上的文档 条形码控制器:
  2. 虽然原始代码建议应该有效,但考虑使用更好的方法跨控制器共享数据:
  3. 这些不是共享数据的唯一两个替代方案,但相对容易实现。