将nsmutablearray从一个视图控制器传递到另一个视图控制器

问题描述:

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

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值传递给appointmentdataarray。

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

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

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

检查firstNameArray页头或没有? –

+0

是firstNameArray是alloc – vijayan

+0

什么问题? –

你分配了数组吗?

NSMutableArray* firstNameArray = [NSMutableArray array]; 
+0

不过,我得到null – vijayan

+0

secondViewController上的AppointmentDataArray是什么类型的属性?它应该很强或保留。 –

您可以尝试一两件事: 在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" withArray:firstNameArray];

希望这有助于更换secondviewcontroller *appointmentViewObject = [[secondviewcontroller alloc]initWithNibName:@"secondviewcontroller" bundle:nil]; [appointmentViewObject setAppointmentDataArray:firstNameArray];

尝试修改声明:

[appointmentViewObject setAppointmentDataArray:firstNameArray]; 

有了这个:

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

使用本:

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