2012-08-28 2 views
0

있는 NSMutableArray detailsDataSourceMainDetailViewController.m에서 다음 뷰 컨트롤러에 전달됩니다 detailIndex : INT UserDetailViewController.m에 색인을 통해패스 지수

#import "UsersDetailViewController.h" 
... 
- (void)swipeDetectedUp:(UISwipeGestureRecognizer *)sender 
{ 
    UsersDetailViewController *usersController = [[self storyboard] instantiateViewControllerWithIdentifier:@"UsersController"]; 
    [self.navigationController pushViewController:usersController animated:NO]; 

    usersController.usersDataSource = [[NSMutableArray alloc] initWithArray:detailsDataSource]; 
    usersController.userDetailIndex = detailIndex; 
} 

출근을 :

- (void)swipeDetectedRight:(UISwipeGestureRecognizer *)sender 
{ 
if (userDetailIndex != 0) 
    userDetailIndex--; 
} 

swipeDetectedDown 다시 팝업하려면 MainDataViewController은 표시 할 색인의 개체를 알아야합니다.

- (void)swipeDetectedDown:(UISwipeGestureRecognizer *)sender 
{ 
//jump to correct object at index, same as current object at index in this view 
[self.navigationController popViewControllerAnimated:NO]; 
} 

코드 제안?

+0

MainDetailViewController의 컨텐츠를 선택하기 위해 UsersDetailViewController의 주된 책임은 무엇입니까? 그렇다면 위임 프로토콜을 사용합니다. 인덱스 선택이 다른 책임에 부수적 인 경우 KVO를 사용합니다. –

답변

0

사용 NSNotificationCenter 다시 MainDataViewController에 객체를 보낼 수 ...

예 : 키 => 값 쌍과 함께있는 NSDictionary를 채우는 UsersDetailViewController에서

그때 당신이 가고 싶은 곳으로 보내 .

NSArray *key = [NSArray arrayWithObject:@"myIndex"]; 
NSArray *object = [NSArray arrayWithObject:detailIndex]; 
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:object forKeys:key]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"MainDataViewController" object:self userInfo:dictionary]; 

주 : MainDataViewController라는 식별자 또는이를 호출 할 식별자를 설정해야합니다. VC 이름을 사용하면 더 간단하게 유지할 수 있습니다.

그런 다음 MainDataViewController에서 viewDidLoad() 메소드에서이 작업을 수행하십시오.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"MainDataViewController" object:nil]; 

그리고 다음과 같은 방법을 사용하여 알림을 수신 :

- (void)receiveNotification:(NSNotification *) notification 
{  
    if([[notification name] isEqualToString:@"MainDataViewController"]) 
    {  
     NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:[notification userInfo]]; 

     if([dictionary valueForKey:@"myIndex"]) 
     { 
      // do whatever you need to do with the passed object here. In your case grab the detailIndex and use it for something... 
     }    
    } 
} 
+0

나는 하나의 객체가 변경을 통보 받아야하므로 위임이나 KVO를 사용하는 것이 더 낫다고 주장 할 것이다. –

0

쉬운 부분이 self.usersController.usersDataSource에게 & 자체에 액세스 할 수 있도록 MainDetailViewController의 속성에 UsersDetailViewController 포인터를 넣어하는 것입니다 .usersController.userDetailIndex 나중에. 그렇다면 유일한 트릭은 UsersDetailViewController가 팝업되었을 때 알리는 것입니다. 내가 작성하는 데 사용 코드에서

, 나는 종종 MainDetailViewController이 UsersDetailViewController의 대리인이 될 만들기, 그리고 UsersDetailViewController가 프로그램을 종료 할 때 MainDetailViewController에서 위임 메소드가 호출 할 수있는 같이 무언가를 시도하고 있다는 점에서 할 모두 popViewControllerAnimated : 및 MainDetailViewController의 상태를 업데이트하십시오. 즉, 항상 부모의 코드가 자식을 팝하게하십시오. 이 작동하지만 탐색 컨트롤러의 뒤로 단추를 통해 자식 뷰 컨트롤러를 자동으로 팝하는 경우에는 전반적으로 해당 기술에 대해 논쟁 할 수 없습니다.

나는 아이가 태어 났을 때 부모의 코드가 호출되도록하는 더 좋은 해결책이 있다고 생각한다. 아마도 viewWillAppear 메서드를 구현하고 self.usersController가 설정되어 있으면 UsersDetailViewController에서 돌아와서 다른 컨트롤러의 속성에 액세스 한 다음 self.usersController를 지우십시오.