简体   繁体   English

从UIDatePicker在UIViewControllers之间传递数据

[英]passing data between UIViewControllers from UIDatePicker

Let me begin to tell you that I am new to Objective C. I have just finished Big Nerd Ranch's book and i want to create a real simple and basic app to learn more. 让我开始告诉您,我是Objective C的新手。我刚读完Big Nerd Ranch的书,我想创建一个真正简单且基本的应用程序以了解更多信息。 My idea was to create an app that will calculate the weeks between 2 dates. 我的想法是创建一个将计算两个日期之间的星期数的应用程序。 I have created a class for that and tested it. 我为此创建了一个类并对其进行了测试。 That works. 这样可行。

As you can see below, I have created to views (programmatically), One with the dates and the other will become visible when you click on start or end date. 正如您在下面看到的,我已经创建了视图(以编程方式),其中一个带有日期,另一个在单击开始或结束日期时可见。

If you select a date and click on the button 'calculate weeks', you will go back to the first view. 如果选择一个日期并单击“计算周”按钮,将返回到第一个视图。 No my big question is, how do I get this selected value back to my main screen? 我的主要问题是,如何将所选的值返回到主屏幕? I have tried several possibilities and search the web for information, but I couldn't get is to work. 我尝试了几种可能性,并在网络上搜索信息,但是无法正常工作。 I know this should be real easy, but for me at this moment it isn't. 我知道这应该真的很容易,但是对我而言,现在还不是。 :-) :-)

I have created a NSMutableArray that contains the values "Start date" and "End date". 我创建了一个NSMutableArray,其中包含值“开始日期”和“结束日期”。 My idea was to add the value of the UILabel from the SelectDateView to this array. 我的想法是将SelectDateView中的UILabel的值添加到此数组。 I have created a property In the inputview @property (readwrite, retain) NSMutableArray *datesArray; 我在inputview @property中创建了一个属性@property (readwrite, retain) NSMutableArray *datesArray; for that. 为了那个原因。

in the selectDateViewController i have created another property @property (nonatomic, assign) BITInputViewController *ivc; 在selectDateViewController中,我创建了另一个属性@property (nonatomic, assign) BITInputViewController *ivc; so I (in my opinion) can add a value to datesArray. 所以我(我认为)可以为datesArray添加一个值。

When I select a date this method is called, it works for the UILabel on SelectDateView, but doesn't do anything with the datesArray. 当我选择一个日期时,将调用此方法,该方法适用于SelectDateView上的UILabel,但对dateArray则不起作用。

- (void)LabelChange:(id)sender{

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;

dateLabel.text = [NSString stringWithFormat:@"%@",
                  [df stringFromDate:datePicker.date]];

[ivc.datesArray addObject:dateLabel.text];

if (ivc.datesArray) {

    for (NSString *d in ivc.datesArray) {
        NSLog(@"This is in datesArray %@",d);
    }
}else NSLog(@"!ivc.datesArray");

} }

When I test the app and select a date, I always see "!ivc.datesArray" in the log file. 当我测试应用程序并选择日期时,我总是在日志文件中看到“!ivc.datesArray”。

I also do this check of ivc.datesArray in -(void)viewWillAppear:(BOOL)animated and here I see the current date, as I set this date in - (void)viewDidLoad with this dateLabel.text = [NSString stringWithFormat:@"%@",[df stringFromDate:[NSDate date]]]; 我也做这个检查ivc.datesArray的-(void)viewWillAppear:(BOOL)animated ,在这里我看到当前的日期,因为我在设置这个日期- (void)viewDidLoad与此dateLabel.text = [NSString stringWithFormat:@"%@",[df stringFromDate:[NSDate date]]];

(When I print out the array in the inputview, it does show start date and end date, but not the selected date. ) (当我在inputview中打印出数组时,它确实显示开始日期和结束日期,但不显示所选日期。)

Hopefully someone can give me a few pointers on this. 希望有人能给我一些指导。

declare a NSString* date above the @interface in your view1.m file and the create a method in tht first class it should be something like 在view1.m文件中@interface上方声明一个NSString* date ,并在第一个类中创建一个方法,它应该类似于

-(void)passDate:(NSString *)dateString
{

  date = [NSString stringWithFormat:@"%@",dateString];

}

and in your second class create object of first class like
View1 * vc = [[View1 alloc]init];
[vc passDate:df];

Hope it works. 希望它能工作。 You have to declare NSString before the @interface and no property and synthesize. 您必须在@interface之前声明NSString ,并且没有属性和synthesize。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM