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 8040603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:57:09+00:00 2026-06-05T03:57:09+00:00

This is an objective-c problem. I have created a subclass person of NSObject with

  • 0

This is an objective-c problem. I have created a subclass person of NSObject with parameters ‘height’ and ‘weight’, with property and synthesize in a file called Person.h that contains both interface and implementation.

I want to import Person.h into my viewcontroller.m and create person objects and alter them using 2 IBActions.

-(IBAction)alterperson_1{

 person *bob = [person alloc]init];
 bob.height = 72;
 bob.weight = 200;
}

-(IBAction)alterperson_2{

 bob.height = 80;
 bob.weight = 250;

}

This arrangement does not work because the method alterperson_2 can’t find Bob because it is a local variable to alterperson_1. My question is how and where in viewcontroller.m do I allocate Bob as a person so that his attributes can be altered by both IBActions.

I have tried allocing in viewdidload as well as in the initwith nibname methods. It did not work. I have also tried in the implementation{ } of viewcontroller.m but that doesn’t work either because Bob’s allocation is not a compile time constant.

Thanks!

Update With Code

So, I have the Person.h file importing properly now (thanks Robotnik), and am able to create instances of Person throughout ViewController.m — however, my created instance *bob does not seem to retain values for its properties (see comments by NSLog statements in the code). I think this is an initialization issue, but I have no idea where to initialize. Currently, I get a warning when initializing in viewDidLoad. How do I get bob.weight to print 200 when my IBAction is called, instead of the 0 I currently get? Thanks.

// Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject{

int weight;
int height;
}

@property int weight, height;

@end

end Person.h

//Person.m


#import "Person.h"

@implementation Person

@synthesize weight, height;

@end

end Person.m

//ViewController.h

#import <UIKit/UIKit.h>
#import "person.h"



@interface ViewController : UIViewController{


}

@property Person *bob;

-(IBAction)persontest:(id)sender;

@end

end ViewController.h

//ViewController.m

#import "ViewController.h"

@implementation ViewController

@synthesize bob;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    Person *bob = [[Person alloc]init]; // this causes a local declaration warning, if I remove this code, however, it still doesn't work
    bob.weight = 100;
    NSLog(@"viewDidLoad bob's weight, %i", bob.weight); // this will print 100, but only because I made the local initialization. The value is lost once the viewDidLoad Method ends.
}

-(IBAction)persontest:(id)sender{

    bob.weight = bob.weight + 100;
    NSLog(@"IBAction bob's weight %i", bob.weight); // this prints 0, probably because value is nil. How can I make it print 200?
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

end ViewController.m

  • 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-06-05T03:57:12+00:00Added an answer on June 5, 2026 at 3:57 am

    You’ll need to declare Bob in your ViewController.h if you want him to be accessible across multiple methods. You can then initialise him in viewDidLoad

    @interface ViewController
    {
        Person *bob;
    }
    
    -(IBAction)alterperson_1;
    -(IBAction)alterperson_2;
    @end
    

    You mentioned you wanted to instantiate multiple people. In that case you may want to keep multiple Person objects in an NSMutableArray or similar. This should still be declared in the ViewController.h in order to be accessible in multiple methods. You can then use the method addObject: to add people to the array. Here is an example of an array, storing strings.

    NSMutableArray *stringArray = [[NSMutableArray alloc] init];
    
    [stringArray addObject:@"Dog"];
    [stringArray addObject:@"Cat"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Objective-C code that I found somewhere. Honestly, I don't understand a
This is Objective-C, in Xcode for the iPhone. I have a method in main.m:
Can anyone help convert this this actionscript to Objective-c? if(mcMain.y >= stage.stageHeight - mcMain.height)
I have some Objective-C code that looks like this: #define myVar 10 float f
I've found a bunch of posts on this problem and have actually managed to
I created my app using StoryBoard. Then, I added objective-c class to have .h
I have created a screen using Objective-C in an iPhone project. In that, there
I have written this piece of objective-c code for playing a video after touching
This problem must have been solved a million times, but Google has not been
First of all, I found this: Objective C HTML escape/unescape , but it doesn't

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.