2012-03-01 154 views
1

我想从我从表视图中创建的类中获取一个变量。基本上我想要这样做是告诉其他控制器哪一行被选中,所以这是我试图做的。Objective C访问其他类的变量?

表视图类.h文件中:

@property(nonatomic) NSInteger itemId; 
-(NSInteger)itemId; 

然后,我会作出这样的设置方法和获取变量表视图 类的.m文件(我合成,并做了所有的东西,我只是显示你的方法)

-(NSInteger)itemId { 
return self.itemId; 
} 

而且现在的表单元格选择的方法...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) 
indexPath { 
NSUInteger row = [indexPath row]; 
self.itemId = row; 
//segue stuff (if you want me to include this just let me know) 
} 

这就是所有该类现在,我需要 视图控制器类的.h该值类正在通过赛格瑞

@property (nonatomic) NSInteger itemId; 

视图控制器类的.m

#import "TableViewController.h" 
//Skip a few things 
@synthesize itemId; 
//skip a few things 
-(void)viewDidLoad { 
TableViewController *tvc = [[TableViewController alloc] init]; 
itemId = [tvc itemId]; 
NSLog(@"%i", itemId); 

推一些原因这不起作用...当我打印出“didselectrow”方法中的“itemId”时,它会返回正确的数字,但是当我尝试在另一个类中打印出来时,它只会给我'0' 任何想法? 如果有什么我遗漏了的,你想在我的代码中看到,我会非常乐意写出来:)我只是想通过减少代码来节省时间和空间。 在此先感谢!

编辑: 我找到了一个可能的解决方案,但它涉及到使用委托。我确信有这样做的更好的方法,所以如果你有任何想法,请让我知道。

+2

你曾经调用过你的表视图类的'itemId' getter吗?肯定会导致无限循环;你应该把它改成'return itemId'。 – yuji 2012-03-01 08:24:36

回答

0

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)未被调用 - 您必须选择一行来更改itemId的值,并且这在表的初始化和itemId = [tvc itemId];行之间是不可能的。

0

试试这个:

@property NSInteger itemId; 

希望这有助于你

0

此代码可以帮助你。

#import <UIKit/UIKit.h> 

extern int outsider; 

@interface ViewController : UIViewController { 

} 

@property int outsider; 

@end 

将所有.m文件导入到您声明变量的.h文件中,可以对其进行访问。

2
TableViewController *tvc = [[TableViewController alloc] init]; 

在创建&不同的类这样做初始化一个新的对象,所以这将是(对int 0)nill。为了在视频控制器之间共享类别&,请使用委托和属性。 我将解释一些小代码;

#import <UIKit/UIKit.h> 

    //YourClassNameAppDelegate.m 
    @interface YourClassName : UIResponder <UIApplicationDelegate> 
    { 
     NSString *uName; 
     NSDictionary *YourDictionary; 
    } 

    @property (copy, readwrite) NSString *uName; 
    @property (copy, readwrite) NSString *YourDictionary; 

And in the other class you want to use this string and dictionary, 

    //import your delegate class 
    #import "YourClassNameAppDelegate" 
    . 
    . 
    . 
    //to me, do this in viewDidLoad(or something like that) method of new view controller 
    YourClassNameAppDelegate *sharedData= (YourClassNameAppDelegate *)([[UIApplication sharedApplication]delegate]); 
    . 
    . 
    -(IBAction)sharedData{ 
     NSLog(@"Shared String: %@\n And Shared Dictionary: %@, sharedData.uName, sharedData.YourDictionary); 

    } 
0

您在您的详细视图控制器内创建表视图控制器的新实例,这是不正确的,是不会给你你的原始表的参考。

只需在您推送的视图控制器上创建一个属性,该控制器包含您想要传递的任何细节信息。然后在你的表格视图控制器中设置prepareForSegue:,其中segue.destinationViewController会给你一个指向即将出现的VC的指针。

此外,修复您的访问者方法由Yuji建议在评论中。

0

我建议保存按下行的索引路径为单例。以下是如何使用singleton class来存储对象并跨类使用它们的指南。

在这个例子中,你也可以在单例中声明一个实例变量,并在你的UITableView的相应代理方法中按下该行时进行设置。可以这样做,像这样:

#import <Foundation/Foundation.h> 
@interface Singleton : NSObject { 

NSIndexPath *tableViewPath; 

} 

#import "DataContainerSingleton.h" 
@implementation DataContainerSingleton 

@synthesize tableViewPath; 

static DataContainerSingleton* _theDataContainerSingleton = nil; 

+ (DataContainerSingleton*) theDataContainerSingleton; 
{ 
    if (!_theDataContainerSingleton) 
    _theDataContainerSingleton = [[DataContainerSingleton alloc] init]; 
    return _theDataContainerSingleton; 
} 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 

      tableViewPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    } 

    return self; 
} 

在与UITableView的视图控制器只是这样做:

@implementation 

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

tableViewPath = indexPath; 

} 

你应该调整你的初始化会是什么适合您的特定计划!

+0

当我做'tableViewPath = indexPath'我只需要导入它?或者我必须做'DataContainerSingleton * dataContainer = [[DataContainerSingleton alloc] init];并使用getter方法从该类中获取变量? – user1242108 2012-03-02 03:27:05

+0

哦,对不起,我提到。是的,这正是你必须做的。像这样:[DataContainerSingleton theDataContainerSingleton] .tableViewPath = indexPath; – wizH 2012-03-02 06:23:30