带帮助程序类的xib文件按钮不起作用

时间:2017-07-05 10:50:32

标签: ios swift

我有一个xib文件,里面有一个按钮,按钮上有两个隐藏在if语句中的标签,我想要做的是在我的帮助器方法中调用一个名为pledgeInterestView的函数,这是一个alertView,我有向该按钮添加了一个目标,然后向父视图添加了一个手势识别器(此视图封装了标签和按钮),但按钮运行不正常并且未调用该功能? 我的xib文件很好,它加载完美,如果需要该代码,我可以提供它

这是我的助手方法:

static func inflateProfileImageFunder(view:UIView){
    let content = ProfileImage()
    view.addSubview(content)
    content.translatesAutoresizingMaskIntoConstraints = false
    let leadingDeviceData = NSLayoutConstraint(item: content, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0)
    let trailingDeviceData = NSLayoutConstraint(item: content, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0)
    let heightDeviceData = NSLayoutConstraint(item: content, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0)
    let topDeviceData = NSLayoutConstraint(item: content, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0)
    NSLayoutConstraint.activate([leadingDeviceData, trailingDeviceData, heightDeviceData, topDeviceData])

    let isFunder = LoginVC.isFunder
        if (!isFunder){
            print("Not a Funder")
            content.buttonViews.translatesAutoresizingMaskIntoConstraints = false
            content.buttonViews.heightAnchor.constraint(equalToConstant: 0).isActive = true
            content.buttonViews.isHidden = true
        }else{
            content.label2.isHidden = true
            content.label1.isHidden = true
            content.buttonTap.isEnabled = true
            content.buttonTap.setTitle("Pledge Interest", for: UIControlState.normal)
            content.buttonTap.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: UIControlState.normal)
            let tap = UITapGestureRecognizer(target: self, action: #selector(self.pledgeInterestView(_:)))
            content.buttonViews.addGestureRecognizer(tap)
            content.buttonViews.isUserInteractionEnabled = true
    }
}
func pledgeInterestView(_ sender: UITapGestureRecognizer){
    let content = PledgeInterest()

    content.translatesAutoresizingMaskIntoConstraints = false
    content.heightAnchor.constraint(equalToConstant: 175).isActive = true
    let attributedString = NSAttributedString(string: "I can Provide", attributes: [
        NSForegroundColorAttributeName : #colorLiteral(red: 0.1657446325, green: 0.3779471517, blue: 0.6579626203, alpha: 1)
        ])
    let alert = AlertController(title: "", message: "")
    alert.setValue(attributedString, forKey: "attributedTitle")


    let okAction = AlertAction(title: "Pledge", style: .normal, handler: nil)
    let cancelAction = AlertAction(title: "Cancel", style: .normal, handler: nil)

    alert.add(cancelAction)
    alert.add(okAction)
    alert.contentView.addSubview(content)

    content.leftAnchor.constraint(equalTo: alert.contentView.leftAnchor).isActive = true
    content.rightAnchor.constraint(equalTo: alert.contentView.rightAnchor).isActive = true
    content.centerXAnchor.constraint(equalTo: alert.contentView.centerXAnchor).isActive = true
    content.topAnchor.constraint(equalTo:alert.contentView.topAnchor).isActive = true
    content.bottomAnchor.constraint(equalTo: alert.contentView.bottomAnchor).isActive = true


    alert.view.tintColor = #colorLiteral(red: 0.1657446325, green: 0.3779471517, blue: 0.6579626203, alpha: 1)
    alert.present(alert, animated: true)
}

The code for my Xib file, the buttonView has 3 elements inside it, 2 labels and 1 button

the view I am trying to add a tap to(ProfileImage)

1 个答案:

答案 0 :(得分:0)

我终于解决了这个问题,只需在我的助手类中添加一个额外的静态函数,就像这样:

class engineering_courses(models.Model):
    course_name = models.CharField(max_length=400)
    course_description = models.CharField(max_length=1000, default='This is a description')
    course_offered_by = models.ManyToManyField(
        engineeringUni, 
        through='ThroughModel', 
        through_fields=('course', 'university'),
        )
    course_duration = models.IntegerField(blank=False, default='2')



    def __str__(self):
        return self.course_name

    def description_course(self):
        return self.course_description

    def offered_by_courses(self):
        return self.course_offered_by

    def duration_courses(self):
        return str(self.course_duration)



class ThroughModel(models.Model):
    course = models.ForeignKey(engineering_courses, on_delete=models.CASCADE)
    university = models.ForeignKey(engineeringUni, on_delete=models.CASCADE)
    additional_text = models.CharField(max_length=200)

这对我来说很完美