나는 액션을 호출하는 방법을 찾기 위해 며칠 동안 노력했다. 사용자 정의 버튼의 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;
}
가 사전에 감사합니다
는 여기에 문제에 관한 전체 코드입니다. 나는 나의 문제에 대해 명확한 설명을하기를 바란다.
UIButton을 하위 클래스로 만들지 마십시오. [이 질문과 대답] (http://stackoverflow.com/questions/5045672/create-uibutton-subclass)을 참조하십시오. – Rayfleck
귀하의 reply.but 주셔서 감사합니다 그것은 링크에서 문제가 좀 me.Cause 나는 버튼으로 데이터를 전달해야합니다, 그게 내가 하위 클래스를 사용하는 이유. 필자가해야 할 일은 특정 핀 주석의 인물 데이터를 새로운 뷰로 전달하여 주석을 표시하는 것입니다. 인물 데이터가 포함 된 핀 주석을 클릭하면 (여러 맵에 있음) 다른 뷰가 표시되어야합니다 이 사람 데이터를 표시합니다. 그래서 MKPinAnnotationView 개체에 맞춤 단추를 추가 할 생각하지만이 문제가 있습니다. 대신에 다른 해결책이 존재할 수 있습니까? thx – Deni
UIButton을 서브 클래스 화하여 각 버튼에 사용자 정보를 포함 할 수 있으면 필요하지 않습니다. 대신 구현해야하는 두 가지 대리자 메서드가 있습니다. mapView : annotationView : calloutAccessoryControlTapped : 및 mapView : didSelectAnnotationView :이 두 가지 모두보기를 알려줍니다. 그래서 view.annotation을 참조하여 해당 기본 데이터 항목이 해당 핀과 연관되어 있는지, 주석에서 해당 데이터 항목이 누구인지 파악할 수 있어야합니다. – Rayfleck