-2

I change the value of 2 UILabels in my "viewDidLoad" method, but I need the view to refresh after that in order to display the values. As it currently stands, the UILabels display the value of the previously selected cell. I need to do the refresh right after I change the labels' values. The "setNeedsDisplay" method is not doing the job.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _nameLabel.text = _selectedLocation.name;
    _addressLabel.text = _selectedLocation.address;

    [self.view setNeedsDisplay];

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vimzy
  • 1,871
  • 8
  • 30
  • 56
  • try to set the values in ViewWillApper – user4261201 May 12 '15 at 03:15
  • At what point you are changing the label values – user4261201 May 12 '15 at 03:18
  • @chinnari At the point I have in the code. ViewController segues into this controller, and then viewDidLoad alters the values. I just need a way to refresh the current controller, and it'll be fine. – Vimzy May 12 '15 at 03:19
  • Use NSNotification for it. – Ashok Londhe May 12 '15 at 03:47
  • @AshokLondhe care to elaborate a little? – Vimzy May 12 '15 at 03:48
  • @Vimzy use NSNotiifcation .. send notification to viewDidLoad method when your label text can change. – Ashok Londhe May 12 '15 at 03:59
  • @AshokLondhe using a notification for this is incredibly bad practise. Should be a segue with the data sent through the prepare for segue method – Simon McLoughlin May 12 '15 at 08:50
  • @SimonMcLoughlin question is different and he get answer. and using Notification is good practice as per my opinion. – Ashok Londhe May 12 '15 at 08:53
  • @AshokLondhe I understand the user got the answer and it was a different issue. That aside, using a notification is not the right approach at all. A notification is to be used when many screens need to respond to the same event. Using a notification to send data from one screen to the next screen is **very** bad practise. Its very difficult to follow a notification through code as it can be defined in hundreds of different places. They should be avoided unless absolutely necessary, especially when a good alternative exists – Simon McLoughlin May 12 '15 at 08:58
  • This kind of discussion could be avoided if the question was a little more detailed about from where the user triggered the update method and/or provided the related source code. – FormigaNinja May 19 '15 at 07:07

2 Answers2

1

Based on your comments, I think you are trying to do something like:

- (void)updateLabelTexts {

    _nameLabel.text = _selectedLocation.name;
    _addressLabel.text = _selectedLocation.address;
}

and wherever you are changing the _selectedLocation values:

//Just an example
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    _selectedLocation = _yourLocationsArray[indexPath.row];

    //now you call your update method
    [self updateLabelTexts];
}

The point is that you have to call [self updateLabelTexts]; just after you update the values.

FormigaNinja
  • 1,571
  • 1
  • 24
  • 36
0

A very stupid bug. Turns out when I made the segue to transition into the next view, I actually dragged it from a physical cell on to the destination controller. However, I should've simply connected the sending uiview controller to the destination viewcontroller with the segue, and then manually handled the transition. That fixed it, so there's no need to "refresh or reload" the UIView as I was trying to do.

Vimzy
  • 1,871
  • 8
  • 30
  • 56