model传值

本文主要探讨了在iOS开发中,如何在不同视图控制器之间传递Student模型的值,通过详细解析`Student.h`、`Student.m`以及`RootViewController.m`中的关键代码,展示了模型数据在应用程序中的流动过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject

@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *hobby;
@property(nonatomic,copy)NSString *address;
@property(nonatomic,copy)NSString *phone;
@property(nonatomic,copy)NSString *sex;
@property(nonatomic,copy)NSString *age;

@end

2.Student.m

#import "Student.h"

@implementation Student

//这个方法是KVC里负责纠错的方法,只要Key和属性名没有对应上,就会执行这个方法
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{

}

@end

3.RootViewController.m

#import "RootViewController.h"
#import "Student.h"
#import "Movie.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
//定义model属性
@property(nonatomic,retain)NSMutableArray *modelArr;
@property(nonatomic,retain)NSMutableArray *movieArr;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor cyanColor];
    UITableView *tableView=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.delegate=self;
    tableView.dataSource=self;


    [self createMovie];

}
-(void)createMovie{

    NSString *path=@"/Users/dllo/Desktop/UI/UI10_KVC/UI10_KVC/movielist.txt";
    NSData *data=[NSData dataWithContentsOfFile:path];
    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSArray *movieArr=dic[@"result"];
    self.modelArr=[NSMutableArray array];
    for (NSDictionary *dic in movieArr) {
        Movie *movie=[[Movie alloc] init];
        [movie setValuesForKeysWithDictionary:dic];
        [self.modelArr addObject:movie];
        [movie release];
    }

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse=@"reuse";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    Student *stu=self.modelArr[indexPath.row];
    cell.textLabel.text=stu.name;

    return cell;
}

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

}

-(void)cresteData{
    NSString *path=[[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
    NSMutableArray *arr=[NSMutableArray arrayWithContentsOfFile:path];
    NSLog(@"%@",arr);
    self.modelArr=[NSMutableArray array];
    for (NSDictionary *dic in arr) {
        Student *stu=[[Student alloc] init];
        [stu setValuesForKeysWithDictionary:dic];
        NSLog(@"%@",stu.name);
        [self.modelArr addObject:stu];
        [stu release];
    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值