2013-04-14 2 views
4

보기 컨트롤러가 있는데, 완료와 함께 dissing 될 때, 나는 notfication을 게시하고 다른보기 컨트롤러에 포함 된 subview에서 oberserve를 추가했습니다. 그러나 게시 통지 방법을 실행하려고하면 exec_bad_access가 발생합니다. 뭐가 문제 야? 코드는 다음과 같습니다 :NSNotificationCenter postNotificationName exec_badaccess

BrandListByIdViewController.m 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSNumber *bid = self.brands[indexPath.row][@"id"]; 
    [self dismissViewControllerAnimated:YES completion:^{ 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"SelectedBrandId" object:nil]; 
    }]; 

} 

SearchNewProduct.h 

@interface SearchNewProduct : UIView 

@end 

SearchNewProduct.m 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSelectedBrandId::) name:@"SelectedBrandId" object:nil]; 
    } 
} 

- (void)didSelectedBrandId:(NSNotification *)notif{ 

    NSLog(@"%s", __PRETTY_FUNCTION__); 
} 

심지어 나는 userInfo를 제거합니다. 여전히 잘못된 액세스입니다. 나는 또 다른 새로운 프로젝트에서 비슷한 상황을 만들었지 만 잘 작동한다.

selector:@selector(didSelectedBrandId::) 

하나의 콜론이 있어야한다 :이 이유입니다,하지만 당신은 알림 센터보기를 추가 할 때, 당신의 선택이 잘못되면

답변

7

나는 당신이 UIView을 다루고 있고 UIViewController이 아니라는 것을 깨닫지 못했다. 무슨 일이 일어나고 있는지는 뷰가 출시 된 후에도 알림을 받고 있다는 것입니다. 당신의 UIViewdealloc를 호출하고 관찰자로서 자체를 제거해야합니다 :

- (void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

는 또한, 한 번 이상 호출되고 있는지 그 UIViewinitWithFrame 방법에 NSLog을 넣어.

이 질문은 매우 유사합니다 :

ios notifications to "dead" objects

+0

'bid'는 사용자 정보 딕셔너리 (값을 유지하고 키를 복사 함)의 멤버이므로 해제하면 안됩니다. – dreamlax

+0

해지 완료 전에 postNotificationName 메소드를 넣으려고했습니다. 아무리 내가 코드를 넣어도 상관 없습니다. 이보기 컨트롤러는 결국 닫을 것입니다. 위임을 시도했지만 작동하지 않는 것 같습니다. 대리자 메서드는 uiview에서 실행되지 않습니다. –

+0

내 대답을 수정했습니다. 두 개의 ViewController를 다루고 있다고 생각했습니다. 대의원은 VC에 소속되어 있습니다. 델리게이트 메서드를 사용하여 해당 뷰를 소유 한 VC가 처리하거나 위의 수정 된 답변을 시도 할 수 있습니다. –

1

확실하지. 전체 줄은 다음과 같아야합니다.

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

두 개의 콜론은 메소드가 두 개의 인수를 취하지 만 하나만 소요됨을 나타냅니다.

+0

두 개의 열이 오타입니다. –

관련 문제