與大多數編程任務一樣,有幾種解決方案,它取決於具體情況。
您可以讓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
感謝回覆,我在我的單身的應用程序有一些方法(以asihttprequest相關),因爲我開始請求爲asychronus並在requestfinished委託我需要一些信息的生命過程中如何去喜歡它在step1中,step1通過或step1failed我想顯示它們在視圖控制器之一。我試圖像分配一些變量的變化,然後使用共享方法將它們提交給我的視圖控制器,但它沒有即時抓取數據,因爲我不知道需要多長時間才能請求完成刪除操作並執行下一步操作,我認爲使用而當(1) – appleDE 2010-11-08 16:26:41
繼續:..當這些變量改變並在這裏顯示時觸發,但我認爲它的簡單和骯髒的想法,這就是爲什麼我在其他地方嘗試。在穩定性或速度方面從singleton類訪問UI元素有什麼問題嗎? – appleDE 2010-11-08 16:28:30