DATA MIGRATION WHEN PROGRAM TRANSITION OF SEGUE

Asked 2 years ago, Updated 2 years ago, 107 views

Please let me know if you can understand it because I don't know how to handle it when the segue transition is done with swift.

Instead of using IB for segue, set the identifier and use it in the program.
There are two screens, ViewController and PuzzleViewController, which are transitioned using the performSegueWithIdentifier.The transition method is to create an image and move it from there using the onClickButton action.Set tag(cTag) when installing the image because the action on the next screen varies depending on the image to be TouchUpInside.So I have a question, I set sender?.tag in prepareFosegue method, but somehow I get nil back.If I set sender?.tag in the onClickButton method, the correct tag value will be returned, but is it because it is out of range when the prepareFosegue method occurs?How should I think about it again?

 funccImageMake (cImageName: String, cTag: Int, cWideth: CGFloat, cHeight: CGFloat, cText: String) {
    // Generating icon images
    let cImage: UIImage = UIImage(named:cImageName) as UIImage!
    letcImageButton=UIButton()
    cImageButton.tag=cTag
    cImageButton.frame = CGRectMake(0,0,75,75)
    cImageButton.layer.position = CGPoint (x:self.view.frame.width/4*cWideth, y:self.view.frame.height/4*cHeight)
    cImageButton.setImage(cImage, forState:.Normal)
    cImageButton.addTarget(self, action: "onClickCButton:", forControlEvents: .TouchUpInside)
    self.view.addSubview(cImageButton)
    // Generating Icon Characters
    varcTextField: UITextField = UITextField (frame: CGRectMake(0,0,75,75))
    cTextField.text=cText
    cTextField.textAlignment=NSTextAlignment.Center
    cTextField.enabled=false
    cTextField.layer.position = CGPoint (x:self.view.frame.width/4*cWideth, y:self.view.frame.height/4*cHeight)
    self.view.addSubview(cTextField)
}

// Display button event PuzzleViewController
funconClickCButton(sender:AnyObject){
    self.performSegueWithIdentifier("goPuzzleSegue", sender:sender.tag)
}
override func prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?) {
    if segue.identifier == "goPuzzleSegue" {
        let puzzleViewController: PuzzleViewController=segue.destinationViewController as!PuzzleViewController
        puzzleViewController.castleId=sender?.tag

swift xcode6

2022-09-30 17:28

1 Answers

// Display button event PuzzleViewController
// funconClickCButton(sender:AnyObject){
FunctionClickCButton (sender:UIButton) {//sender type to UIButton.
    self.performSegueWithIdentifier("goPuzzleSegue", sender:sender) // sender.tag→sender
}

override func prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?) {
    if segue.identifier == "goPuzzleSegue" {
        // Optional Binding.
        iflet theButton=sender as?UIButton{
            let puzzleViewController: PuzzleViewController=segue.destinationViewController as!PuzzleViewController
            puzzleViewController.castleId=theButton.tag
        }

Note that the method argument sets the type sender to AnyObject.sender.tag does not make sense because AnyObject does not have the property tag.
Because tag has the property UIView and its subclasses (including UIButton, you must downcast sender to UIView or UIButton.
funconClickCButton (sender:UIButton) is changing the type of direct argument.
iflet theButton=sender as?UIButton is downcast using Optional Binding.If the downcast fails, theButton contains nil.

*In addition to method function arguments, it is recommended to predict that some kind of downcast is necessary when Any or AnyObject types appear.For example, elements of an array Array or a dictionary Dictionary are often of the AnyObject type.If you do not downcast an element when you take it out, you will often be unable to use it as it is.


2022-09-30 17:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.