/ / Tab Bar Controller間でデータを受け渡しようとしています-iOS、Objective-C、TabView

タブバーコントローラ間でデータをやり取りしようとしています - ios、objective-c、tabview

私は私のビュー間でいくつかのデータを渡そうとしていますタブバー。最初のビューは、モデルクラスからデータをロードして操作できます。しかし、Tab Bar Controllerで2番目または3番目のタブを押すと、データは「渡されません。ここで」私はそれを渡そうとします。

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

if (tabBarController.selectedIndex == 1){
HashTagTableViewController *hash [[HashTagTableViewController alloc]init];
hash.userArray = feed.userArray;
}else if (tabBarController.selectedIndex == 2){
PhotoTagTableViewController *photo = [[PhotoTagTableViewController alloc]init;
photo.userArray = feed.userArray;

}

}

feedはモデルのインスタンスの名前です現在のView Controllerで作成したクラス。 APIに対して複数の呼び出しを行う必要があるため、モデルクラスの複数のインスタンスを作成しないようにしようとしています。 feed.userArray 異なる方法で操作される異なるビューに。

回答:

回答№1の場合は3

この方法でView Controllerを作成しないでください。 UITabBarControllerは、初期化時にすべてのView Controllerを自動的に作成します。代わりにこれを試してください:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if (tabBarController.selectedIndex == 1){
HashTagTableViewController *hash = (HashTagTableViewController *) viewController;
hash.userArray = feed.userArray;
}else if (tabBarController.selectedIndex == 2){
PhotoTagTableViewController *photo = (PhotoTagTableViewController *)viewController;
photo.userArray = feed.userArray;
}
}

回答№2の場合は0

新しいViewControllerインスタンスを作成しています。これの代わりに、TabBarControllerのViewController配列から選択されたViewControllerを取得する必要があります。コードを変更しました。以下で確認してください。

注意:

スペルミス/メソッド名を解決してください。私はメモ帳でこれを書いているので。 Xcodeではありません。

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([viewController isKindOfClass: [HashTagTableViewController class]])
{
HashTagTableViewController *hash = (HashTagTableViewController) viewController;
hash.userArray = feed.userArray;
}
else if([viewController isKindOfClass: [PhotoTagTableViewController class]])
{
PhotoTagTableViewController *photo = (PhotoTagTableViewController) viewController;
photo.userArray = feed.userArray;
}
}