performSegueWithIdentifier不能用于编程生成的uibutton (iOS/Swift)



Xcode 6, Swift, iOS8

我有一个由动态生成的uibutton填充的视图控制器。按钮的数量取决于数据提要,而不是静态的。我计算提要中的对象数量,并为每个对象生成一个按钮。每个按钮都应该segue到一个细节视图,该视图显示其对应对象的信息。

在Interface Builder中,我在两个视图控制器之间创建了一个segue并命名它。我没有添加IBAction来初始化segue,因为我不能将它绑定到特定的按钮。

在视图控制器类中,我执行以下命令:

@IBOutlet weak var localScrollView: UIScrollView! 

override func viewDidLoad() {
    super.viewDidLoad()
//Define the button dimensions
    let buttonWidth:CGFloat = 200
    let buttonHeight:CGFloat = 113
    var xPos:CGFloat = 0
    var scrollViewContent:CGFloat = 0
    var thumbURL:String
//keeps track of the number of videos. The count is used to set a tag on the button to help identify it.
    var vidCount:Int = 0  
    //loop through the array of recommended video objects
    for index in recommended{
   //Create a button for each object 
       var myButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
        myButton.frame = CGRectMake(xPos, 0.0, buttonWidth, buttonHeight)
    //For the button action I call the handleTap function detailed below
        myButton.addTarget(self, action: "handleTap:", forControlEvents: .TouchUpInside)
        myButton.tag = vidCount
    //The image for the button is pulled from a CDN. This code sets the image in the button.
        let curVal = index.thumbURL
        if let url = NSURL(string: curVal) {
            if let data = NSData(contentsOfURL: url){
                myButton.setBackgroundImage(UIImage(data:data), forState: UIControlState.Normal)
            }
        }
    //add the button to the scroll view
        localScrollView.addSubview(myButton)
        let spacer:CGFloat = 10
        xPos+=buttonWidth + spacer
        scrollViewContent += buttonWidth + spacer
        localScrollView.contentSize = CGSize(width: scrollViewContent, height: buttonHeight)
        vidCount += 1
    }

}

//function to handle the tap action
func handleTap(sender:UIButton){
    //Set the variable that will be passed to the next view controller
    curRecVid = recommended[sender.tag]
    //initiate the segue
    dispatch_async(dispatch_get_main_queue()) {
        self.performSegueWithIdentifier("toSingle", sender: self)
    }
}
//prepare the data to be transferred to the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "toSingle") {
        var targetViewController = segue.destinationViewController as! SingleViewController
        targetViewController.targetVid = curRecVid
    }
}

}

当我在模拟器中运行应用程序时,它到达self。performSegueWithIdentifier("toSingle", sender: self)调用,然后终止到以下未捕获的异常:'NSInvalidArgumentException',原因:'-[UILabel copyWithZone:]:未识别的选择器发送到实例0x7fd468c8ddc0'

任何帮助,帮助追查这个异常的原因,将不胜感激。

似乎目标视图控制器的类文件有某种问题。在Epic Defeater的评论之后,我删除了它的swift文件,重新生成它并放入完全相同的代码(从字面上看,复制/粘贴),这就奏效了。

最新更新