Objective-C:访问其他UIViewControllers中的对象的问题 [英] Objective-C: Problems accessing objects in other UIViewControllers
问题描述
所以我有一个Custom UITableViewCell,它包含对其包含视图控制器的引用(包含其表的VC)。
So I have a Custom UITableViewCell that holds a reference to its containing view controller (the VC that has its table in it).
// MyCell.h
#import <UIKit/UIKit.h>
#import "RootViewController.h"
@interface MyCell : UITableViewCell
@property (nonatomic, strong) IBOutlet RootViewController *rootViewController;
-(IBAction)checkBoxClicked:(UIButton*)sender;
// MyCell.m
@implementation MyCell
@synthesize rootViewController = _rootViewController;
-(IBAction)checkBoxClicked:(UIButton*)sender
{
[self setCheckBoxChecked:!_checkBoxChecked];
[_rootViewController refreshVisibleViewForCellTagged:self.tag];
}
在我的单元格中,我有一个更改变量然后调用函数的按钮在我的rootViewController中。实际调用该方法但是当我尝试访问refreshVisibleViewForCellTagged方法内的RootViewController中的任何对象时,它们是'0x0'/ nil;
In my cell I have a button that changes a variable and then calls a function in my rootViewController. The method is actually called however when I try to access any object in the RootViewController inside of the refreshVisibleViewForCellTagged method they are are '0x0' / nil;
// RootViewController.h
@interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *myTableView;
// RootViewController.m
- (void) refreshVisibleViewForCellTagged:(NSInteger)cellTag
{
UITableView *tableView = self.myTableView; // nil
NSIndexPath *indexPath = [self.myTableView indexPathForSelectedRow]; // nil
MyCell *selectedCell = (MyCell*)[self.myTableView cellForRowAtIndexPath:indexPath]; // nil
if (selectedCell.tag == cellTag) {
NSLog(@"Refresh one way.");
} else {
NSLog(@"Do something else.");
}
}
任何人都可以解释为什么我无法访问RootController中方法'refreshVisibleViewForCellTagged'中的任何对象/变量?
Can anyone shed some light as to why I cant access any objects/variables in the RootController from within the method 'refreshVisibleViewForCellTagged'?
请谢谢!
**我的一个大问题是为什么在调用视图控制器中的方法时无法访问任何对象来自不同的视图控制器。有一些很棒的编程真相,我在这里不知道,这是一个权限问题吗?我在这个例子中没有使用@class(正向分类)。
** My big question is Why can't I access any objects when calling a method in a view controller From a different view controller. There is some great programming truth that I am not aware of here, is it a permissions issue? Im not using @class (forward classing) in this instance.
推荐答案
正如@trojanfoe所说,委托是一种更好的方法。
而不是 #importRootViewController.h
,最好采用委托。因为 UITableViewCell
是一个子节点, RootViewController
是父视图。您不希望孩子与父母直接谈论 。
As @trojanfoe said, delegation is a better way to do it.
Instead of #import "RootViewController.h"
, it is better to adop delegation. Because UITableViewCell
is a child and RootViewController
is the parent view. You don't want the child to talk directly with the parent.
-
采用委托:
To adopt delegation:
MyCell.h
档案
删除 #importRootViewController.h
。
修改 MyCell.h
如下:
@protocol MyCellDelegate; // if you need to have forward declaration
@interface MyCell : UITableViewCell
// @property (nonatomic, strong) IBOutlet RootViewController *rootViewController;
@property (nonatomic) id <MyCellDelegate> delegate;
@end
@protocol MyCellDelegate <NSObject>
- (void)refreshVisibleViewForCellTagged:(NSInteger)cellTag;
@end
- 在
MyCell.m
。 - in
MyCell.m
.
@synthesize delegate;
-(IBAction)checkBoxClicked:(UIButton*)sender {
[self setCheckBoxChecked:!_checkBoxChecked];
//[_rootViewController refreshVisibleViewForCellTagged:self.tag];
[self.delegate refreshVisibleViewForCellTagged:self.tag];
}
- in
RootViewController.h
采用MyCell授权 - in
RootViewController.h
adopt the delegation of MyCell
#import "MyCell.h"
@interface RootViewController : UIViewController <MyCellDelegate>
- in
RootViewController.m
。 - in
RootViewController.m
.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = // your implementation
//assuming all your cells are of MyCell kind
// set RootViewController as the delegate of each cell
((MyCell *)cell).delegate = self;
return cell;
}
- 实施
RootViewController.m
中的委托方法。 - implement the delegate method in
RootViewController.m
.
- (void)refreshVisibleViewForCellTagged:(NSInteger)cellTag {
// whatever you have
}
PS以上代码仅供参考。我没有运行它们。如果某些部分不起作用,请告诉我,我会修改它。
P.S. The above codes are for illustration. I didn't run them. If some part doesn't work, let me know, and I'll revise it.
RootViewController中的这些对象在你调用的方式中是nil的原因是因为您没有访问RootViewController的同一个实例。它是一个不同的(新)实例,因此所有对象都是零。
The reason those objects in RootViewController are nil in the way you call, is because you are not accessing the same instance of RootViewController. It is a different (new) instance and hence all objects are nil.
这篇关于Objective-C:访问其他UIViewControllers中的对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!