问题
i want to pass data from one view controller to another view controller thats not connect to each other with segue.in firstviewcontroller when a button touch(favorite) want data pass to secondviewcontroller (favoriteviewcontroller) that is part of tabbarviewcontroller. i know that best solution maybe to use Delegate? but how can i do that?
回答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:^{
}];
}
来源:https://stackoverflow.com/questions/39315143/ios-passing-data-between-two-view-controllers-without-segue