【问题标题】:Pass nsmutablearray from one viewcontroller to another view controller将 nsmutablearray 从一个视图控制器传递到另一个视图控制器
【发布时间】:2013-05-15 09:59:09
【问题描述】:

在这里,我将值添加到 firstviewcontroller 中的 nsmutablearray。当我单击 UIButton 时,我想将此数组传递给另一个视图控制器。

AppointmentClass *appObj = [[AppointmentClass alloc]init];
appObj.subject = [key objectForKey:@"Subject"];
appObj.location = [key objectForKey:@"Location"];
appObj.scheduledStart = [key objectForKey:@"ScheduledStart"];
appObj.scheduledEnd = [key objectForKey:@"ScheduledEnd"];
[firstNameArray addObject:appObj];            
[appObj release];
appObj=nil;

当我将 firstnamearray 值传递给约会数据数组时。

secondviewcontroller *appointmentViewObject = [[secondviewcontroller alloc]initWithNibName:@"secondviewcontroller" bundle:nil];
[appointmentViewObject setAppointmentDataArray:firstNameArray];

从上面的约会数据数组返回一个空值。

[self presentModalViewController:appointmentViewObject animated:YES];
[appointmentViewObject release];

【问题讨论】:

  • 检查 firstNameArray alloc 与否?
  • 是 firstNameArray 是 alloc
  • 问题是什么?
  • 跳过presentModalViewController。并且一旦尝试 [self.navigationcontroller pushviewController:animated:];方法

标签: iphone ios


【解决方案1】:

你分配了数组吗?

NSMutableArray* firstNameArray = [NSMutableArray array];

【讨论】:

  • secondViewController 上的 AppointmentDataArray 是什么类型的属性?它应该是强大的或保留的。
【解决方案2】:

你可以尝试一件事: 在secondviewcontroller.m添加如下方法:

-(id) initwithnibName:(NSString*)_nib withArray:(NSMutableArray*)_array{

  self = [super initWithNibName:_nib bundle:nil];

if (self) {
    self.array =_array;
}
return self;

}

并将-(id) initwithnibName:(NSString*)_nib withArray:(NSMutableArray*)_array; 添加到secondviewcontroller.h

现在替换secondviewcontroller *appointmentViewObject = [[secondviewcontroller alloc]initWithNibName:@"secondviewcontroller" bundle:nil]; [appointmentViewObject setAppointmentDataArray:firstNameArray];

secondviewcontroller *appointmentViewObject = [[secondviewcontroller alloc]initwithnibName:@"secondviewcontroller" withArray:firstNameArray];

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    尝试修改语句:

      [appointmentViewObject setAppointmentDataArray:firstNameArray];
    

    有了这个:

      [appointmentViewObject setAppointmentDataArray:[NSArray arrayWithArray:firstNameArray]];
    

    【讨论】:

      【解决方案4】:

      使用这个:

      secondviewcontroller.h 文件

       NSMutableArray* AppointmentDataArray;
      
       @property(nonatomic,retain) NSMutableArray* AppointmentDataArray;
       -(void)setMyArray:(NSMutableArray *)arr;
      

      secondviewcontroller.m 文件

       @synthesize AppointmentDataArray;
      
       - (id)initWithNibName:(NSString *)nibNameOrNil 
                  bundle:(NSBundle *)nibBundleOrNil
       {
            if ((self = [super initWithNibName:nibNameOrNil 
                                  bundle:nibBundleOrNil])) {
               // Custom initialization.
                 AppointmentDataArray = [[NSMutableArray alloc] init];
             }
          return self;
        }
      
         -(void)setMyArray:(NSMutableArray *)arr{
                AppointmentDataArray=arr;
          }
      

      ///////当你按下时

         secondviewcontroller *appointmentViewObject = [[secondviewcontroller alloc]initWithNibName:@"secondviewcontroller" bundle:nil];
         [appointmentViewObject setMyArray:firstNameArray];
         [self presentModalViewController:appointmentViewObject animated:YES];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多