Help, NSTextField not displaying output! :(

Discussion in 'Mac Programming' started by Kamurei, Jun 14, 2010.

  1. Kamurei macrumors newbie

    Joined:
    Jun 14, 2010
    #1
    Alright, I'm new to programming with XCode. I figured I would try something simple as my first program. I searched online for about a week for just what is wrong and I have no idea. I hope that someone can help me here. All I am trying to do is to take one number, multiply by a second number, and then post the product. All data is in/out puted into NSTextFields with an NSButton calling the multiply action. It compiles fine with no errors but when it runs, no product value appears in the third textfield after the button has been pressed.

    Code:
    MultiplyController.h
    #import <Cocoa/Cocoa.h>
    
    @interface MultiplyController : NSObject  {
        IBOutlet id firstNumber;
        IBOutlet id product;
        IBOutlet id secondNumber;
    }
    - (IBAction)multiply:(id)sender;
    @end
    
    Code:
    MultiplyController.m
    #import "MultiplyController.h"
    
    @implementation MultiplyController
    - (IBAction)multiply:(id)sender 
    {
    	[product setIntValue: [firstNumber intValue] * [secondNumber intValue]];
    }
    @end
     
  2. Ap0ks macrumors 6502

    Joined:
    Aug 12, 2008
    Location:
    Cambridge, UK
    #2
    The code looks fine.

    I suspect the problem lies in your .xib file connections, have you made sure that the button is connected to the multiply method?
     
  3. Kamurei thread starter macrumors newbie

    Joined:
    Jun 14, 2010
    #3
    Thank you for replying. I have opened the .xib file to double check the connections. I selected the button and checked the connections heading. There it had under Sent Actions, multiply: -> MultiplyController. I tried it again and it's still not working. Should I re-write the class files and pray? :3
     
  4. Ap0ks macrumors 6502

    Joined:
    Aug 12, 2008
    Location:
    Cambridge, UK
    #4
    I assume you also have the product IBOutlet on the MultiplyController connected to the product text field? If so perhaps try setting a breakpoint on the [product setIntValue...] line in multiply, and debug, it may give you some info on why it's not working correctly.
     
  5. Kamurei thread starter macrumors newbie

    Joined:
    Jun 14, 2010
    #5
    Yes, as far as I can tell everything is connected appropriately. What do you mean by a breakpoint? I once tried to set the product to a known value, like 40 or something, but even that wasn't displayed...
     
  6. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #6
    If you are using XCode, there should be a gutter at the left side of your source. If you click in that gutter, a blue arrow (pointy rectangle) will mark where the debugger will stop execution so you can examine the state of your variables. Alternatively, you can use NSLog to examine things through the console, as in,

    NSLog( @"%d", [firstNumber intValue] );

    which will print that number in the console when that line is executed.
     
  7. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #7
    i think it would be best if you declare product as an NSTextField * instead of id. Inside the multiply method, you can then find the product of two numbers. The NSTextField needs to have an NSString value to be displayed

    Code:
    - (IBAction)multiply:(id)sender 
    {
            int x = [firstNumber intValue] * [secondNumber intValue];
    	[product setStringValue:[NSString stringWithFormat:@"%d",x]];
    }
    
     
  8. Kamurei thread starter macrumors newbie

    Joined:
    Jun 14, 2010
    #8
    Sydde:
    I tried what you said but I think I did it wrong because I can't get it to print the values into console. Is there any way you can give me step by step instructions? I make a debugger blue bar and then what? :3

    ritsard:
    I tried your method as well. It did not appear to work either. I made sure to change them from id to NSTextField *, then I went ahead and added the int x part. Still, nothing is posted into the third text field though.

    Thank you to both of you though for replying. It means a lot that people are willing to help.
     
  9. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #9
    The NSLog would go in the code where you set the value of the text field (somewhere in the -multiply: method). Put one in for each input field, then set the product field and put a similar NSLog for that field.

    For debugging, you put the blue mark next to the line where you want the program to stop, then make sure the build mode is set to "Debug". Build the program, open the debugger (from the Run menu) and click the green Run button in its toolbar. The program will start (you may have to put in an admin password) and then stop right on the blue mark (before it runs the line). At this point, you can examine variables and step through lines one at a time.

    If none of that is working, the program must not actually ever be reaching that method and you will have to check your connections in Interface Builder.
     
  10. Kamurei thread starter macrumors newbie

    Joined:
    Jun 14, 2010
    #10
    NSLog:
    I still can't get it to work properly to print it to terminal. Perhaps I'm putting it in the wrong place or something. I kept ritsard's recommendation and had the numbers add up to int x. So I put the NSLog lines after that. But still, nothing printed.

    Debugging:
    I placed a blue break tab right on the line with the product. However I couldn't figure out just where the values are supposed to be displayed. I did see that it seemed to be running through a CF loop though. When I researched that, it seemed alright though.

    Interface Builder:
    I opened that up assuming that I did it wrong once more. But it said all the connection in the connections tab was correct. So I'm not sure what I'm missing. Perhaps there some other connection that I'm missing other than having a pointer from the text field to one of the values... I have no idea :confused:

    After all of this, the only thing I can think of is that there is a problem with some part of the program itself?
     
  11. Littleodie914 macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #11
    Zip up the project and post it here, we'll get to the bottom of it. :)
     
  12. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #12
    I am assuming that the firstNumber and secondNumber are NSTextFields as well. From what you have been describing, I think you want to enter a number on two fields and pressing a button would show the product on the product NSTextField. First, you need to make sure that you are getting the proper values. Getting the intValue of an NSTextField is not what you want. I missed this on my post earlier but you would want to try to do something like this:

    Code:
    - (IBAction)multiply:(id)sender 
    {
           int firstInt = [[firstNumber stringValue] intValue];
           int secInt = [[secondNumber stringValue] intValue];
           int x = firstInt * secInt;
    	[product setStringValue:[NSString stringWithFormat:@"%d",x]];
    }
    
     
  13. Kamurei thread starter macrumors newbie

    Joined:
    Jun 14, 2010
    #13
    Littleodie914:
    I zipped it as requested. Though this zip does not have the suggestion that ritsard made. http://www.mediafire.com/?j3ogydkm3ww

    ritsard:
    I did as you requested as well. Still the same result. I finally found the console after poking around through the drop down menus. I noticed that it was saying this.... "Could not connect the action multiply: to target of class NSObject" Kind of something important. Though I don't know how to fix that lol. I undid what you requested and it was still spitting that out.
     
  14. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #14
    It looks like the problem is that you did not define the Class Identity of the custom object in IB. Set the class identitiy (looks like the info button) and set the Class to MultiplyController. Apply the changes I suggested. Let us know. Goodluck!!! :D
     
  15. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #15
    In the xib file: select your MultiplyController object, open the inspector panel to the next-to-last tab (the blue circled "i") and select MultiplyController from the pulldown menu near the top where it says "NSObject" (IOW, set the class of the object).
     
  16. Kamurei thread starter macrumors newbie

    Joined:
    Jun 14, 2010
    #16
    ritsard, Sydde:
    I changed the class to MultiplyController as you suggested... I am happy to announce that now a 0 appears in the product text field. However, no matter what numbers I enter a 0 appears hahaha. This simple code is out to run me for a loop.
     
  17. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #17
    Did you happen to also update the multiply code like I suggested? What value are you expecting there?
     
  18. Kamurei thread starter macrumors newbie

    Joined:
    Jun 14, 2010
    #18
    Yes, I took your suggestions and went ahead and added them in. Despite that though it is still returning a zero regardless of what I enter. Below is the completed code with your suggestions. I am trying to take ints from two text fields, multiply them, and return an int to a third text field. I tried it back with my original code as well and it still produced a 0 in the third text box.

    I do have one question though. In order to see the stringValue of the numbers, don't they need to be NSStrings instead of NSTextFields? Though at this point I am grasping at straws....

    Code:
    MultiplyController.h
    #import <Cocoa/Cocoa.h>
    
    @interface MultiplyController : NSObject {
    	IBOutlet NSTextField *firstnumber;
    	IBOutlet NSTextField *secondnumber;
    	IBOutlet NSTextField *product;
    }
    - (IBAction)multiply:(id)sender;
    @end
    Code:
    MultiplyController.m
    #import "MultiplyController.h"
    
    @implementation MultiplyController
    - (IBAction)multiply:(id)sender {
    	int firstInt = [[firstnumber stringValue] intValue];
    	int secInt = [[secondnumber stringValue] intValue];
    	int x = firstInt * secInt;
    	[product setStringValue:[NSString stringWithFormat:@"%d",x]];
    }
    @end
    
     
  19. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #19
    At this point, I would suggest the you remove all the IB connections (outlet and actions) and reconnect them. In your initial code, you had firstNumber and on the update it is firstnumber.

    With regards to your question, NSTextField has a method called stringValue which returns its visible content as an NSString. For you to do the math properly, you need to get the int values of what was actually entered. The code I have given you is very rudimentary. You may even want to add some test if the string that was entered is even a number of some character but for this exercise, this should suffice.
     
  20. Kamurei thread starter macrumors newbie

    Joined:
    Jun 14, 2010
    #20
    YATTA!!!!!! /jumps exuberantly

    Thank you so much. I did as you recommended thinking that surely it wasn't something so simple. Then I saw that MultiplyController did in fact have have the other pointers just ghosting there. It now works and calculates like it is supposed to! XD /huggiles

    Thank you all so much. I will forever remember what you all have taught me with this. XDD
     

Share This Page