0

I have an app with mulitple views. On view one I create and NSDate as follows:

NSString *storeDate = [[NSDate date] description];

On view 2, in viewDidLoad: I want to set the value of a label (outlet etc created and linked) to storeDate value using

timeRecord.text = storeDate;

I have imported my views but storeDate is being flagged as undeclared.

Any idea how I can get this to work?

SOFViewController.h

#import <UIKit/UIKit.h>

@interface SOFViewController : UIViewController {

}
-(IBAction) storeDateBut: (id) sender;
-(IBAction) goToView2: (id) sender;
@property (nonatomic,retain) NSString *storeDate;

@end

SOFViewController.m

#import "SOFViewController.h"
#import "view2.h"

@implementation SOFViewController
@synthesize storeDate;

-(IBAction) storeDateBut: (id) sender{

    self.storeDate = [[NSDate date] description];

}

-(IBAction) goToView2: (id) sender{
    view2 *myview2 = [[view2 alloc] initWithNibName:@"view2" bundle:nil];
    [self.view addSubview:myview2.view];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload {

}

- (void)dealloc {
    [super dealloc];
}

@end

view2.h

#import <UIKit/UIKit.h>


@interface view2 : UIViewController {
    IBOutlet UILabel *dateLabel;

}

-(IBAction) goToView1: (id) sender;
@property (nonatomic, retain) IBOutlet UILabel *dateLabel;

@end

view2.m

#import "view2.h"
#import "SOFViewController.h"


@implementation view2
@synthesize dateLabel;

-(IBAction) goToView1: (id) sender{

    SOFViewController *mySOFViewController = [[SOFViewController alloc] initWithNibName:@"view2" bundle:nil];
    [self.view addSubview:mySOFViewController.view];



}

- (void)viewDidLoad {

    dateLabel.text = SOFViewController.storeDate;
    [super viewDidLoad];
}




- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    }

    - (void)viewDidUnload {
        [super viewDidUnload];

    }


    - (void)dealloc {
        [super dealloc];
    }


    @end
TJ15
  • 353
  • 2
  • 10
  • 22
  • does the second view have a reference to the first ? a reference to the string ? can't you get the same date in the second view ? –  Jan 31 '12 at 14:26
  • @Vince I have imported the .h file. The NSDate is recorded on a button click and the user may say on view one without going to view 2 for a while. I need to record the time at the button click and pass this to view 2. – TJ15 Jan 31 '12 at 15:20
  • Side Note: you shouldn't use `description` to generate date strings. It's not localized and the format isn't customizable. Use NSDateFormatter instead. http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html – kubi Jan 31 '12 at 15:45

3 Answers3

1

Scope of storeDate is limited to method of view1. So you can not access it other views or even other method of same class. you have to make "storeDate" property then you can access it anywhere like

@interface view1:UIView {} 
@property(nonatomic, retain) NSString *storeDate;
@end

in implementation use

@synthesize storeDate;

now set the value of storeDate as

self.storeDate = [[NSDate date] description];

and you can use it any where.

fibnochi
  • 1,113
  • 1
  • 9
  • 26
  • Thank you for your help, I have trued this but see the same error. I have used the code above in view 1, in view 2 I have imported view 1 and tried to set the label text with timeRecord.text = storeDate. storeDate is flagged as undeclared. – TJ15 Jan 31 '12 at 15:13
  • You should use timeRecord.text = view1.storeDate; If it help u then dont hesitate to vote up – fibnochi Feb 01 '12 at 08:05
  • Cheers, think an I'm almost there. Made the modification that you suggested but now get "Accessing unknown 'storeDate' class method". I am imported the view and all spelling is correct, any idea? – TJ15 Feb 01 '12 at 10:25
  • I have tested the solution I posted for you and it is working fine with no error. share your code, so that I may find the real issue. – fibnochi Feb 02 '12 at 10:36
  • Thanks again. I have added the code used to the issue description above, it is stripped out but still has the issue. I hope is something simple I am missing. There is 2 views with the action to navigate between then. – TJ15 Feb 02 '12 at 15:26
  • I got your problem. You are using class object of SOFVoewController. You have to use mySOFViewController in view2's viewDidLoad method. – fibnochi Feb 09 '12 at 08:04
0

I assume from your question you mean you have multiple view controllers in your app.

storeDate is only local to the method you've declared it in. I think you need to read up on variable scope in Objective-c

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
0

use this code.

in view 2.

#import "view2.h"
#import "SOFViewController.h"

@implementation view2
@synthesize dateLabel;

-(IBAction) goToView1: (id) sender{

  SOFViewController *mySOFViewController = [[SOFViewController alloc]                 initWithNibName:@"view2" bundle:nil];
  [self.view addSubview:mySOFViewController.view];
}

- (void)viewDidLoad {

   dateLabel.text = mySOFViewController.storeDate;
   [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];

}


- (void)dealloc {
    [super dealloc];
}

@end
fibnochi
  • 1,113
  • 1
  • 9
  • 26
  • Thanks, I have tried this but get 'mySOFViewController' undeclared, I will have to look into it further. Really not sure where I am going wrong. – TJ15 Feb 09 '12 at 12:37