Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

noobcodered

macrumors newbie
Original poster
Jan 29, 2016
4
0
So there aren't many resources online for this problem I'm having. All of the tutorials I find involving external JSON/Databases end with putting the data into a table view, which makes the coding a bit different than what I am trying to do. I'm connecting with my Database just fine, but actually using the data isn't working because it's set up for a table view with a delegate for the data, so I assume the data from my Database is being parsed correctly and all of that, but I don't know how to output the data because I obviously can't tie UILabels to the ViewController delegate, etc. At least I think that's what's going on.

I have a Database that I set up, and I'm converting the data to JSON using PHP. Figuring out how to even set up the Database was an issue because I had never done it before, but here we are. The JSON looks like this:

Code:
[{"id":"0","saleDate":"February 30th - February 31st","saleAddress1":"3333 Butterball Rd.","saleAddress2":"Muskegon, MI 53443"}]

I've already poured through documentation and various tutorials. They aren't much help. The examples they're using are a lot more complicated than what I'm trying to do. Most of the stuff that uses Obj-C is for older versions of iOS with depreciated methods and such. I had to change my info.plist to Allow Arbitrary Loads because the certificates aren't set up correctly on my server. I'm just trying to figure out the logistics of what I'm missing, and then I can work on "updating" the code to more current methods.

I got up to the Table View part of a tutorial and this is what I have:

Code:
@interface ViewController : UIViewController {
   
    NSDictionary *saleDictionary;
   
    IBOutlet UIImageView *imageOutlet2;
    IBOutlet UILabel *saleDate;
    IBOutlet UILabel *add1;
    IBOutlet UILabel *add2;
   
    NSArray *sale;
    NSMutableData *data;
   
}


@property (nonatomic, copy) NSDictionary *saleDictionary;

Code:
@interface ViewController ()


@end

@implementation ViewController
@synthesize saleDictionary;

- (void)viewDidLoad {
   
    imageOutlet2.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://upload.wikimedia.org/wikipedia/commons/4/49/Koala_climbing_tree.jpg"]]];
   
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
   
    NSURL *url = [NSURL URLWithString:@"http://www.striatecortex.com/eesales/json/json.php"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
   
    saleDate.text = [NSString stringWithFormat:@"%@",[saleDictionary objectForKey:@"saleDate"]];
    add1.text = [NSString stringWithFormat:@"%@",[saleDictionary objectForKey:@"saleAddress1"]];
    add2.text = [NSString stringWithFormat:@"%@",[saleDictionary objectForKey:@"saleAddress2"]];
   
   
    NSLog(@"objectForKey : 1--- %@",[saleDictionary objectForKey:@"saleDate"]);
    NSLog(@"objectForKey : 2--- %@",[saleDictionary objectForKey:@"saleAddress1"]);
    NSLog(@"objectForKey : 3--- %@",[saleDictionary objectForKey:@"saleAddress2"]);
   
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
   
    data = [[NSMutableData alloc] init];
   
    NSLog(@"didRespond");
   
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
   
    [data appendData:theData];
   
    NSLog(@"didData");
   
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
   
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    sale = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
   
   
    NSLog(@"didLoad");
   
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Sports!"
                                                                   message:@"You're connected!"
                                                            preferredStyle:UIAlertControllerStyleAlert];
   
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];
   
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
   
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error {
   
    NSLog(@"didFail");
   
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Oops!"
                                                                   message:@"You ****ed up!"
                                                            preferredStyle:UIAlertControllerStyleAlert];
   
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];
   
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
   
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I'm getting all of the proper logs except for the Dictionary values. I'm connecting, and loading the data just fine. xCode is telling me the expression result is unused from my [[NSURLConnection alloc] initWithRequest:request delegate:self]; bit. I understand that once your TableView is set up, you set the delegate and whatnot, so I'm assuming that is part of the problem.

How do I adapt this to use for UILabel as opposed to Table View?
 

1458279

Suspended
May 1, 2010
1,601
1,521
California
Ok, I want to make sure I understand the question.

1. You're saying that you are getting the data from the source and you confirm this with the NSLogs().

2. You have some message concerning unused results, but you already know you are getting the result data you expected to get.

3. You are trying to put the data into a UILabel.text using:
Code:
add1.text = [NSString stringWithFormat:mad:"%@",[saleDictionary objectForKey:mad:"saleAddress1"]];

Your problem is that add1.text doesn't have the expected string in it, but the NSLog() does?

if that's the case, the first thing I'd do is make a string var and store the results in there before the call to "add1.text = ...".

Change to:
Code:
myString = [NSString stringWithFormat:mad:"%@",[saleDictionary objectForKey:mad:"saleAddress1"]];
add1.text = myString;
Check the display and the value.
NSLog(@"string: %@", add1.text);

I'd also look at any buffers or queues on the data stream/source that can be flushed.
 
Last edited:

AxoNeuron

macrumors 65816
Apr 22, 2012
1,251
855
The Left Coast
I'm not seeing where this object 'salesDictionary' is getting its data. It seems to me you're probably just printing out a blank/empty object.

In the NSURLConnection's didFinishLoading() delegate method, this is where you should be setting the value of the label. I would imagine that you would set the value of the salesDictionary object here too. viewDidLoad() gets called before the networking request has been completed, so if you set the label's text value inside viewDidLoad() the label will be blank simply because this needs to happen in didFinishLoading.

Also, I don't understand what you're saying here:

but I don't know how to output the data because I obviously can't tie UILabels to the ViewController delegate, etc. At least I think that's what's going on.

EDIT: Karljay also makes a good point, your post doesn't make it really clear, when you use NSLog what is it actually printing out? These types of things are stuff you always want to make very clear when asking questions, I know it can be hard as a new programmer to learn how to ask the right questions and give the right information.

There is no fundamental difference between displaying data on a table view vs. displaying it on a UILabel. The UITableViewCell itself just uses a UILabel to display text, so it's all the same thing really.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.