如何使用iOS中的NSUserDefault值更新以前的视图标签,目标c

时间:2016-06-10 07:35:19

标签: objective-c uilabel nsuserdefaults

我的app中有两个视图。在第一个视图中有一个标签,我想在第二个视图控制器中更新它的值(这显示为一个弹出视图)。在第二个视图控制器被解除后,更改的值应该在标签中(如NSUserDefault)。我使用了- (IBAction)updatetheDatepicker:(id)sender { NSDate *flightDeparturedate = self.flightDepartureDatepicker.date; NSDateFormatter *flightdepatureFormatter = [[NSDateFormatter alloc] init]; [flightdepatureFormatter setDateFormat:@"d/MM/y"]; NSString *flightDeparturedateDisplay = [flightdepatureFormatter stringFromDate:flightDeparturedate]; NSLog(@"selected departure date :%@", flightDeparturedateDisplay); [[NSUserDefaults standardUserDefaults] setObject:flightDeparturedateDisplay forKey:@"flightDepartureDate"]; [[NSUserDefaults standardUserDefaults] synchronize]; }

在我的第二个视图控制器中,我分配如下值:

firstviewcontroller

并且在我的self.depdate.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"flightDepartureDate"]; 第二个视图被解雇时,我想在我的第一个视图中更新标签。我可以这样做吗。

UIPopoverpresentationController

注意:第二个视图显示为- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSString *identifier = segue.identifier; if([identifier isEqualToString:@"popover"]) { UIViewController *destinationController = segue.destinationViewController; UIPopoverPresentationController *popController = destinationController.popoverPresentationController; if(popController) { popController.delegate = self; } } if([identifier isEqualToString:@"popoverone"]) { UIViewController *destinationController = segue.destinationViewController; UIPopoverPresentationController *popController = destinationController.popoverPresentationController; if(popController) { popController.delegate = self; } } } - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller { return UIModalPresentationNone;; }

这是我从第一个视图控制器

弹出第二个视图控制器的方法
  var Promise = require('bluebird');

  // slow method that takes a second to return
  var asyncCall = (value) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => resolve(value), 1000);
    });
  }

  // things to iterate over
  var values = [1,2,3,4];

  // using Promise, iterate over the promises
  Promise.each(values, (value) => asyncCall(value).then(() => console.log(value)));

  // if you have some other promise library instead of bluebird, you
  // can manually implement it something like this.
  // please be careful with error handling
  function sequentialEach(values, cb) {
    if (values.length === 0) { return };
    try {
      var result = cb(values[0]);
      if (result && result.then) {
        return result.then(() => sequentialEach(values.slice(1), cb));
      } else {
        return sequentialEach(values.slice(1), cb);
      }
    } catch (e) {
      // assuming your library has .reject, you could alternatively
      // new Promise((resolve, reject) => reject(e))
      return Promise.reject(e);
    } 
  }

希望你对此有所帮助。

2 个答案:

答案 0 :(得分:1)

注意:这里我没有使用任何segue。我启动了视图控制器

FirstViewController.h

@interface FirstViewController : UIViewController<UIPopoverPresentationControllerDelegate>
@property(nonatomic,strong)IBOutlet UILabel *depdate;


@end

FirstViewController.m

    -(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self updateDateLable];

}

- (IBAction)btnClicked:(id)sender
{

    //[self performSegueWithIdentifier:@"openDatePicker" sender:self];
    UIButton *btnInfo=(UIButton *)sender;
    DatePickerPopUpViewController* popOverFileProperty = [[self storyboard] instantiateViewControllerWithIdentifier:@"DatePickerPopUpViewController"];

    popOverFileProperty.modalPresentationStyle = UIModalPresentationPopover;
    popOverFileProperty.popoverPresentationController.sourceView = btnInfo;
    popOverFileProperty.popoverPresentationController.delegate=self;
    [self presentViewController:popOverFileProperty animated:YES completion:nil];


}


-(void)updateDateLable
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"flightDepartureDate"] != nil) {
        self.depdate.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"flightDepartureDate"];
    }
    else
    {
        //date is not set yet , set other text as you wish
        self.depdate.text=@"Date not selected";
    }
}


// Called on the delegate when the user has taken action to dismiss the popover. This is not called when the popover is dimissed programatically.
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
{
    [self updateDateLable];
}

如果你想使用Segue:

- (IBAction)btnClicked:(id)sender
{

    [self performSegueWithIdentifier:@"openDatePicker" sender:self];

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString:@"openDatePicker"])
    {
        DatePickerPopUpViewController* popOverFileProperty = [segue destinationViewController];
        popOverFileProperty.modalPresentationStyle = UIModalPresentationPopover;
        popOverFileProperty.popoverPresentationController.delegate=self;

    }
}

答案 1 :(得分:0)

尝试从NSUserDefaults调用您的更新值。

-(void)viewWillAppear:(BOOL)animated 
 {
 [super viewWillAppear:animated];

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"flightDepartureDate"] != nil||![[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"flightDepartureDate"]] isEqualToString:@"(null)"]||![[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"flightDepartureDate"]] isEqualToString:@""]) {
    self.lbl.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"flightDepartureDate"];
}
else
    self.lbl.text=@"NO Date..";

 }