在我的ViewDidLoad方法我的主要視圖控制器我編程方式創建一個分段控制,然後將其添加到我的導航控件的titleView。如果我在viewDidLoad中以編程方式添加控件,如何從其他方法訪問它?
NSArray *seggieItems = [[NSArray alloc] initWithObjects:@"See Entire List",@"See Near Me", nil];
UISegmentedControl *seggie = [[UISegmentedControl alloc]initWithItems:seggieItems];
[seggie setSegmentedControlStyle:UISegmentedControlStyleBar];
[seggie setTintColor:[UIColor redColor]];
[seggie addTarget:self
action:@selector(seggieChanged:)
forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = seggie;
然而,當我想從另一個方法,如viewWillAppear中訪問此分段控制,我不能 - 這是一個未聲明的標識符。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[seggie setSelectedSegmentIndex:0]; //this should always be 0 for this view controller viewDidLoad method
}
如果我使用的是廈門國際銀行,這將是沒有問題的,因爲我有一個IBOutlet我可以從任何地方使用。我試着爲我的Segmented控件創建一個屬性,但是實例變量隱藏了局部變量,所以這必須是錯誤的方法。
這是什麼最好的方法呢?如果這是一個愚蠢的問題,我很抱歉 - 我對iOS相當陌生,目前大約有一兩本書的一半。我無法在Google上找到答案或搜索StackOverflow。我會很感激任何幫助,如果這件事很簡單,我很抱歉,我很抱歉。謝謝
嘗試使用Google搜索範圍。這基本上是一個變量的生命週期。 – 11684