先定義viewController 1簡稱爲A,viewController2簡稱爲B
從A內的屬性傳給B;
第1、在B內先定義屬性(Setter),供外部A調用;
比如:
B.h定義:
@property (nonatomic, strong) NSMutableArray* mysearchResultArray;
B.m中定義:
@synthesize mysearchResultArray;
接着2、在A定義到B的segue,即在storyboard中拖一根從A中按鈕或者其他控件到B面板的線,並定義:Identifier;比如:
特別注意右邊紅色框內的屬性別忘定義。
最後3、在A中定義方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"imageShow"])//此處判斷是否是我們之前定義的segue,一個面板是可以定義多個去向的。
{
HSOATUserCollectionViewController* targetController = [segue destinationViewController];//在A中得到B面板對象;
[targetController setMysearchResultArray:self.searchResultArray];//把A中的屬性設置到B中。
}
}
這樣基本就ok啦,你可以在B的下面方法中嘗試輸出屬性的值情況,比如這裏的數組長度:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%i",[[self mysearchResultArray] count]);
}
是不是很ok啊。
這裏需要注意的是:
1、segue方式和delegate方式是兩種方式,請勿爲了實現一個傳遞功能用兩種方式,當然,在整個項目中用哪種?甚至都用都無所謂啦。
2、storyboard裏面別忘了給A,B面板指定Class(即對應的viewController);
3、本人也是剛剛纔學習ios開發,如有描述錯誤,歡迎批評指正。[email protected]