UIView 未在 UIViewController 中加载
UIView not loading in UIViewController
尝试将 UIView
加载到 UIViewController
中的 table 视图的 header 中,我正在执行以下操作,但由于某种原因无法正常工作.正在添加视图,但未显示图表..
在UIView
,
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self symbolLookup];
}
return self;
}
-(void)symbolLookup{
MAFinance *stockQuery = [MAFinance new];
// set the symbol
stockQuery.symbol = @"goog";
/* set time period
MAFinanceTimeFiveDays
MAFinanceTimeTenDays
MAFinanceTimeOneMonth
MAFinanceTimeThreeMonths
MAFinanceTimeOneYear
MAFinanceTimeFiveYears
*/
stockQuery.period = MAFinanceTimeOneMonth;
[stockQuery findStockDataWithBlock:^(NSDictionary *stockData, NSError *error) {
if (!error) {
// we've got our data
self.allData = stockData;
NSLog(@"%@", [[self.allData objectForKey:@"StockInformation"] allKeys]);
self.pricesArray = [stockData objectForKey:@"Prices"];
self.datesArray = [stockData objectForKey:@"Dates"];
self.maV = [MAStockGraph new];
self.maV.delegate = self;
[self addSubview:self.maV];
[self.maV reloadGraph];
} else {
// something went wrong, log the error
NSLog(@"Error - %@", error.localizedDescription);
}
}];
}
并且在视图控制器中,
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
StockView *view = [[StockView alloc]initWithFrame:tableView.frame];
return view.maV;
}
您正在(可能)异步调用中分配 maV
一个值:findStockDataWithBlock
。
而是在块外部实例化视图,然后在执行块时更新其值。
self.maV = [MAStockGraph new];
self.maV.delegate = self;
[stockQuery findStockDataWithBlock:^(NSDictionary *stockData, NSError *error) {
if (!error) {
// we've got our data
self.allData = stockData;
NSLog(@"%@", [[self.allData objectForKey:@"StockInformation"] allKeys]);
self.pricesArray = [stockData objectForKey:@"Prices"];
self.datesArray = [stockData objectForKey:@"Dates"];
[self.maV reloadGraph];
} else {
// something went wrong, log the error
NSLog(@"Error - %@", error.localizedDescription);
}
}];
不得不指出,你的做法很奇怪。为什么要创建一个视图以获取其子视图之一并丢弃原始视图?考虑对其进行重组,以便您直接创建 MAStockGraph
并将其传递给外部数据源,或者传回您创建的视图而不是子视图。
在你块中,在 [self.maV reloadGraph] 之后;只需调用 -[setNeedsDisplay] 即可显示新结果。
尝试将 UIView
加载到 UIViewController
中的 table 视图的 header 中,我正在执行以下操作,但由于某种原因无法正常工作.正在添加视图,但未显示图表..
在UIView
,
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self symbolLookup];
}
return self;
}
-(void)symbolLookup{
MAFinance *stockQuery = [MAFinance new];
// set the symbol
stockQuery.symbol = @"goog";
/* set time period
MAFinanceTimeFiveDays
MAFinanceTimeTenDays
MAFinanceTimeOneMonth
MAFinanceTimeThreeMonths
MAFinanceTimeOneYear
MAFinanceTimeFiveYears
*/
stockQuery.period = MAFinanceTimeOneMonth;
[stockQuery findStockDataWithBlock:^(NSDictionary *stockData, NSError *error) {
if (!error) {
// we've got our data
self.allData = stockData;
NSLog(@"%@", [[self.allData objectForKey:@"StockInformation"] allKeys]);
self.pricesArray = [stockData objectForKey:@"Prices"];
self.datesArray = [stockData objectForKey:@"Dates"];
self.maV = [MAStockGraph new];
self.maV.delegate = self;
[self addSubview:self.maV];
[self.maV reloadGraph];
} else {
// something went wrong, log the error
NSLog(@"Error - %@", error.localizedDescription);
}
}];
}
并且在视图控制器中,
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
StockView *view = [[StockView alloc]initWithFrame:tableView.frame];
return view.maV;
}
您正在(可能)异步调用中分配 maV
一个值:findStockDataWithBlock
。
而是在块外部实例化视图,然后在执行块时更新其值。
self.maV = [MAStockGraph new];
self.maV.delegate = self;
[stockQuery findStockDataWithBlock:^(NSDictionary *stockData, NSError *error) {
if (!error) {
// we've got our data
self.allData = stockData;
NSLog(@"%@", [[self.allData objectForKey:@"StockInformation"] allKeys]);
self.pricesArray = [stockData objectForKey:@"Prices"];
self.datesArray = [stockData objectForKey:@"Dates"];
[self.maV reloadGraph];
} else {
// something went wrong, log the error
NSLog(@"Error - %@", error.localizedDescription);
}
}];
不得不指出,你的做法很奇怪。为什么要创建一个视图以获取其子视图之一并丢弃原始视图?考虑对其进行重组,以便您直接创建 MAStockGraph
并将其传递给外部数据源,或者传回您创建的视图而不是子视图。
在你块中,在 [self.maV reloadGraph] 之后;只需调用 -[setNeedsDisplay] 即可显示新结果。