Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 存储来自另一个视图控制器的数组值_Ios_Objective C_Iphone_Nsmutablearray - Fatal编程技术网

Ios 存储来自另一个视图控制器的数组值

Ios 存储来自另一个视图控制器的数组值,ios,objective-c,iphone,nsmutablearray,Ios,Objective C,Iphone,Nsmutablearray,我有两个视图控制器(viewController和popUpViewController)。当我打开应用程序时,viewController将加载。它有按钮。当我点击按钮时,图像将在popUpViewController中打开。然后我单击popUpViewController上的关闭按钮,我将关闭popUpViewController。我的情况是,当我在viewController中单击按钮时,我必须获取打开时间,而在popUpViewController上单击关闭按钮时,我必须获取结束时间。两

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

viewController.m

[baseObj storeArrayFromPopUp :openedFileName second:fileOpenStime third:fileOpenEtime];
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后。FileOpenedar显示为nil


请给出建议。

在添加对象之前,您是否已分配初始化FileOpenedar。如何创建baseObj。是的,在viewdidload本身中。@Vishnuvardhan ViewServiceWController*baseObj=[[ViewServiceWController alloc]init];如果分配ViewerVC,它将是一个全新的实例。很明显,数组值将为零。。在您的情况下,我建议将NSArray移到全局变量。(类似于在appdelegate中)或共享实例在添加对象之前,请先分配初始化fileOpenedArr。如何创建basObj。是的,在viewdidload本身中。@Vishnuvardhan ViewerViewController*baseObj=[[ViewerViewController alloc]init];如果分配ViewerVC,它将是一个全新的实例。很明显,数组值将为零。。在您的情况下,我建议将NSArray移到全局变量。(类似于appdelegate)或共享实例
    @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;
}