iOS頁(yè)面間傳值及自定義類拷貝問題
自定義一個(gè)TypesItem類,繼承自NSObject,含有三個(gè)變量(可自定義添加多個(gè))
TypesItem.h
- #import <Foundation/Foundation.h>
- @interface TypesItem : NSObject<NSCopying>
- {
- NSString *type_id;
- NSString *type_memo;
- NSString *type_name;
- }
- @property (nonatomic,copy) NSString *type_id;
- @property (nonatomic,copy) NSString *type_memo;
- @property (nonatomic,copy) NSString *type_name;
- @end
TypesItem.m文件中,除了要synthesize這三個(gè)變量之外
- @synthesize type_id,type_memo,type_name;
還要實(shí)現(xiàn)NSCopying協(xié)議方法
- (id)copyWithZone:(NSZone *)zone
- - (id)copyWithZone:(NSZone *)zone
- {
- TypesItem *newItem = [[TypesItem allocWithZone:zone] init];
- newItem.type_name = self.type_name;
- newItem.type_id = self.type_id;
- newItem.type_memo = self.type_memo;
- return newItem;
- }
頁(yè)面間傳值,假設(shè)A->B,A中的TypeItem的值要傳到B中
在B中.h文件寫上代碼
- @property(nonatomic,copy) TypesItem *selectedItem;
在B.m文件中
- @synthesize selectedItem;
在A.m中跳轉(zhuǎn)到B之前加上代碼
- BViewController *BVC = [[[BViewController alloc] initWithNibName:@"BViewController" bundle:nil] autorelease];
- // item為TypeItem類型,且不為空
- BVC.selectedItem = item;
- [self.navigationController pushViewController:BVC animated:YES];
PS:頁(yè)面間傳值時(shí),此處的BVC.selectedItem中的BVC一定與push過去的BVC保持一致,否則push到B界面中的selectedItem值必定為null。