Reputation: 305
I'm new to iOS development and to stack overflow. I need to show user's data in multiple UILabel and UITextField, the data is obtained from a POST method. There is a slight delay for getting the data from the server. How to reload or populate the elements after getting the details. This is my viewDidLoad()
@interface EditProfileViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *displayPictureView;
@property (weak, nonatomic) IBOutlet UITextField *firstnameFeld;
@property (weak, nonatomic) IBOutlet UITextField *lastNameField;
@property (weak, nonatomic) IBOutlet UITextField *emailField;
@property (weak, nonatomic) IBOutlet UITextField *bdayField;
@property (weak, nonatomic) IBOutlet UIButton *calField;
@property (weak, nonatomic) IBOutlet UITextView *addressTextView;
@property (weak, nonatomic) IBOutlet UILabel *userIDLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneNumberLabel;
@property (weak, nonatomic) IBOutlet UIScrollView *editScrollView;
@end
@implementation EditProfileViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.editScrollView.delegate=self;
}
I receive data from the server using the code, i have this POST method in viewDidLoad()
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:urlEdit];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[editParameters dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ \nError:%@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"DataText = %@",text);
}
NSError *error2 = nil;
jsonDicEditAcc = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error2];
if (error2 != nil) {
NSLog(@"Error parsing JSON.");
}
else {
NSLog(@"JsonDictEditAcc: \n%@",jsonDicEditAcc);
[self dismissViewControllerAnimated:alert completion:nil];}
}];
[dataTask resume];
I will extract data from the "jsonDicEditAcc", i need to know how to populate the UILabels and UITextField, after getting the data because the view gets loaded before getting the data. I have tried viewWillAppear() did not work. Help me out.. Thanks in advance..
Upvotes: 1
Views: 69
Reputation: 229
You'll need to know the content of the data you're trying to apply. If you know the key's that associate with each value you can apply those values to the text fields (and labels and buttons) and be done. They will change when you successfully update them.
Inside your block you need to trigger the population of the text fields. You can add something like this:
else {
NSLog(@"JsonDictEditAcc: \n%@",jsonDicEditAcc);
[self updateLabelsAndTextFields];
[self dismissViewControllerAnimated:alert completion:nil];}
}];
This function that will extract the data from jsonDicEditAcc and update the text of each relevant UILabel. The view has already loaded, but the UILabel will update when you change the text.
A solution could look like this:
- (void)updateLabelsAndTextFields{
self.firstnameFeld.text = jsonDicEditAcc[@"key"];
self.lastNameField.text = jsonDicEditAcc[@"key"];
self.emailField.text = jsonDicEditAcc[@"key"];
self.bdayField.text = jsonDicEditAcc[@"key"];
self.userIDLabel.text = jsonDicEditAcc[@"key"];
[calField setTitle:jsonDicEditAcc[@"key"] forState:UIControlStateNormal];
}
Be sure name a UIButton a button, i.e. "calButton" not "calField"
Upvotes: 1
Reputation: 323
It's pretty straightforward, add similar code to the else branch of your if (error2 != nil) statement:
weakSelf.firstnameFeld = jsonDicEditAcc[@"<key-for-firstname>"];
Also, you might find it necessary to remove this statement from that branch as it will close the current viewController:
[self dismissViewControllerAnimated:alert completion:nil];
And I might point out, you need to use weakSelf inside of blocks to prevent memory leaks. Using self inside a block creates a retain cycle which means your entire viewController remains allocated until iOS eventually crashes your app for excessive memory use.
Put this at the top of your method and it is accessible inside the block, and then change all references of self inside of blocks to weakSelf.
typeof(self) __weak weakSelf = self;
Upvotes: 1