向PopUPViewController添加一个按钮是什么目标?在SWIFT

时间:2015-03-31 19:46:04

标签: swift uibutton uipopovercontroller

我已经为popOverViewController添加了一个按钮,但是当我添加动作时它会一直崩溃。

我认为这是因为我没有正确设定目标,虽然因为它是一个popUp我不知道目标是什么?我尝试过使用self并尝试使用实际弹出UIView。虽然两者都会导致崩溃。这是我的代码:

    import UIKit
import QuartzCore

@objc class PopUpViewControllerSwift : UIViewController {

    var popUpUserImage: UIImageView!
    var messageLabel: UILabel!
    var popUpView: UIView!
    var congratsLabel: UILabel!

    var matchedUser : PFUser!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        popUpViewSetUp()
    }

    func popUpViewSetUp() {

        self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
        self.popUpView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width - 40, self.view.frame.size.height - 120))
        popUpView.center = self.view.center
        self.popUpView.layer.cornerRadius = 5
        self.popUpView.layer.shadowOpacity = 0.8
        self.popUpView.backgroundColor = UIColor.whiteColor()
        self.popUpView.layer.shadowOffset = CGSizeMake(0.0, 0.0)

        messageLabel = UILabel(frame: CGRectMake(10, self.popUpView.frame.size.height / 1.5, self.popUpView.frame.size.width - 20, 80))
        self.messageLabel.adjustsFontSizeToFitWidth = true
        self.messageLabel.font = UIFont(name: Font.FuturaMedium, size: 25)
        self.messageLabel.numberOfLines = 0
        self.messageLabel.textAlignment = NSTextAlignment.Center
      //  messageLabel.center = CGPointMake(self.view.center.x, CGRectGetMidY( self.popUpView.bounds ))

        popUpUserImage = UIImageView(frame: CGRectMake(30, 30, popUpView.frame.size.width - 60, popUpView.frame.size.width - 60))
        popUpUserImage.layer.cornerRadius = popUpUserImage.frame.size.width / 2
        popUpUserImage.clipsToBounds = true
        popUpUserImage.contentMode = .ScaleAspectFill

        congratsLabel = UILabel(frame: CGRectMake(10, self.popUpView.frame.size.height / 1.8, self.popUpView.frame.size.width - 20, 80))
        congratsLabel.adjustsFontSizeToFitWidth = true
        congratsLabel.font = UIFont(name: Font.FuturaBlack, size: 25)
        congratsLabel.numberOfLines = 0
        congratsLabel.textAlignment = NSTextAlignment.Center

        var continueButton = UIButton(frame: CGRectMake(25, popUpView.frame.size.height / 1.2, popUpView.frame.size.width / 2.5, 60))
        continueButton.setTitle("Play Again", forState: .Normal)
        continueButton.titleLabel?.font = UIFont(name: Font.FuturaBlack, size: 16)
        continueButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
        continueButton.backgroundColor = UIColor.groupTableViewBackgroundColor()
        continueButton.addTarget(popUpView, action: "continueButton", forControlEvents: .TouchUpInside)
        continueButton.layer.cornerRadius = 30

        var chatButton = UIButton(frame: CGRectMake(continueButton.frame.origin.x + continueButton.frame.width + 15, popUpView.frame.size.height / 1.2, popUpView.frame.size.width / 2.5, 60))
        chatButton.setTitle("Say Hello", forState: .Normal)
        //chatButton.contentVerticalAlignment = UIControlContentVerticalAlignment.Top
        chatButton.titleLabel?.font = UIFont(name: Font.FuturaBlack, size: 16)
        chatButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
        chatButton.backgroundColor = UIColor.groupTableViewBackgroundColor()
        chatButton.addTarget(self, action: "chatButton:", forControlEvents: .TouchUpInside)
        chatButton.layer.cornerRadius = 30

        view.addSubview(popUpView)
        popUpView.addSubview(messageLabel)
        popUpView.addSubview(congratsLabel)
        popUpView.addSubview(popUpUserImage)
        popUpView.addSubview(continueButton)
        popUpView.addSubview(chatButton)

    }

    func continueButton(sender: UIButton!) {
        self.removeAnimate()
    }

    func chatButton(sender: UIButton!) {
        println("here")

    func showInView(aView: UIView!, withImage image : UIImage!, withMessage message: String!, withCongrats: String, animated: Bool)
    {
        aView.addSubview(self.view)
        popUpUserImage!.image = image
        messageLabel!.text = message
        congratsLabel.text = withCongrats
        if animated
        {
            self.showAnimate()
        }
    }

    func showAnimate()
    {
        self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
        self.view.alpha = 0.0;
        UIView.animateWithDuration(0.25, animations: {
            self.view.alpha = 1.0
            self.view.transform = CGAffineTransformMakeScale(1.0, 1.0)
        });
    }

    func removeAnimate()
    {
        UIView.animateWithDuration(0.25, animations: {
            self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
            self.view.alpha = 0.0;
            }, completion:{(finished : Bool)  in
                if (finished)
                {
                    self.view.removeFromSuperview()
                }
        })
    }
}

