NSString在segue结束时被擦除



我一直在将xcode项目更新为故事板样式,并将旧xcode xib中的三个视图更改为新故事板样式上的三个查看控制器。我创建的将数据从一个视图控制器传递到另一个视图控制器的字符串出现问题,当我切换到第三个视图控制器以获取更多信息时,带有初始数据的第一个标签变为空白。

我有三个视图控制器。第一个是字符串*concentrationString和*DOBString。这是在其他视图上选择的数据的显示位置。

FirstViewController.h

@interface FirstViewController : UIViewController {
    IBOutlet UILabel *concentrationlabel;
    IBOutlet UILabel *DOBlabel;
}
@property (strong, nonatomic) NSString *concentrationString;
@property (strong, nonatomic) NSString *DOBString;

FirstViewController.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方法将该值发回时,第一个标签变为空白,尽管我没有指定更改concentrationString

#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

我该怎么做?提前谢谢。

谨致问候,库尔特。

这不起作用的原因是您正在为同一个视图控制器(即第一个视图控制器)创建两个不同的对象。为了从第一个控制器中可见的两个控制器获得更改,您必须在两个控制器中获得第一个视图控制器的相同引用。

可以通过为AppDelegate类中的第一个视图控制器创建IBOutlet作为属性来完成@property(nonatomic,strong)IBOutlet FirstViewController*FirstViewController;

然后通过导入AppDelegate.h:在两个类中使用代码

   AppDelegate *app=[UIApplication sharedApplication].delegate;
   FirstViewController *vc=(FirstViewController *)app.firstViewController;
   vc=[[segue destinationViewController];
Now vc will refer to the same class rather than two different objects of the FirstViewController and then you can see the changes done together.

尝试这样更改FirstViewController。。。

FirstViewController.h

@interface FirstViewController : UIViewController
@property (strong, nonatomic) NSString *concentrationString;
@property (strong, nonatomic) NSString *dOBString;
@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
// anything that isn't accessed from outside this class should be
// in the private category extension.
@property (nonatomic, weak) IBOutlet UILabel *concentrationLabel;
@property (nonatomic, weak) IBOutlet UILabel *dOBLabel;
@end
@implementation FirstViewController
...
- (void)viewDidLoad
{
    [super viewDidLoad];
     self.concentrationLabel.text = self.concentrationString;
     self.dOBLabel.text = self.dOBString;
}
...

我认为你把程序与ivar和属性混淆了,它们实际上并没有指向同一个东西。

使用特性*时,请始终使用self.property引用该特性。(除非在应该使用_property的getter、setter或构造函数中)。

通过这种方式更改代码,您应该会发现它是有效的。

此外,变量名以小写字母开头,并使用驼色大小写。

即DOBlabel不正常…DOBlabel正常。concentrationlabel不正常…concentrationlabel正常。

最新更新