简体   繁体   中英

Pass nsmutablearray from one viewcontroller to another view controller

Here I am adding values to nsmutablearray in the firstviewcontroller. When I click UIButton I want pass this array to another view controller.

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;

When I passing firstnamearray value to appointmentdataarray.

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

From the above the appointmentdataarray returning a null value.

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

您是否已分配阵列?

NSMutableArray* firstNameArray = [NSMutableArray array];

You can try one thing: in secondviewcontroller.m add a following method:

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

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

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

}

and add -(id) initwithnibName:(NSString*)_nib withArray:(NSMutableArray*)_array; to secondviewcontroller.h .

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

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

Hope this helps.

Try modifying statement:

  [appointmentViewObject setAppointmentDataArray:firstNameArray];

With this:

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

use this:

secondviewcontroller.h file

 NSMutableArray* AppointmentDataArray;

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

secondviewcontroller.m file

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

///////When you push

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

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