0

So i have two view controllers home, second. I create a variable in home and want to call it in the second UIViewController. I get an error saying:

Use of undeclared identifier 'plusNumber'

Please let me know why i am not connecting the second UIViewController with the variable in home. I imported the header file home.h into the second.h file.

Thanks.

Code is below: home.h

@interface homeViewController : UIViewController <UIAlertViewDelegate>
{
    int plusNumber;  
}
- (IBAction)plusSign:(id)sender;

home.m

- (IBAction)plusSign:(id)sender {

    if(self.plusNumber == 0)
    {
        self.plusNumber = @"1";
    } 
    else 
    {
        self.plusNumber = @"0";
    }

}

second.h

#import "home.h"

@property (weak, nonatomic) IBOutlet UILabel *output;

second.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.output.text = [NSString stringWithFormat:@"%i", plusNumber];
}
Indrajeet
  • 5,490
  • 2
  • 28
  • 43
  • You will need to create @property in home.h to access this in other VC. `@property (nonatomic) int plusNumber;` – Yogesh Suthar Jul 08 '14 at 03:38
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Yogesh Suthar Jul 08 '14 at 03:42

6 Answers6

1

You must declare it as a public variable

So you must change your home.h interface to

@interfacehomeViewController : UIViewController <UIAlertViewDelegate>
 {
  @public
  int plusNumber;  
 }

more info: How to access @public instance variable from another class in Objective-C?

Community
  • 1
  • 1
Jack C
  • 1,044
  • 2
  • 12
  • 22
  • I have tried both suggestions and am stilling getting the same error message. I read through the links as well and think that i am doing what they say to do but it still says Use of undeclared identifier 'plusNumber'. – user3806600 Jul 08 '14 at 03:53
0

Use properties.

@interface HomeViewController : UIViewController
@property (nonatomic) int plusNumber;
@end
CrimsonChris
  • 4,651
  • 2
  • 19
  • 30
0

In home.m, create a segue from view1 to view2 and give it a segueIdentifier Import second.h inside home.h file

- (IBAction)plusSign:(id)sender {

if(self.plusNumber == 0)
{
    self.plusNumber = @"1";
[self performSegueWithItentifier @"segueName"];
} 
else 
{
    self.plusNumber = @"0";
   [self performSegueWithItentifier @"segueName"];
}

}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  Second secondInstance = [segue destinationViewController];
secondInstance.output = self.plusNumber;

}

In Second.h remove the import statement or it will get into a memory leak cycle Make one NSString named @property(nonatomic,strong) NSString *outputStr;

 viewDidLoad{
   self.output.text = self.outputStr;
   }

and voila hope it helps

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
0

home.h

@property (nonatomic) int plusNumber;

home.m

- (IBAction)plusSign:(id)sender {
   [self performSegueWithItentifier @"segue"];
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"segue"]){
        SecondViewController *controller = 
                         (SecondViewController *)segue.destinationViewController;
        controller.plusNumberString  = (self.plusNumber == 0) ?  @"1" :  @"0";
    }
}

second.h

@property (nonatomic, copy) NSString *plusNumberString;

second.m

-(void)viewDidLoad{
    NSLog(@"_plusNumberString = %@", _plusNumberString);
}
meda
  • 45,103
  • 14
  • 92
  • 122
0

In Objective C, way of coding we call this as getter & setters.

You can create your getters and setters by declaring the property to the variable. Since you're using int datatype, just assign it as below

@Property(nonatomic, assign) int plusNumber

HomeViewController.h

@interface homeViewController : UIViewController <UIAlertViewDelegate>
{
    int plusNumber;  
}
@Property(nonatomic, assign) int plusNumber
- (IBAction)plusSign:(id)sender;

SecondViewController.m

#import "homeViewController.h"

- (void)viewDidLoad
{
    [super viewDidLoad];
    homeViewController *homecontroller = [[homeViewController alloc] init]
    self.output.text = [NSString stringWithFormat:@"%i", homecontroller.plusNumber];
}
raja
  • 1,151
  • 7
  • 9
0
HomeViewController.h
    @interfacehomeViewController : UIViewController <UIAlertViewDelegate>
     {

      NSUInteger plusNumber;  
     }
     @property (nonatomic,assign) NSUInteger plusNumber;

HomeViewController.m
@synthesize plusNumber;

SecondViewController.m

#import "homeViewController.h"

- (void)viewDidLoad
{
    [super viewDidLoad];
    homeViewController *homecontroller = [[homeViewController alloc] init]
    self.output.text = [NSString stringWithFormat:@"%i", homecontroller.plusNumber];
}

Hope it helps.

Iphonenew
  • 299
  • 2
  • 11