2015-03-31 58 views
-1

我已經添加了一個按鈕到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() 
       } 
     }) 
    } 
} 
+0

你在哪裏添加了按鈕到主視圖? – 2015-03-31 19:53:52

+0

我已經將按鈕添加到popUpView,它已經以編程方式創建爲UIView。 popUpView彈出主視圖。當我在這些視圖中使用目標時,它會崩潰。 – 2015-03-31 19:57:30

+0

請發佈更多的代碼,以便更好地理解你 – 2015-03-31 19:58:43

回答

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() 
      } 
    }) 
} 
}   
+0

對不起,我已經做了,我只是忘了把它添加到我的代碼。彈出視圖工作正常,它彈出了主視圖,因爲我也想要它。問題是按下按鈕時它崩潰。我不確定我打算提及哪個目標。無論是工作 – 2015-03-31 21:00:21

+0

@HenryBrown正如我上面說過的,我已經在Xcode中創建了一個新項目,並將上面的代碼和它的工作正常,請驗證它! – 2015-03-31 21:01:40

+0

@HenryBrown看到更新的答案,我把你在代碼中的錯誤。 – 2015-03-31 21:28:38

相關問題