代码之家  ›  专栏  ›  技术社区  ›  Hanz Cheah

NSMutable数组在另一个方法中不返回值

  •  0
  • Hanz Cheah  · 技术社区  · 7 年前

    我已经在一个方法中创建了一个NSMutable数组并将其添加到其中。下面是代码

    -(void)setupSegmentButtons {
    
    NSInteger numControllers = 7;
    
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *beginningOfThisWeek;
    NSTimeInterval durationOfWeek;
    
    [calendar rangeOfUnit:NSWeekCalendarUnit
                startDate:&beginningOfThisWeek
                 interval:&durationOfWeek
                  forDate:now];
    
    NSMutableArray *dtDate = [@[] mutableCopy];
    
    NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:now];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM/YYYY"];
    
    for (int i = 0; i<numControllers; i++) {
    
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT)];
    
        [navigationView addSubview:button];
    
        NSString *dateString = [dateFormatter stringFromDate:[calendar dateFromComponents:comps]];
    
        [dtDate addObject:dateString];
    
        [button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
        ++comps.day;
    }
    
    //****** Manage to display my array with all the dates
    NSLog(@" Can get my array : %@",dtDate);
    
    }
    

    但当按下按钮并调用下面的另一个方法时,NSMutable数组dtDate返回(null),为什么?

    -(void)tapSegmentButtonAction:(UIButton *)button {
    
    //Can't get the NSMutable Array here 
    NSLog(@" Returns Null : %@",dtDate);
    
    NSString *txtDate = dtDate[0];
    //Returns NULL
    NSLog(@"txtDate=%@",txtDate);
    }
    
    4 回复  |  直到 7 年前
        1
  •  1
  •   CRD    7 年前

    您的方法 tapSegmentButtonAction: 访问权限 dtDate 没有 任何声明,指示它是实例或全局变量。

    您的方法 setupSegmentButtons 声明 变量 dtDate日期 创建一个局部变量,该变量隐藏具有相同名称的任何实例或全局变量。

    而不是:

    NSMutableArray *dtDate = [@[] mutableCopy];
    

    您应该尝试:

    dtDate = [NSMutableArray new]; 
    

    HTH公司

        2
  •  1
  •   Akhil    7 年前

    dtDate是方法中的局部变量。。

    如果您有用于存储dtDate变量的属性

    您应该执行

    self.yourProperty = dtDate
    
        3
  •  0
  •   Nilesh Jha    7 年前

    //将其作为全局变量写入

    NSMutableArray *dtDate;
    
    -(void)setupSegmentButtons {
    
    NSInteger numControllers = 7;
    
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *beginningOfThisWeek;
    NSTimeInterval durationOfWeek;
    
    [calendar rangeOfUnit:NSWeekCalendarUnit
                startDate:&beginningOfThisWeek
                 interval:&durationOfWeek
                  forDate:now];
    
    dtDate = [NSMutableArray alloc] init];
    
    NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:now];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM/YYYY"];
    
    for (int i = 0; i<numControllers; i++) {
    
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT)];
    
        [navigationView addSubview:button];
    
        NSString *dateString = [dateFormatter stringFromDate:[calendar dateFromComponents:comps]];
    
        [dtDate addObject:dateString];
        NSLog(@"dtDate inside loop =%@",dtDate);// for checking wheter at ths instance You have value or not
        button.tag = i;
    
        [button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
        ++comps.day;
    }
    
    //****** Manage to display my array with all the dates
    NSLog(@" Can get my array : %@",dtDate);
    
    }
    
    -(void)tapSegmentButtonAction:(UIButton *)button {
    
    //Can't get the NSMutable Array here 
    NSLog(@" Returns Null : %@",dtDate);
    
    NSString *txtDate = dtDateArray[0];
    //Returns NULL
    NSLog(@"txtDate=%@",txtDate);
    }
    
        4
  •  0
  •   Hanz Cheah    7 年前

    在我将NSMutable数组放在viewDidLoad之后,它就变成了全局数组

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    
    dtDate = [[NSMutableArray alloc] init];
    }
    

    我真的不明白为什么,如果有人能解释的话,那就太好了。