0
我有一個pickerview來顯示位置數組(2個組件);低於2 UILabels(每個組件下有2個)。在實例方法中從UILabel獲取label.text iOS 7
當用戶打開應用程序時,選取器默認選擇器組件中每個陣列中的第一個對象,並更改下面的標籤以反映該選擇。
因此,這將是這個樣子:
|PickerComponent1|PickerComponent2|
| Label 1 | Label 2 |
它正確地顯示在標籤[viewDidLoad中]被調用時。標籤更改爲適當的名稱。
我創建了一個實例方法 - 但我無法將標籤的文本值傳遞給該方法。
日誌顯示爲空(null),並且它返回爲空。 關於如何將標籤文本傳遞給方法的任何想法? *編輯:如果我把代碼分配label1和NSLog行到viewDidLoad方法它適當地打印出標籤文本。當我把它放入方法中時,它不會。
我的viewDidLoad在我的.m文件:
- (void)viewDidLoad
{
//set self as delegate for tripPicker
_tripPicker.delegate = self;
//Set size of tripPicker & datePicker
_tripPicker.transform = CGAffineTransformMakeScale(0.60, 0.60);
_datePicker.transform = CGAffineTransformMakeScale(0.60, 0.60);
//Call method to get array of school names (reusable)
MetaMiles *model = [MetaMiles schoolNameList];
_schoolArray1 = (NSArray *)model;
_schoolArray2 = (NSArray *)model;
//Set date to current date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
_dateLabel.text = [NSString stringWithFormat:@"Date of trip: %@",[dateFormatter stringFromDate:self.datePicker.date ]];
//Set selector method for datePicker so on the value change it updates the date
[self.datePicker addTarget:self action:@selector(updateLabelForDate:) forControlEvents:UIControlEventValueChanged];
[super viewDidLoad];
//Set labels to be appropriate schools
begSchoolLabel.text = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]];
endSchoolLabel.text = [_schoolArray2 objectAtIndex:[_tripPicker selectedRowInComponent:1]];
//Update the Mileage indicator to display miles between currently selected values
ML54AddMilesViewController *milesObject = [[ML54AddMilesViewController alloc]init];
NSArray *currentMiles = [milesObject getMileage];
NSLog(@"Current Miles first run: %@",currentMiles);
}
我getMileage方法:
- (NSArray *)getMileage {
//Update the Mileage indicator to display miles between currently selected values
NSString *begSchool = begSchoolLabel.text;
NSLog(@"FIRE THIS OFF : %@",begSchool);
NSString *endSchool = endSchoolLabel.text;
NSPredicate *milesFilter = [NSPredicate predicateWithFormat:@"beg_school=%@ AND end_school=%@", begSchool, endSchool];
NSArray *resultMiles = [MetaMiles MR_findAllWithPredicate:milesFilter];
if (!resultMiles || ![resultMiles count]) {
NSLog(@"Empty Array");
} else {
NSLog(@"Our array is: %@", [resultMiles objectAtIndex:0]);}
return resultMiles;
}
這甚至不應該編譯。這是你的確切代碼嗎? –
不,這只是我試圖獲得的主要部分記錄標籤的價值。但日誌只顯示爲空 – Hanny
如果標籤的文本屬性爲空,或者標籤本身爲空,它將僅顯示空值。您確定您在創建標籤/文本值設置之前的某個時間點沒有嘗試訪問此屬性嗎? –