I'm relatively new to Objective C based programming and currently writing a program which uses a TabBarControllerView along with 4 tabs all linked to different .xib files. I have one separate view called "DataManager", which holds all the data to which the other views need access to.
I have managed to get the other views to pull NSIntegers from the DataManager, along with managing to get the other views to 'alter' these NSIntegers.
However, I have a button in one view which alters the value of (for example) "value1", and a label on another view which displays the value of said "value1". When testing the app, the view displays the value set by the DataManager, however whilst pressing the button changes the value of "value1", the label still displays the old unchanged "value1".
I can force the label to re-check the DataManager by re-setting the label.text = value1 through the use of a button, but I need the program to auto-update the label whenever the value is changed.
I have attempted using UILabel/UITextView/UITextField but don't know the programming language well enough to apply any hint of instruction from the developer help.
I am more than happy to provide any code that is needed to help me, as this is a necessary part of my application, and any help will be appreciated!
I just had a look at your link but couldn't find any instructions as to how to implement this in objective C?
Is there any other way to achieve this "auto-update" feature?
Edit:
Just had a thought - the other way I could possibly do this is to bring up an alertview everytime the tab is selected just with a simple 'continue' option (with a method behind it that forces the re-check of all labels). Any thoughts?
1) In DataManager, declare public variables called FirstViewController *first, SecondViewController *second - whatever the view controllers are called.
2) In FirstViewController and SecondViewController, when they load up, assign the DataManager variables. In ViewDidLoad put the following:
Code:
// Initialise DataManager as you normally do, something like below
DataManager *manager = [DataManager sharedInstance];
// For FirstViewController add:
manager.first = self;
// For SecondViewController add:
manager.second = self;
3) Create a public method in DataManager called refreshLabels, and put the following code in:
1) In DataManager, declare public variables called FirstViewController *first, SecondViewController *second - whatever the view controllers are called.
2) In FirstViewController and SecondViewController, when they load up, assign the DataManager variables. In ViewDidLoad put the following:
Code:
// Initialise DataManager as you normally do, something like below
DataManager *manager = [DataManager sharedInstance];
// For FirstViewController add:
manager.first = self;
// For SecondViewController add:
manager.second = self;
3) Create a public method in DataManager called refreshLabels, and put the following code in:
4) In FirstViewController and SecondViewController, after updating a value, call the function by doing:
Code:
[manager refreshLabels];
That should work, hope that all makes sense
Thank you VERY much for the prompt reply MiniRobinho, however I'm having a little trouble on the first step (I apologize for the lack of experience and knowledge - I'm doing this for a university project and only started learning a few months ago).
I don't understand how to declare a public variable of a viewcontroller in the DataManager. I'm sure I'll be fine with the next 3 steps, but could you possibly explain the first step please?
Many Thanks!
Ben K
Edit:
Where and how should I declare the variable of the viewcontroller? This is an excerpt from my DataManager.h:
Ok, so first of all apologies for just continually posting updates - but I've made some progress and now stuck again:
I have managed to once again access and write to the Data Manager, however with MiniRobinho's help I've managed to create variables of the viewcontrollers and access a method within the DataManager:
Method (DataManager.m):
Code:
-(void) refreshLabels {
NSString *tempResistor = [ [NSString alloc] initWithFormat: @"$%d", resistorValue];
second.labelRandom.text = tempResistor;
NSLog(@"Labels have been Refreshed!");
}
|Here "second" refers to the second view called BuySell..
Declaration of method (DataManager.h):
Code:
-(void) refreshLabels;
Now, the method is called from within another method on a view called Location.m:
Code:
-(void) buttonPressedEE {
yourLocation.text = @"Electrical Engineering";
TestApp2AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
DataManager* myDataManager = [appDelegate myDataManager];
[myDataManager setResistorValue:555];
NSLog(@"Resistor Value Set to 555");
NSLog(@"Method Called1");
[myDataManager refreshLabels];
}
Now, in the console, all NSLogs are reported - meaning the method from DataManager is being called successfully. However nothing changes on my second view (second.labelRandom). I can force the label to update from a second view button method, but for some reason the method called in DataManager (refreshLabels) isn't doing anything other than performing the NSLog.
I don't understand why this is so difficult to do. What I'm trying to do is such a fundamental part of most games or applications (passing data through views) so why am I finding it so hard to Update a Label?
I would really appreciate some help, because this is for a university project and without this I may have to start my app from scratch.
For anyone who has this problem in the future (who want the labels within any given view to 'auto-update' without having to manually 'refresh' the label:
So in the view in which I want to labels to refresh all i had to do was add this method:
Where mylabel is the label you want to update, and string is the string you should already have the label attached to - all this does is forces the label to recheck the string upon the view appearing.