[英]How to pass data back from a view to a view in a tab bar controller with a done button
我有一個帶有4個標簽的標簽欄控制器。 在我的3選項卡中,我有一個按鈕,可將您帶到另一個帶有日期選擇器的視圖。 我想通過“完成”按鈕將此日期返回到我的3選項卡,但是不幸的是數據沒有返回。 我已經找到本文如何在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];
謝謝您的幫助!
您為什么不使用AppDelegate? 您可以在AppDelegate中創建一個date屬性,然后為其分配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];
}
所選日期將出現在標簽中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.