2011-05-05 6 views
3

나는 액션을 호출하는 방법을 찾기 위해 며칠 동안 노력했다. 사용자 정의 버튼의 selector는 UIButton에서 파생되었으며 핀 주석과 연관시키고 싶다. 하위 클래스를 사용하는 이유는 단추를 눌렀을 때이 데이터를 표시하는 또 다른 UIView을 표시하기 위해이 단추 개체에 일부 데이터를 전달하기 위해서입니다.커스텀 UIButton 서브 클래스에 대한 액션 설정

문제는 실행하는 동안 핀 주석을 클릭하면 단추가 열리고 아무 것도 클릭하지 않으면 문제가 발생한다는 것입니다.

[rightButton addTarget:self 
       action:@selector(showDetails:) 
     forControlEvents:UIControlEventTouchUpInside]; 

내 질문 등 addTarget:action:forControlEvents:, buttonWithType:처럼, UIButton의 방법을 상속하는 방법을 내 서브 클래스에서 사용하기 위해 : 여기 설정 한 내 showDetails: 방법은 touchupInside 이벤트를 수신에 호출되지 않습니다 ?

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    MKPinAnnotationView *annView=[[[MKPinAnnotationView alloc] 
     initWithAnnotation:annotation reuseIdentifier:@"MyPin"] autorelease]; 
    annView.animatesDrop=FALSE; 
    annView.canShowCallout = YES; 
    [annView setSelected:YES]; 

    if ([[annotation title] isEqualToString:@"Current Location"]) { 
     annView.pinColor = MKPinAnnotationColorRed; 
    } 
    else { 
     annView.pinColor = MKPinAnnotationColorPurple; 
    } 

    annView.calloutOffset = CGPointMake(-5, 5); 

    PlaceMark *pm=(PlaceMark *)annotation; 
    AddressItem *ai=[pm getData]; 

    //Another problem is that i cant set buttonWithType:UIButtonTypeDetailDisclosure cause it generate error cause it is a method of UIbutton superclass 
    MyDetailButton* rightButton = [MyDetailButton buttonWithType:UIButtonTypeCustom]; 
    [rightButton setData:ai]; 

    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 

    [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; 

    annView.rightCalloutAccessoryView = rightButton; 

    return annView; 
} 

가 사전에 감사합니다

는 여기에 문제에 관한 전체 코드입니다. 나는 나의 문제에 대해 명확한 설명을하기를 바란다.

+0

UIButton을 하위 클래스로 만들지 마십시오. [이 질문과 대답] (http://stackoverflow.com/questions/5045672/create-uibutton-subclass)을 참조하십시오. – Rayfleck

+0

귀하의 reply.but 주셔서 감사합니다 그것은 링크에서 문제가 좀 me.Cause 나는 버튼으로 데이터를 전달해야합니다, 그게 내가 하위 클래스를 사용하는 이유. 필자가해야 할 일은 특정 핀 주석의 인물 데이터를 새로운 뷰로 전달하여 주석을 표시하는 것입니다. 인물 데이터가 포함 된 핀 주석을 클릭하면 (여러 맵에 있음) 다른 뷰가 표시되어야합니다 이 사람 데이터를 표시합니다. 그래서 MKPinAnnotationView 개체에 맞춤 단추를 추가 할 생각하지만이 문제가 있습니다. 대신에 다른 해결책이 존재할 수 있습니까? thx – Deni

+0

UIButton을 서브 클래스 화하여 각 버튼에 사용자 정보를 포함 할 수 있으면 필요하지 않습니다. 대신 구현해야하는 두 가지 대리자 메서드가 있습니다. mapView : annotationView : calloutAccessoryControlTapped : 및 mapView : didSelectAnnotationView :이 두 가지 모두보기를 알려줍니다. 그래서 view.annotation을 참조하여 해당 기본 데이터 항목이 해당 핀과 연관되어 있는지, 주석에서 해당 데이터 항목이 누구인지 파악할 수 있어야합니다. – Rayfleck

답변

0

내가 잘못 생각한 것 같습니다. @selector 액션을 보내려면 클래스는 UIControl이 아니라 UIControl을 상속 받아야합니다. 체크 아웃 UIControl Class Reference 나는 문제가 여기에 있다고 생각합니다. 귀하의 질문을 이해할 수 없다면 주저하지 말고 알려주십시오.

+0

답장을 보내 주셔서 감사합니다. 사실 UICutton에서 UIControl 로의 상속을 대체했는데 버튼이 MKPinAnnotationView 객체 (annView)에 표시되지 않기 때문에 문제가 지속됩니다. MyDetailButton * rightButton = [MyDetailButton buttonWithType : UIButtonTypeDetailDisclosure]; UICOntrol 클래스의 nnot 부분이거나 잘못된 것인가? 그래서, 나는 MKPinAnnotationView 개체에서 클릭 몇 가지 트릭을 찾으려고 노력하고있어, 그것은 데이터가 표시된 새로운 뷰를 열 것이다. 하지만 제가 알다시피, 그것은 행동 방법이 없습니다, 다시 감사합니다 – Deni

+0

나는 그것이 핵심 문제라고 생각하지 않습니다. 내가 올바르게 기억하면 UIButton은 UIControl에서 상속되므로 UIButton을 상속하면 UIControl의 모든 동작도 제공됩니다. – Nicholas1024

+0

@ Nicholas1024 나는 UIButton을 상속 받았다고 생각했지만, 내 테스트를하면서. buttonWithType을 호출 할 때 예외가 발생합니다. UIButtonTypeDetailDisclosure – Deni

관련 문제