简体   繁体   中英

store array values from another view controller

I have two view controllers(viewController and popUpViewController). When i open app, viewController will load. It has buttons. When i click on button, image will be opened in popUpViewController. Then i click close button on popUpViewController,i am dismissing popUpViewController. My scenario is, when i click button in viewController i have to take opened time and i click close button on popUpViewController, i have to take end time.Both should be stored in an array. But array is showing as nil. I am using the following code.

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];

After calling storeArrayFromPopUp. fileOpenedArr is showing as nil.

Please advice.

    @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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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