2

UITABLEVIEWCONTROLLERから別のUITABLEVIEWCONTROLLERにIDを渡していますが、次のエラーがスローされます。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setCityId:]: unrecognized selector sent to instance 0x75225e0'

ここでprepareForSegue関数を使用します。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"cityPushToTab"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        featuredViewController *destViewController = segue.destinationViewController;
        destViewController.cityId = [productKeys objectAtIndex:indexPath.row];
    }

}

この関数は、注目のコントローラーのcityIdを呼び出す前にうまく機能しました。正しい値を出力するproductKeysをログに記録しようとしましたが、宛先のViewControllerオブジェクトに値を割り当てようとすると終了します。助けてください。

4

1 に答える 1

1

本当にdestViewControllerクラスの人featuredViewControllerですか?そうではないと確信しています。クラッシュログはそれがであることを示していUITabBarControllerます。

私がお勧めするのは、から継承するクラスを作成することUITabBarControllerです。私はそれを呼びますMyTabBarViewController。ストーリーボードのタブバーコントローラーのクラスをこの新しいクラスに設定します。

MyTabBarViewController.h、プロパティを作成します。

@property (nonatomic, strong) id cityId;

(cityIdは、必要な任意のタイプにすることができます。たとえば、、NSString... NSNumber)。

次に、prepareForSegueコードを変更します。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"cityPushToTab"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        MyTabBarViewController *destViewController = segue.destinationViewController;
        destViewController.cityId = [productKeys objectAtIndex:indexPath.row];
    }
}

次に、タブバーにある4つのView Controllerの.mファイルで、次のコードを使用してアクセスできますcityId

// Cast the viewcontroller's tab bar to your class
MyTabBarViewController *tabBarController = (MyTabBarViewController*)self.tabBarController;

// Access your property
id cityId = tabBarController.cityId;
// You can test to see if it works by casting to an NSString and NSLog it
NSString *cityIdString = (NSString*) tabBarController.cityId;
NSLog (@"%@", cityIdString);
于 2012-12-26T15:02:23.460 に答える