添加的xib子视图中的IBAction不起作用

时间:2015-05-15 12:51:39

标签: ios swift mapkit

我有一个MapKit应用程序。我已使用.xib文件中的View替换了callout视图。当用户点击注释时,此子视图将显示以下代码:

func mapView(mapView: MKMapView!, didSelectAnnotationView annotation: MKAnnotationView!)
    {
        if let pin = annotation.annotation as? CustomPointAnnotation{

            if var infoView = UIView.viewFromNibName("myAnnView") as? myAnnView {
                infoView.nameLabel.text = pin.theTitle
                infoView.detailsLabel.text = pin.theDetails

                annotation.addSubview(infoView)

            } else {
                println("not the right type")
            }
        }
    }

.xib文件有两个标签和一个按钮。我想要做的是当用户按下这个按钮时它应该“激活”一个segue,但如果我点击按钮它什么都不做。以下是视图的代码:

class MarkerInfoView: MKAnnotationView {


  @IBOutlet weak var placePhoto: UIImageView!
  @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var detailsLabel: UILabel!
    @IBOutlet weak var theButton: UIButton!

    private var hitOutside:Bool = true

    override func setSelected(selected: Bool, animated: Bool) {
        let calloutViewAdded = self.superview != nil

        println("MapPin")

        if (selected || !selected && hitOutside) {
            super.setSelected(selected, animated: animated)
        }

        self.superview?.bringSubviewToFront(self)

//        if (self == nil) {
//            self = MyAnnInfoView()
//        }

        if (self.selected && !calloutViewAdded) {
            addSubview(self)
        }

        if (!self.selected) {
            self.removeFromSuperview()
        }
    }

    override func hitTest(var point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        let viewPoint = superview?.convertPoint(point, toView: self) ?? point

        let isInsideView = pointInside(viewPoint, withEvent: event)

        var view = super.hitTest(viewPoint, withEvent: event)

        println("evrika")

        return view
    }

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        return CGRectContainsPoint(bounds, point)
    }

    @IBAction func readIt(sender: AnyObject) {
        println("evrika")
    } 
}

“evrika”永远不会出现。为什么不调用此操作,如何调用它?

为什么我很难做到这一点,因为我没有使用默认的Pins。相反,我使用它:https://github.com/RVLVR/REVClusterMap

我将REVClusterPin修改为:

#import "REVClusterPin.h"

@implementation REVClusterPin
@synthesize title,coordinate,subtitle;
@synthesize nodes;
@synthesize selected;

- (NSUInteger) nodeCount
{
    if( nodes )
        return [nodes count];
    return 0;
}

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    printf("hitTest");
    UIView* hitView = [super hitTest:point withEvent:event];
    if (hitView != nil)
    {
        [super bringSubviewToFront:self];
    }
    return hitView;
}

#if !__has_feature(objc_arc)
- (void)dealloc
{
    [title release];
    [subtitle release];
    [nodes release];
    [super dealloc];
}
#endif

@end

非常感谢!

0 个答案:

没有答案