第2ビューコントローラモーダルファイル。
#import "View2Controller.h"
@interface View2Controller()
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@end
@implementation View2Controller
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
for (id object in self.array) {
// do something with object
NSLog(@"%@", object);
}
self.textLabel.text = self.array.lastObject;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
ヘッダーに配列を宣言します。
#import <UIKit/UIKit.h>
@interface View2Controller : UIViewController
@property NSArray *array;
@end
ここにメインビューコントローラがあります。
#import "ViewController.h"
#import "View2Controller.h"
@interface ViewController()
@property NSArray *array;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.array = [NSArray arrayWithObjects:@"test",@"string2", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnListClicked:(UIButton *)sender {
[self performSegueWithIdentifier:@"passData" sender:sender];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"passData"]) {
View2Controller *vc = [segue destinationViewController];
vc.array = self.array;
}
}
@end
マイストーリーボード -
[ビューコントローラ間でデータを受け渡す]の可能複製(http://stackoverflow.com/questions/ 5210535/passing-data-between-view-controllers) –
配列を使用する場合は、objectAtIndexメソッドを使用します。 – Pavankumar
[ビューコントローラ間の変数の受け渡し]の可能な複製(0120-919-03-xx) – User511