我有一个带有4个标签的标签栏控制器。在我的3选项卡中,我有一个按钮,可以将您带到另一个带有日期选择器的视图中。我想通过完成按钮将此日期返回到我的3选项卡,但不幸的是数据不会返回。我找到了这篇文章How to pass data back from one view to other View in IOS?
我能够返回,但数据不会传输。这是我的代码:
NSDateFormatter *Format = [[NSDateFormatter alloc]init];
[Format setDateFormat:@"MMM dd, yyyy"];
NSString *Date = [Format stringFromDate:selectDate.date];
NSLog(Date);
BookArtistViewController *parentView = (BookArtistViewController *)[self.tabBarController.viewControllers objectAtIndex:2];
parentView.Date = Date;
[self.navigationController popViewControllerAnimated:YES];
感谢您的帮助!
答案 0 :(得分:1)
为什么不使用AppDelegate?您可以在AppDelegate中创建日期属性,然后为其分配UiDatePicker日期。
在AppDelegate.h中创建属性:
@property (strong, nonatomic) NSDate *myDate;
在包含UIDatePicker的视图控制器中导入AppDelegate.h然后在“完成”按钮中将所选日期保存到myDate中:
#import "myViewController.h"
#import "AppDelegate.h"
@interface myViewController ()
@property (strong, nonatomic) IBOutlet UIDatePicker *myUIDatePicker;
- (IBAction)saveDate:(id)sender;
@end
- (IBAction)saveDate:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.myDate = self.myUIDatePicker.date;
NSLog(@"%@", appDelegate.myDate );
}
现在,在你的第三个TAB中,导入AppDelegate.h,创建一个名为dateLabel的标签并创建其属性:
#import "myThirdTabViewController.h"
#import "AppDelegate.h"
@interface myThirdTabViewController ()
@property (strong, nonatomic) IBOutlet UILabel *dateLabel;
@end
并将此代码放在viewWillAppear:
中-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;
self.dateLabel.text = [dateFormatter stringFromDate:appDelegate.myDate];
}
所选日期将显示在标签中。