Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7555933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:44:34+00:00 2026-05-30T11:44:34+00:00

I have a class RandomGenerator: // RandomGenerator.h #import <Foundation/Foundation.h> @interface RandomGenerator : NSObject {

  • 0

I have a class RandomGenerator:

// RandomGenerator.h
#import <Foundation/Foundation.h>
@interface RandomGenerator : NSObject 
{
    @private
    IBOutlet NSTextField* textField;
    unsigned int number;
}
@end

//RandomGenerator.m
#import "RandomGenerator.h"
@implementation RandomGenerator

- (id)init
{
    self = [super init];
    if (self) 
    {
        textField=[[NSTextField alloc]init];
        [textField setStringValue:@"Insert a number between 1 and 100"];
        srand((unsigned int)time(NULL));
    }
    return self;
}
- (void)dealloc
{
    [super dealloc];
}
@end

That when constructed it sets automatically the value of the NSTextField.
I allocate a RandomGenerator object from the file GuessTheNumberAppDelegate.m :

#import "GuessTheNumberAppDelegate.h"
#import "RandomGenerator.h"
@implementation GuessTheNumberAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];
    RandomGenerator* random=[[RandomGenerator alloc]init];
    [pool drain];
}
@end

And I have made connections in interface builder:

Connection

Interface builder screen

But the content of the NSTextField is not changed, it appears the same, why this?

Result

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:44:35+00:00Added an answer on May 30, 2026 at 11:44 am

    In -[RandomGenerator init], you’re creating a new text field object that has no relation to the text field that’s already in your xib file, and pointing the outlet at that new object. The objects in the xib are real, actual objects that are allocated for you by the loading mechanism. You don’t need textField = [[NSTextField alloc] init];,* nor do you need RandomGenerator* random=[[RandomGenerator alloc]init];. Both of those objects already exist in your xib.

    You do need to change a few things, however. First, if you want your application delegate to be able to access the RandomGenerator, you’ll need to give it an outlet and connect it: IBOutlet RandomGenerator * generator;. Second, you will need to move [textField setStringValue:@"Insert a number between 1 and 100"]; out of -[RandomGenerator init]. Due to the way nib loading works, the generator’s init method will be called before the IBOutlet to the text field is hooked up, and possibly before the text field is even created.

    I’m pretty sure that if you add:

    - (void)awakeFromNib {
        [textField setStringValue:@"Insert a number between 1 and 100"];
    }
    

    to RandomGenerator, that will do the trick. Once the nib has been loaded and all the objects in it are recreated, awakeFromNib should be sent to all those objects.


    *and that’s not the correct initializer for an NSTextField anyways

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: class first{ private: int *array; public: first(int x){ array = new int[x][10];
I have class Distance and typedef enum Unit, @interface Distance:NSObject{ double m_miles; } @property
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have :- Class Abc extends Activity(){ private ActivityManager am; private List<RunningAppProcessInfo> rList; @Override
I have class @Embeddable public class MyClass implements Serializable { private String field1; private
I have class X that implements an interface IX . I also have a
I have class Employee which is something like this. class Emp { int EmpID;
I have class Car < ActiveRecord::Base; end class Porsche < Car has_many :page_items, :as=>:itemable,
I have class method that returns a list of employees that I can iterate
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.