我一直在努力将 xcode 项目更新为故事板样式,并将旧 xcode xib 中的三个视图更改为新故事板样式上的三个视图控制器,我创建的字符串有问题,用于将数据从一个视图控制器传递到其他,当我切换到第三个视图控制器以获取更多信息时,出生日期,带有初始数据的第一个标签变为空白。
我有三个视图控制器。第一个是我有字符串 *concentrationString 和 *DOBString 的地方。这是显示在其他视图上选择的数据的位置。
第一视图控制器.h
@interface FirstViewController : UIViewController {
IBOutlet UILabel *concentrationlabel;
IBOutlet UILabel *DOBlabel;
}
@property (strong, nonatomic) NSString *concentrationString;
@property (strong, nonatomic) NSString *DOBString;
第一视图控制器.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize concentrationString, DOBString;
...
- (void)viewDidLoad
{
[super viewDidLoad];
concentrationlabel.text = concentrationString;
DOBlabel.text = DOBString;
...
SecondViewController.m 有几个药物按钮,告诉第一个视图控制器使用我找到的 prepareForSegue 东西将什么值放入浓度标签
#import "SecondViewController.h"
#import "FirstViewController.h"
...
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"Adrenaline"]) {
NSString *concentrationString = @"Adrenaline";
FirstViewController *vc = [segue destinationViewController];
vc.concentrationString = concentrationString; }
else if ([segue.identifier isEqualToString:@"Morphine"]) {
NSString *concentrationString = @"Morphine";
FirstViewController *vc = [segue destinationViewController];
vc.concentrationString = concentrationString; }
它根据单击的按钮使用“肾上腺素”或“吗啡”更新了第一个视图上的标签。
当用户转到 ThirdViewController.m 从选择器中选择患者的出生日期并且我使用 prepareForSegue 方法将该值发回时,第一个标签变为空白,尽管我没有指定对浓度字符串的更改
#import "DOBViewController.h"
#import "FirstViewController.h"
@interface DOBViewController ()
@end
@implementation DOBViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
NSDate *now = [NSDate date];
[picker setDate:now animated:YES];
[DOBset setTitle:@"Date of birth" forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)displayDate:(id)sender {
// NSDate * selected = [picker date];
// NSString * date = [selected description];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"dd-MMMM-yyy"]; //24hr time format
NSString *dateString = [outputFormatter stringFromDate:picker.date];
[DOBset setTitle:dateString forState:UIControlStateNormal];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"DOB"]) {
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"dd-MMMM-yyy"]; //24hr time format
NSString *DOBString = [outputFormatter stringFromDate:picker.date];
FirstViewController *vc = [segue destinationViewController];
vc.DOBString = DOBString;
}
}
@end
我应该如何工作?先感谢您。
最好的问候,库尔特。