Как и в большинстве задач программирования, существует несколько решений, и это зависит от ситуации.
Вы можете настроить FirstViewController на экземпляр MyCustomClass, а затем передать его себе.
@interface FirstViewController : UIViewController {
MyCustomClass *customClass_;
}
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
customClass_ = [[MyCustomClass alloc] init]
customClass_.firstViewController = self;
}
@end
@interface MyCustomClass : NSObject {
FirstViewController *firstViewController;
}
@property (nonatomic, assign) FirstViewController *firstViewController;
@end
Таким образом, MyCustomClass может получить доступ к свойствам FirstViewController. Если вы не хотите предоставлять MyCustomClass доступ ко всем FirstViewController, вы можете использовать передачу свойств, о которых MyCustomClass должен знать.
@interface FirstViewController : UIViewController {
MyCustomClass *customClass_;
UILabel *lab;
}
@property (nonatomic, retain) IBOutlet UILabel *lab;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
customClass_ = [[MyCustomClass alloc] init]
customClass_.lab = self.lab;
}
@end
@interface MyCustomClass : NSObject {
UILabel *lab;
}
@property (nonatomic, retain) IBOutlet UILabel *lab;
@end
Вполне возможно, что FirstViewController не имеет смысла быть владельцем MyCustomClass. Возможно, это AppDelegate или какой-либо родительский контроллер, которые имеют доступ к обоим экземплярам.
@interface RandomParentClass : NSObject {
FirstViewController *firstViewController;
MyCustomClass *customClass:
}
@end
@implementation RandomParentClass
- (void)setUpController {
firstViewController = [[FirstViewController alloc] init];
customClass = [[MyCustomClass alloc] init];
customClass.lab = firstViewController.lab;
// or
customClass.firstViewController = firstViewController;
}
@end