我已經添加了一個按鈕到popOverViewController
,雖然當我添加動作時會一直崩潰。向PopUPViewController添加一個按鈕目標是什麼?在SWIFT
我認爲這是因爲我沒有正確設置目標,雖然它是彈出窗口,我不知道目標是什麼?我曾嘗試使用自我,並嘗試使用實際彈出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()
}
})
}
}
你在哪裏添加了按鈕到主視圖? – 2015-03-31 19:53:52
我已經將按鈕添加到popUpView,它已經以編程方式創建爲UIView。 popUpView彈出主視圖。當我在這些視圖中使用目標時,它會崩潰。 – 2015-03-31 19:57:30
請發佈更多的代碼,以便更好地理解你 – 2015-03-31 19:58:43