从另一个视图控制器存储数组值

时间:2015-08-18 07:24:02

标签: ios objective-c iphone nsmutablearray

我有两个视图控制器(viewController和popUpViewController)。当我打开app时,viewController将加载。它有按钮。当我点击按钮时,图像将在popUpViewController中打开。然后我点击popUpViewController上的关闭按钮,我正在解雇popUpViewController。我的情况是,当我在viewController中单击按钮时,我必须打开时间,然后单击popUpViewController上的关闭按钮,我必须占用结束时间。两者应该存储在一个数组中。但阵列显示为零。我使用以下代码。

viewController.m

viewDidLoad中:

fileOpenedValues = @[@"",@"" ,@""];
fileOpenedKeys = @[@"File Name",@"STime",@"ETime"];
fileOpened = [[NSMutableDictionary alloc] initWithObjects:fileOpenedValues forKeys:fileOpenedKeys];
[fileOpenedArr addObject:fileOpened];


-(void) storeArrayFromPopUp :(NSString *)fname second:(NSString *)mname third:(NSString *)lname
{
 fileOpenedValues = @[fname ,mname ,lname];
 fileOpenedKeys = @[@"File Name",@"STime",@"ETime"];
 fileOpened = [[NSMutableDictionary alloc] initWithObjects:fileOpenedValues forKeys:fileOpenedKeys];    
 [fileOpenedArr addObject:fileOpened];
}

popUpViewController.m

[baseObj storeArrayFromPopUp :openedFileName second:fileOpenStime third:fileOpenEtime];

调用storeArrayFromPopUp之后。 fileOpenedArr显示为nil。

请建议。

1 个答案:

答案 0 :(得分:0)

    @implementation ViewController {


        NSString *strStartTime;
        NSString *strEndTime;
        NSMutableArray *arrTime;
    }

    - (void)viewDidLoad {
        [super viewDidLoad];

        arrTime = [[NSMutableArray alloc]init];
        strStartTime = @"";
        strEndTime = @"";
    }

    -(void)viewWillAppear:(BOOL)animated {

        //check start has value means popupview controller was open.
        //viewWillAppear always call when you close the popup.
        if (strStartTime.length > 0) {
            strEndTime = [self getCurrentTime];

            NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:strStartTime,@"StartTime",strEndTime,@"EndTime", nil];
            [arrTime addObject:dic]; //you can have all the start and endtime details in the arrTime array.
            strStartTime = @"";
            strEndTime = @"";
        }
    }

    -(IBAction)btnShowPopupClicked:(id)sender {
        //Set the start time when popup is going to open.
        strStartTime = [self getCurrentTime];
        [self performSegueWithIdentifier:@"imagePopup" sender:nil];

    }

-(NSString *)getCurrentTime {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    NSString *currentTime = [dateFormatter stringFromDate:[NSDate date]];

    return currentTime;
}