【问题标题】:Adding a button to a PopUPViewController what is the target? in SWIFT向 PopUPViewController 添加按钮的目标是什么?在 SWIFT 中
【发布时间】:2015-03-31 19:46:04
【问题描述】:

我已经向popOverViewController 添加了一个按钮,尽管当我添加该操作时它一直在崩溃。

我认为是因为我没有正确设置目标,虽然它是一个弹出窗口,但我不知道目标是什么?我尝试使用 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()
                }
        })
    }
}

【问题讨论】:

  • 您在哪里将按钮添加到主视图?
  • 我已将按钮添加到 popUpView,它已作为 UIView 以编程方式创建。 popUpView 弹出主视图。当我在这些视图中使用目标时,它会崩溃。
  • 请发布更多代码以更好地了解您

标签: swift uibutton uipopovercontroller


【解决方案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()
            }
    })
 }
}        

【讨论】:

  • 对不起,我已经完成了,只是忘记将它添加到我的代码中。弹出视图工作正常,它也可以根据我的需要弹出主视图。问题是当按下按钮时它会崩溃。我也不确定我要引用哪个目标。都不工作
  • @HenryBrown 正如我上面所说的,我在 Xcode 中创建了一个新项目,并将上面的代码及其工作正常,请验证它!!!
  • @HenryBrown 查看更新的答案,我将您遇到的错误放入代码中。
  • 对不起,这不是错误,正如您所见,我有两个按钮,一个有 :,但它仍然不起作用。我几乎可以肯定问题来自我所说的目标。如果你把它放到你的 x 代码中,你会看到它会崩溃
  • @HenryBrown 我把你的代码就像我在我的 Xcode 中一样我只更改了类的名称并删除了字体,它工作正常!!
猜你喜欢
  • 2017-02-21
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多