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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:24:27+00:00 2026-06-10T12:24:27+00:00

I have Class1 , Class2 , and SubclassOfClass2 . I want to call method

  • 0

I have Class1, Class2, and SubclassOfClass2. I want to call method that is in SubclassOfClass2 from Class1. I do:

Class2 *class = [[Class2 alloc] init];
[class someMethod];

But, as I guess, due to alloc, all variables values are being lost after that. Because:

At runtime Class2 is being executed. It sets for example variable float x = image.size.width. And it returns correct value. Later, after for example user pressed button, Class1 is being executed. After calling someMethod from Class1 variable x returns 0.00000. How to make it working so that variables values wouldn’t be lost?

As I have been asked, here is my someMethod code:

-(void)someMethod {
NSLog(@"%f", x);
}

At run time that method returns 3200.00000 and when its called from Class1 it returns 0.00000. Variable x is declared in Class2 and method someMethod is in SubclassOfClass2


Class1.h

@interface Class1

@end

Class1.m

#import "Class1.h"
#import "Class2.h"

@implementation Class1
-(IBAction)buttonPressed:(id)sender {
Class2 *class = [[Class2 alloc] init];
[class someMethod];
}
@end

Class2.h

@interface Class2 {
 float x;
}
@end

Class2.m

#import "Class2.h"
#import "SubclassOfClass2.h"

@implementation Class2
- (id)initWithCoder:(NSCoder *)coder {
    x = 3200;
    [self someMethod];
    return self;
}
@end

SubclassOfClass2.h

#import "Class2.h"

@interface Class2 (subclass) {
}
-(void)someMethod;
@end

SubclassOfClass2.m

#import "SubclassOfClass2.h"
@implementation Class2 (subclass)
 -(void)someMethod {
   NSLog(@"%f", x);
 }
@end
  • 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-10T12:24:29+00:00Added an answer on June 10, 2026 at 12:24 pm

    It returns 0.00000 because you’re calling init. That’s a different method from initWithCoder:, which is the one that sets x.

    (Other problems: have your classes inherit from NSObject as a base class and don’t use class as a variable name…especially when it’s not a class object.)

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

Sidebar

Related Questions

For example, I want to have code like this: class Class1 { Class2.print(Hello there.);
I have a class (let's call it Class1), which inherits another class (Class2). What
I have a form in spring mvc 2 having command class Class1 I want
I have a < div id=thisdiv class=class1 class2 class3 class4 class5> text < /div>
I have a Java class which has some field annotated with @SearchCriteria(criteria = class1.class2.field)
I have a super-class named ClassA and two sub-classes Class1 and Class2. I have
I have base class Class1 and a derived class2. I create two instances of
How can I set an element to have multiple classes? Initial attempt: element.setAttribute(class,class1,class2); element.className=class1
I have three classes derived from Control...Class1, Class2 and Class3. Class1 is the parent.
I have such html data: <div class=class1>...</div> <div class=class1 class2>...</div> <div class=class3>...</div> ... <div

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.