Как передать конкретные данные класса NSObject из одного класса в другой класс в IOS
В моем приложении ios я создал один объект NSModel для отображения tableList, и когда я коснулся строки tableList, я хочу перенести данные этого конкретного класса модели в другой класс
Как я могу это сделать?
мой код:
NSMutableArray * mainArray;
//TableList Delegate Methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return mainArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"HistoryCell";
// Similar to UITableViewCell, but
UITableViewCell *cell = (UITableViewCell *)[MaintableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
model1 = mainArray[indexPath.row];
cell.textLabel.text = model1.Name;
cell.detailTextLabel.text = model1.MasterId;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewController1 *Controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
Controller.indexPosition = indexPath.row;
[self.navigationController pushViewController:Controller animated:YES];
}
@end
ModelObject1:
#import "ModelObject1.h"
@implementation ModelObject1
@synthesize MasterId,Name;
-(void)loadingservices :(id)mainDictionary{
NSLog(@"loadingservices %@",mainDictionary);
Name = [mainDictionary objectForKey:@"Name"];
MasterId = [mainDictionary objectForKey:@"MasterId"];
}
@end
3 ответа
Создайте один объект модели в вашем контроллере Destination View
@property (сильный, неатомный) ModelClass *job;
в вашем выбранном методе получить эти данные модели и передать его
ModelClass *SelectedJob;
SelectedJob = [muarrJobs objectAtIndex:indexPath.row];
JobDescriptionViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"JobDescription"];
obj.job = SelectedJob;
[self.navigationController pushViewController:obj animated:YES];
Попробуйте как ниже:
1) Создайте один модельный объект в вашем ViewController1
@property (strong,nonatomic) ModelObject1 *modelObj;
2) Напишите ниже код в viewDidLoad
из ViewController1
,
modelObj = [[ModelObject1 alloc]init];
Затем замените метод didSelect следующим образом:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewController1 *Controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
Controller.indexPosition = indexPath.row;
ModelObject1 *model = mainArray[indexPath.row];
Controller.modelObj = model;
[self.navigationController pushViewController:Controller animated:YES];
}
Затем получите данные из класса модели в viewDidLoad
из ViewController1
e.g, NSLog("%@", modelObj.Name);
Таким образом, вы можете передать объект класса модели другому контроллеру представления. Надеюсь, что это поможет вам.
Используйте этот код,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewController1 *Controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
model1 = mainArray[indexPath.row];
Controller.modelVal = model1;
[self.navigationController pushViewController:Controller animated:YES];
}
Другой ViewControllerB:
ViewControllerB.h
объявить свойство Value
@property (strong, nonatomic)modelName *modelVal;
ViewControllerB.m
напечатать значение имени в viewDidLoad
:
NSLog(@"%@",modelVal.Name);
надеюсь, это полезно