【问题标题】:ios-passing data between two view controllers without segue?ios在两个视图控制器之间传递数据而没有segue?
【发布时间】:2017-01-11 21:53:57
【问题描述】:
我想将数据从一个视图控制器传递到另一个视图控制器,该控制器不使用 segue.in firstviewcontroller 当按钮触摸(收藏夹)希望数据传递到作为 tabbarviewcontroller 一部分的 secondviewcontroller(favoriteviewcontroller)时。
我知道最好的解决方案可能是使用委托?但是我该怎么做呢?
【问题讨论】:
标签:
ios
objective-c
button
delegates
pass-data
【解决方案1】:
- (IBAction)showFavouriteViewController:(UIButton *)sender {
//Create an instance of FavouriteViewController
FavouriteViewController *fVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FavouriteViewController"];
//Set public property of FavouriteViewController (these are the data you wanted to send)
fVC.favouriteArray = @[@"Apple", @"Orange"];
//Show the FavouriteViewController
[self.navigationController showViewController:fVC sender:nil];
}
- (IBAction)showFavouriteViewController:(UIButton *)sender {
//Create an instance of FavouriteViewController
FavouriteViewController *fVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FavouriteViewController"];
//Set public property of FavouriteViewController (these are the data you wanted to send)
fVC.favouriteArray = @[@"Apple", @"Orange"];
//Show the FavouriteViewController
[self.navigationController pushViewController:fVC animated:YES];
}
- (IBAction)showFavouriteViewController:(UIButton *)sender {
//Create an instance of FavouriteViewController
FavouriteViewController *fVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FavouriteViewController"];
//Set public property of FavouriteViewController (these are the data you wanted to send)
fVC.favouriteArray = @[@"Apple", @"Orange"];
[self presentViewController:fVC animated:YES completion:^{
}];
// OR
UINavigationController *nVC = [[UINavigationController alloc] initWithRootViewController:fVC];
//Presnet
[self presentViewController:nVC animated:YES completion:^{
}];
}