Using object declared in a method in another implementation in another file?
I have a tableview method:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
that declares: NSInteger row = [indexPath row];
with the above method in the file TableView.m and I want to use the value of 'row' in another file开发者_StackOverflow社区 called SecondView.m and I do not know how to allow the value of row to be used in the other file and make it public for the whole project to us it - I want to use it in an if
statement in another file.
Thanks a lot!
If the scope of the variable you want to use is limited to inside one of your methods, you cannot use it outside.
You'll have to make it a member variable and query it from an instance of the class.
@interface MyTable{
NSInteger row; //Member variable
}
@property (nonatomic, assign) NSInteger row; //Making it a property allows it to be accessed from other classes
@end
...
@implementation
@synthesize row; //Used in conjunction with @property
....
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
row = [indexPath row]; //sets the variable you want to use
...
}
....
@end
other class:
[myTable row]; //Use the variable
精彩评论