swift - PerformSegueWithIdentifier 不适用于以编程方式生成的 UIButtons (iOS/Swift)

标签 swift

Xcode 6、Swift、iOS8

我有一个由动态生成的 UIButtons 填充的 View Controller 。按钮的数量取决于数据馈送,并且不是静态的。我计算提要中的对象数量,并为每个对象生成一个按钮。每个按钮都应该进入一个详细信息 View ,该 View 显示其相应对象的信息。

在 Interface Builder 中,我在两个 View Controller 之间创建了一个 segue 并为其命名。我没有添加 IBAction 来启动 segue,因为我无法将其绑定(bind)到特定按钮。

在 View Controller 类中我执行以下操作:

@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'

任何有助于追查此异常原因的帮助将不胜感激。

最佳答案

目标 View Controller 的类文件似乎存在某种问题。在 Epic Defeater 发表评论后,我删除了它的 swift 文件,重新生成它并放入完全相同的代码(字面意思是复制/粘贴),这样就成功了。

关于swift - PerformSegueWithIdentifier 不适用于以编程方式生成的 UIButtons (iOS/Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31737320/

相关文章:

ios - ios swift 中的 Linkedin 共享弹出窗口

swift - 统一的 Swift API : use of undeclared type T

swift - 如何将数据传递给 UITabBarController?

ios - UITableView 中单元格之间的大间隙,iOS 错误?

ios - 如何从 UIViewController 获取 CLASS

swift - 2 十进制定时器在 swift

SwiftUI:调整弹出窗口的大小以适合

ios - 从 JSON 数组到 UICollectionView 的多个图像

ios - swift 3.0 TableViewCell : Saving the Status of a Button and Having it Persist

ios - 如何将 4 个 uibutton 居中,彼此之间的距离相等?