2012-12-26 4 views
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를 logg하려고했지만 대상보기 컨트롤러 객체에 값을 할당하려고하면 종료됩니다. 도와주세요.

+0

나는 자신의 .m 및 .h 파일로 각각 4 개의 탭이있는 대상에서 tabcontroller를 가지고 있는데, 나는 그것들 모두에 대해 cityId가 필요하다. – MIrfan

답변

1

destViewControllerfeaturedViewController입니까? 나는 그렇지 않다라고 확신한다. 오류 로그는 UITabBarController이라고 알려줍니다.

UITabBarController에서 상속하는 클래스를 만드는 것이 좋습니다. 나는 그것을 MyTabBarViewController라고 부를 것이다. 스토리 보드의 탭 막대 컨트롤러 클래스를이 새 클래스로 설정하십시오. MyTabBarViewController.h에서

는, 속성을 만들 :

@property (nonatomic, strong) id cityId; 

(cityId는 당신이 필요로하는 모든 유형이 될 수 있습니다, 예를 들어 NSString, NSNumber, ...).

- (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 뷰 컨트롤러의하는 .m 파일에이 코드를 사용할 수 있습니다

cityId에 액세스 할 수 :

그런 다음 prepareForSegue 코드를 변경

// 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); 
관련 문제