您好我是ios的新手,在我的应用程序中,我创建了一个用于显示tableList的NSModel对象,当我点击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";
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
#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
#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);
}
}
答案 0 :(得分:1)
将property
model class
创建到目标视图控制器和源控制器中,创建目标控制器的对象并设置模型属性的值。
像这样修改你的方法:
-(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];
}
}
答案 1 :(得分:0)
关注此
目标视图中的控制器.h文件创建此属性
#import <UIKit/UIKit.h>
#import "YourObjectClass.h"
@interface DestinationView : UIViewController
@property (strong, nonatomic) YourObjectClass *objectValue;
@end
当你需要通过时
DestinationView *newObjc = // Create object of class
之后
newObjc.objectValue = yourObjectValue;
希望有所帮助
答案 2 :(得分:0)
试试这个,可能这对你有帮助.. :))
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewController1 *fController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
model1 = mainArray[indexPath.row];
fController.modelVal = model1;
[self.navigationController pushViewController:Controller animated:YES];
}
在下一个视图控制器中:
声明属性
@property (strong, nonatomic)modelName *modelVal;
在.m
档案
viewDidLoad
中的:
NSLog(@"%@",modelVal.Name);
要处理按钮,请使用:
-(void)checkboxSelected:(id)sender {
model1 = mainArray[indexPath.row];
if([checkbox1 isSelected]==YES) {
[checkbox1 setSelected:NO];
} else {
[checkbox1 setSelected:YES];
}