1 个答案:

答案 0 :(得分:1)

你错误就在这一行:

continueButton.addTarget(self, action: "continueButton", forControlEvents: .TouchUpInside)

它应该是这样的:

continueButton.addTarget(self, action: "continueButton:", forControlEvents: .TouchUpInside)

您将:留在了电话

  

您的代码在我的Xcode中进行了测试:

import UIKit
import QuartzCore

@objc class ViewController : UIViewController {

var popUpUserImage: UIImageView!
var messageLabel: UILabel!
var popUpView: UIView!
var congratsLabel: UILabel!

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}


override func viewDidLoad() {
    super.viewDidLoad()

    popUpViewSetUp()
}

func popUpViewSetUp() {

    self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)

    self.popUpView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width - 40, self.view.frame.size.height - 120))
    self.popUpView.center = self.view.center
    self.popUpView.layer.cornerRadius = 5
    self.popUpView.layer.shadowOpacity = 0.8
    self.popUpView.backgroundColor = UIColor.blueColor()
    self.popUpView.layer.shadowOffset = CGSizeMake(0.0, 0.0)

    self.messageLabel = UILabel(frame: CGRectMake(10, self.popUpView.frame.size.height / 1.5, self.popUpView.frame.size.width - 20, 80))
    self.messageLabel.adjustsFontSizeToFitWidth = true        

    self.messageLabel.numberOfLines = 0
    self.messageLabel.textAlignment = NSTextAlignment.Center

    popUpUserImage = UIImageView(frame: CGRectMake(30, 30, popUpView.frame.size.width - 60, popUpView.frame.size.width - 60))
    popUpUserImage.layer.cornerRadius = popUpUserImage.frame.size.width / 2
    popUpUserImage.clipsToBounds = true
    popUpUserImage.contentMode = .ScaleAspectFill

    congratsLabel = UILabel(frame: CGRectMake(10, self.popUpView.frame.size.height / 1.8, self.popUpView.frame.size.width - 20, 80))
    congratsLabel.adjustsFontSizeToFitWidth = true

    congratsLabel.numberOfLines = 0
    congratsLabel.textAlignment = NSTextAlignment.Center

    var continueButton = UIButton(frame: CGRectMake(25, popUpView.frame.size.height / 1.2, popUpView.frame.size.width / 2.5, 60))
    continueButton.setTitle("Play Again", forState: .Normal)

    continueButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
    continueButton.backgroundColor = UIColor.groupTableViewBackgroundColor()
    continueButton.addTarget(self, action: "continueButton:", forControlEvents: .TouchUpInside)
    continueButton.layer.cornerRadius = 30

    var chatButton = UIButton(frame: CGRectMake(continueButton.frame.origin.x + continueButton.frame.width + 15, popUpView.frame.size.height / 1.2, popUpView.frame.size.width / 2.5, 60))
    chatButton.setTitle("Say Hello", forState: .Normal)

    chatButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
    chatButton.backgroundColor = UIColor.groupTableViewBackgroundColor()
    chatButton.addTarget(self, action: "chatButton:", forControlEvents: .TouchUpInside)
    chatButton.layer.cornerRadius = 30        

    self.view.addSubview(popUpView)

    popUpView.addSubview(messageLabel)
    popUpView.addSubview(congratsLabel)
    popUpView.addSubview(popUpUserImage)

    popUpView.addSubview(continueButton)
    popUpView.addSubview(chatButton)

}

func continueButton(sender: UIButton!) {
    //self.removeAnimate()
    println("here")
}

func chatButton(sender: UIButton!) {
    println("here")
    //self.removeAnimate()
}

func showInView(aView: UIView!, withImage image : UIImage!, withMessage message: String!, withCongrats: String, animated: Bool)
{
    aView.addSubview(self.view)
    popUpUserImage!.image = image
    messageLabel!.text = message
    congratsLabel.text = withCongrats
    if animated
    {
        self.showAnimate()
    }
}

func showAnimate()
{
    self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
    self.view.alpha = 0.0;
    UIView.animateWithDuration(0.25, animations: {
        self.view.alpha = 1.0
        self.view.transform = CGAffineTransformMakeScale(1.0, 1.0)
    });
}

func removeAnimate()
{
    UIView.animateWithDuration(0.25, animations: {
        self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
        self.view.alpha = 0.0;
        }, completion:{(finished : Bool)  in
            if (finished)
            {
                self.view.removeFromSuperview()
            }
    })
 }
}