Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-01-2010, 02:18 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 8
BAK2K3 is on a distinguished road
Default Help! How to update TextField/TextView/Label

Hi all,

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!

Thanks,
Ben K
BAK2K3 is offline   Reply With Quote
Old 12-01-2010, 02:24 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

To achieve that you will need to implement the observer design pattern. Take a look at this article on Wiki Observer pattern - Wikipedia, the free encyclopedia
baja_yu is offline   Reply With Quote
Old 12-01-2010, 02:28 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 8
BAK2K3 is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
To achieve that you will need to implement the observer design pattern. Take a look at this article on Wiki Observer pattern - Wikipedia, the free encyclopedia

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?

Last edited by BAK2K3; 12-01-2010 at 02:31 PM.
BAK2K3 is offline   Reply With Quote
Old 12-01-2010, 02:28 PM   #4 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 159
MiniRobinho is on a distinguished road
Default

You could try doing the following:

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:

Code:
first.myLabel.text = value1;
second.myTextField.text = value2;
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
MiniRobinho is offline   Reply With Quote
Old 12-01-2010, 02:33 PM   #5 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Here is a very good tutorial on implementing the observer patter in ObjC a-coding: The Observer Pattern in Objective-C
baja_yu is offline   Reply With Quote
Old 12-01-2010, 02:45 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 8
BAK2K3 is on a distinguished road
Default

Quote:
Originally Posted by MiniRobinho View Post
You could try doing the following:

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:

Code:
first.myLabel.text = value1;
second.myTextField.text = value2;
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:

Code:
#import <Foundation/Foundation.h>


@interface DataManager : NSObject {
	
	NSInteger resistorValue;
	

}

@property (nonatomic) NSInteger resistorValue;


@end

Last edited by BAK2K3; 12-01-2010 at 03:46 PM.
BAK2K3 is offline   Reply With Quote
Old 12-01-2010, 03:45 PM   #7 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 8
BAK2K3 is on a distinguished road
Default

I've removed this post because It was no longer relevant, I'd managed to fix the problem I had myself.

Last edited by BAK2K3; 12-01-2010 at 05:09 PM.
BAK2K3 is offline   Reply With Quote
Old 12-01-2010, 05:08 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 8
BAK2K3 is on a distinguished road
Default

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.

Any help would be GREATLY appreciated!

Thanks
BAK2K3 is offline   Reply With Quote
Old 12-02-2010, 05:15 AM   #9 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 8
BAK2K3 is on a distinguished road
Default Bump?

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.

Thanks
BAK2K3 is offline   Reply With Quote
Old 12-02-2010, 06:15 AM   #10 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 8
BAK2K3 is on a distinguished road
Default Fixed it.

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:

Code:
-(void)viewWillAppear:(BOOL)animated{
 mylabel.text = string;
}
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.

Thanks those who helped!

Ben K
BAK2K3 is offline   Reply With Quote
Reply

Bookmarks

Tags
data, label, pass, text, update

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 367
6 members and 361 guests
daudrizek, HemiMG, Kirkout, MarkC, Sami Gh
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0