问题描述
我有一个具有多个视图的应用程序.在视图一上我创建和 NSDate
如下:
I have an app with mulitple views. On view one I create and NSDate
as follows:
NSString *storeDate = [[NSDate date] description];
在视图 2 中,在 viewDidLoad:
我想使用
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;
我已导入我的视图,但 storeDate
被标记为未声明.
I have imported my views but storeDate
is being flagged as undeclared.
知道如何让它发挥作用吗?
Any idea how I can get this to work?
SOFViewController.h
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
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
推荐答案
storeDate 的范围仅限于 view1 的方法.所以你不能访问它的其他视图,甚至是同一个类的其他方法.您必须创建storeDate"属性,然后您就可以在任何地方访问它
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
在实现中使用
@synthesize storeDate;
现在将 storeDate 的值设置为
now set the value of storeDate as
self.storeDate = [[NSDate date] description];
你可以在任何地方使用它.
and you can use it any where.
这篇关于将另一个视图上的标签设置为存储的 NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!