1

I'm having this problem regarding the displaying and updating of the username label.

User id 1 - ‘Felicia’

User id 2 - ‘Sam’

(1st screen: default user is 'Felicia') 1st screen: default user is 'Felicia'

(2nd screen: when change user to 'Sam') 2nd screen: when changed user to 'Sam'

(3rd screen: after clicking to another screen, the username's label auto revert back to 'Felicia' when it supposed to be 'Sam') 3rd screen: after clicking to another screen, the username's label auto revert back to 'Felicia' when it supposed to be 'Sam' as user is not changed

My ideal output is that the username's label will stay as the selected user unless user is changed and also to be able to reflect the name label on the side menu bar whenever the name is updated.

In my codes, I tried adding my methods(getUserInfo() and setUserNameLabel()) in viewDidAppear and viewDidLoad, however both scenarios are not generating the output I want.

viewDidAppear

Advantages: - Name label will reflect the update of selected user's name (User can change their name)

Disadvantages: - Name label will revert to the user id 1’s name on side menu bar when selecting other views

viewDidLoad

Advantages: - Name label won’t revert to the user id 1’s name on side menu bar when selecting other views

Disadvantages - Name label won’t reflect the update of selected user's name

shallowThought
  • 19,212
  • 9
  • 65
  • 112
Meebfel
  • 63
  • 1
  • 13

2 Answers2

1

You need to create delegate or use NSNotificationCenter for trigger name change event in side menu viewcontroller .

Because while you create side menu so that in this class viewDidLoad or any other method called once during life cycle .

Example for do this through NSNotificationCenter.

https://stackoverflow.com/a/24756761/3901620

Community
  • 1
  • 1
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

work around: what about viewWillAppear or viewWillLayoutsubviews

I thinks viewWillAppear or viewWillLayoutsubviews (in case you use autolayout) is what you need. When you switch from Felicia to Sam, you must save that state to somewhere to refer when you reopen your side menu.

pseudocode code:

var selected = Felicia
if `Felicia` clicked:
   selected = Felicia
if `Sam` clicked:
   selected = Sam
(save selected to somewhere like NSUserDefault)

And in your side menu viewWillAppear: change the UI with selected user.

load selected (from somewhere like NSUserDefault)

if selected == `Felicia` 
  do UI update at main thread
if selected == `Sam`
  do UI update at main thread

Take a look at UIViewController lifecycle:

enter image description here

Nhat Dinh
  • 3,378
  • 4
  • 35
  • 51
  • I've tried viewWillAppear and it works the same as viewDidAppear.. viewWillLayoutsubviews doesn't work too :( – Meebfel Jan 31 '17 at 02:25
  • @Meebfel ah, i mean u should update your view according to value of selected user. – Nhat Dinh Jan 31 '17 at 04:42
  • I managed to save the selected user's name into a variable. But the ui can't update. I think it's because of the methods: setUserNameLabel() and getUserInfo(). I've placed them into viewdidappear too.. Do you know where else can i place this two methods if I want them to load only once when the app starts? – Meebfel Jan 31 '17 at 06:15