gpt4 book ai didi

ios - 如何在 ios 中将 NSobject 类数据从一个类传递到另一个类?

转载 作者:行者123 更新时间:2023-11-29 11:59:03 25 4
gpt4 key购买 nike

您好,我是 ios 的新手,在我的应用程序中,我创建了一个 NSModel 对象来显示 tableList,当我点击 tableList 行时,我想将该模型类对象数据带到另一个类。

我该怎么做?

View Controller :-

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

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;

newBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[newBtn setFrame:CGRectMake(250,5,30,30)];
[newBtn addTarget:self action:@selector(urSelctor:) forControlEvents:UIControlEventTouchUpInside];
UIImage *btnImage = [UIImage imageNamed:@"uncheck.png"];
[newBtn setImage:btnImage forState:UIControlStateNormal];
[cell addSubview:newBtn];

return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

ViewController1 *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
//ModelObject1 *model = mainArray[indexPath.row];
model1 = mainArray[indexPath.row];
controller.modelobj = model1;
controller.modelobjArray = mainArray;
[self.navigationController pushViewController:controller animated:YES];
}

-(void)urSelctor :(UIButton*)sender{

UIImage *currentImage = sender.imageView.image;
isValidate = [currentImage isEqual:[UIImage imageNamed:@"uncheck.png"]]?NO:YES;
NSLog(@"VALUE IS : %@", (isValidate) ? @"YES" : @"NO");
[sender setImage:[UIImage imageNamed:((isValidate) ? @"uncheck.png" : @"check.png")] forState:UIControlStateNormal];

if (isValidate) {
model1.ischeck = YES;
}else{
model1.ischeck = NO;
}
}

@end

模型对象1:-

#import "ModelObject1.h"

@implementation ModelObject1

@synthesize MasterId,Name,ischeck;

-(void)loadingservices :(id)mainDictionary{

NSLog(@"loadingservices %@",mainDictionary);

Name = [mainDictionary objectForKey:@"Name"];
MasterId = [mainDictionary objectForKey:@"MasterId"];
}

@end

View Controller 1:-

#import "ViewController1.h"

@interface ViewController1 ()

@end

@implementation ViewController1
@synthesize modelobj,modelobjArray;

- (void)viewDidLoad {
[super viewDidLoad];

for (int i=0; i<modelobjArray.count;++i) {

modelobj = modelobjArray[i];
NSLog(@"Name is=======>%@",modelobj.Name);
NSLog(@"Check value is%hhd",modelobj.ischeck);
}
}

最佳答案

将您的模型类属性创建到目标 View Controller 和源 Controller 中,创建目标 Controller 的对象并设置模型属性的值。

像这样修改你的方法:

-(void)urSelctor :(UIButton*)sender{
model1 = mainArray[indexPath.row];
if (model1.ischeck)
{
model1.ischeck = NO;
[sender setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
}
else
{
model1.ischeck = YES;
[sender setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
}
}

关于ios - 如何在 ios 中将 NSobject 类数据从一个类传递到另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37807753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com