cloud1 Posted July 14, 2008 Share Posted July 14, 2008 How do I set outlets's or text fields' string value using setStringValue during runtime with out an event been triggered? What I meant by "with out an event been triggered" is with out the user interaction with the GUI, like doesn't require an user to click on a button or etc. For example: //DisplayController.h #import <Cocoa/Cocoa.h> @interface DisplayController : NSObject { IBOutlet NSTextField *textFieldDisplay; } - (IBAction)changeTextFieldText:(id)sender; -(IBAction) changeText:(NSString*) text; @end // DisplayController.m #import "DisplayController.h" @implementation DisplayController - (IBAction)changeTextFieldText:(id)sender { [textFieldDisplay setStringValue:@"This is a text field!"]; } -(IBAction) changeText: (NSString *) message { textFieldDisplay=[[NSTextField alloc] init]; [textFieldDisplay setStringValue:message]; } @end //main.m #import <Cocoa/Cocoa.h> #import <stdio.h> #import "DisplayController.h" int main(int argc, char *argv[]) { DisplayController *dc; dc=[[DisplayController alloc] init]; [dc changeText:@"This is a text field!"]; /*This doesn't work because it doesn't change the text field's string value*/ [dc release]; return NSApplicationMain(argc, (const char **) argv); } Link to comment Share on other sites More sharing options...
stroke Posted July 15, 2008 Share Posted July 15, 2008 Well, there are some issues with your code. This version, through just modifying DisplayController.m would work: #import "DisplayController.h" @implementation DisplayController -(void)awakeFromNib // awakeFromNib is automatically called when the object (DisplayController) is instantiated by the NIB file — no need to modify main.m { [self changeText:@"string"]; } - (IBAction)changeTextFieldText:(id)sender { [textFieldDisplay setStringValue:@"This is a text field!"]; } -(IBAction) changeText: (NSString *) message { [textFieldDisplay setStringValue:message]; } @end textFieldDisplay is an IBOutlet — it should be hooked up to an NSTextField in your XIB/NIB file. Also, you need to make sure that you made an instance of DisplayController in your XIB/NIB. Link to comment Share on other sites More sharing options...
cloud1 Posted July 29, 2008 Author Share Posted July 29, 2008 I had been busy lately, but I have tried the code that you gave me and it still doesn't work. Anyway I don't know what is wrong with the code that you gave me and my before source code and after source code, and I also follow all the instructions that you told me to follow and it still doesn't work. But thanks anyway for trying to help me. Link to comment Share on other sites More sharing options...
stroke Posted July 30, 2008 Share Posted July 30, 2008 Did you properly connect the outlets? Link to comment Share on other sites More sharing options...
cloud1 Posted August 1, 2008 Author Share Posted August 1, 2008 Yes, I have had properly connected the outlets. Link to comment Share on other sites More sharing options...
stroke Posted August 1, 2008 Share Posted August 1, 2008 Well, I don't know what you're doing wrong. Assuming you want the string to change as soon as the program starts, the code I used above works fine for me. You can see this with the project I've attached... Field.zip Link to comment Share on other sites More sharing options...
cloud1 Posted August 9, 2008 Author Share Posted August 9, 2008 Well, I don't know what you're doing wrong. Assuming you want the string to change as soon as the program starts, the code I used above works fine for me. You can see this with the project I've attached... Sorry for the late reply again because I'm very busy. The source codes/project file that you have attached also works for me too. But my intention is based on the current design/set up I wanted the text field string value to change while the program is running and it doesn't require user interaction(s), system event(s), etc. For example: //main.m #import <Cocoa/Cocoa.h> #import <stdio.h> #import "DisplayController.h" int main(int argc, char *argv[]) { DisplayController *dc; int counter=0; dc=[[DisplayController alloc] init]; while (counter<100) { counter++; [dc changeText:@""+counter];/*This doesn't work because it doesn't change the text field's string value*/ } [dc release]; return NSApplicationMain(argc, (const char **) argv); } Link to comment Share on other sites More sharing options...
stroke Posted August 9, 2008 Share Posted August 9, 2008 main.m is not a class. You can not give it outlets. What you want is an NSTimer, look in the documentation. You need to make an AppController class, or somesuch. Link to comment Share on other sites More sharing options...
cloud1 Posted August 21, 2008 Author Share Posted August 21, 2008 main.m is not a class. You can not give it outlets. What you want is an NSTimer, look in the documentation. You need to make an AppController class, or somesuch. I have tried your suggestion and many other possibilities, but it still doesn't work. The following attachment is my XCode project. Cocoa_Application.zip Link to comment Share on other sites More sharing options...
alloutmacstoday Posted August 21, 2008 Share Posted August 21, 2008 Just get rid of the stuff in main.m that you added. Link to comment Share on other sites More sharing options...
cloud1 Posted August 21, 2008 Author Share Posted August 21, 2008 Just get rid of the stuff in main.m that you added. [dc changeText1:@"This is a text field!!!!!!!"]; The above code and the rest of the codes in main.m doesn't produce compiler time errors or runtime errors, but just that it doesn't change the text filed string value when they got executed. How can I made this work based on my current design? Cocoa_Application.zip Link to comment Share on other sites More sharing options...
alloutmacstoday Posted August 21, 2008 Share Posted August 21, 2008 I know that it doesn't produce them If you do get rid of them though, it will work. The issue is you have two instances of displayController. 1)the once created when the nib loads. and 2) the one you create in main.m. The one created in main.m will not work, because it is loaded first. Link to comment Share on other sites More sharing options...
cloud1 Posted August 24, 2008 Author Share Posted August 24, 2008 I know that it doesn't produce them If you do get rid of them though, it will work. The issue is you have two instances of displayController. 1)the once created when the nib loads. and 2) the one you create in main.m. The one created in main.m will not work, because it is loaded first. Oh, okay, so it will not work. I get it. Anyway, then, how can I can access IBOutlet textField from main.m? And, what access modifier(s) do I have to specified in front of some variables in some other files/classes so that I can access it from main.m? I have to use the "extern" access modifier, right? Link to comment Share on other sites More sharing options...
alloutmacstoday Posted August 24, 2008 Share Posted August 24, 2008 why do you want to access them from main.m so badly? Link to comment Share on other sites More sharing options...
cloud1 Posted August 26, 2008 Author Share Posted August 26, 2008 why do you want to access them from main.m so badly? Because I came from a C++, Java, and etc programming background, and this is how the text field, label, and etc were accessed. In C++, and Java, and etc, the text field, label, and etc, can be access and it string value can be modified if only if the programmer provide access to it in some other classes or in the same class where the text field, label, etc, were defined and declared. For example: // Main.java public class UserInterface extends JFrame { JTextField textField; public UserInterface () { super(); initialize(); } private void initialize () { int x=0; String result=""; textField=new JTextField(); while (x<=100) { x+=1; result+=x+", "; } TextField.setText(result); } } Link to comment Share on other sites More sharing options...
alloutmacstoday Posted August 26, 2008 Share Posted August 26, 2008 Yeah, I came from java too, but you really just can't do it that way in objective-c. Sorry! Link to comment Share on other sites More sharing options...
cloud1 Posted August 30, 2008 Author Share Posted August 30, 2008 Thanks for all your help. Link to comment Share on other sites More sharing options...
Recommended Posts