Mike JS Choi
Mike JS Choi

Reputation: 1151

UIView not loading in UIViewController

Trying to load an UIView into a header of a table view in a UIViewController and I'm doing the following but it's not working for some reason. The view is being added but the graph isn't being displayed..

In the 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);
        }
    }];
}

And in the view controller,

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    StockView *view = [[StockView alloc]initWithFrame:tableView.frame];
    return view.maV;
}

Upvotes: 0

Views: 69

Answers (2)

Feng Lin
Feng Lin

Reputation: 593

in you block ,after [self.maV reloadGraph]; just call -[setNeedsDisplay] to display the new result.

Upvotes: 0

Ian MacDonald
Ian MacDonald

Reputation: 14030

You are assigning maV a value inside a (presumably) asynchronous call: findStockDataWithBlock.

Instead, instantiate the view outside of the block and then update its values when the block is executed.

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);
    }
}];

I am compelled to point out that what you're doing is very strange. Why are you creating a view in order to get one of its subviews and throw away the original view? Consider restructuring this so that you create a MAStockGraph directly and pass it an external datasource, or pass back the view you created instead of a subview.

Upvotes: 1

Related Questions