Development Tip

Swift에서 Segue 준비

yourdevel 2020. 12. 2. 22:03
반응형

Swift에서 Segue 준비


오류 메시지가 나타납니다.

"UIStoryboardSegue does not have a member named 'identifier'"

다음은 오류를 일으키는 코드입니다.

if (segue.identifier == "Load View") {
    // pass data to next view
}

Obj-C에서는 다음과 같이 사용하는 것이 좋습니다.

if ([segue.identifier isEqualToString:@"Load View"]) {
   // pass data to next view
}

내가 도대체 ​​뭘 잘못하고있는 겁니까?


이것은 UITableViewController서브 클래스 템플릿 의 문제로 인한 것 같습니다 . prepareForSeguesegue를 풀어야 하는 메서드 버전이 함께 제공됩니다 .

현재 prepareForSegue함수를 다음으로 바꿉니다 .

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "Load View") {
        // pass data to next view
    }
}

이 버전은 매개 변수를 암시 적으로 풀기 때문에 괜찮습니다.


스위프트 4, 스위프트 3

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "MySegueId" {
        if let nextViewController = segue.destination as? NextViewController {
                nextViewController.valueOfxyz = "XYZ" //Or pass any values
                nextViewController.valueOf123 = 123
        }
    }
}

문제는! 식별자 번들 해제

나는 가지고있다

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        if segue!.identifier == "Details" {
            let viewController:ViewController = segue!.destinationViewController as ViewController
            let indexPath = self.tableView.indexPathForSelectedRow()
            viewController.pinCode = self.exams[indexPath.row]

        }

    }

내 이해는! 당신은 참 또는 거짓 값을 얻습니다


Swift 2.3, swift3 및 swift4의 경우 :

didSelectRowAtindexPath에서 수행 Segue 만들기

예 :

   self.performSegue(withIdentifier: "uiView", sender: self)

그 후 목적지 segue를 포착하고 값을 전달하는 prepareforSegue 함수를 작성하십시오.

전의:

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

       if segue.identifier == "uiView"{

        let destView = segue.destination as! WebViewController
        let indexpath = self.newsTableView.indexPathForSelectedRow
        let indexurl = tableDatalist[(indexpath?.row)!].link
        destView.UrlRec = indexurl

        //let url =

    }
    }

You need to create a variable named UrlRec in Destination ViewController


Swift 1.2

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
            if (segue.identifier == "ShowDeal") {

                if let viewController: DealLandingViewController = segue.destinationViewController as? DealLandingViewController {
                    viewController.dealEntry = deal
                }

            }
     }

Prepare for Segue in Swift 4.2 and Swift 5.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "OrderVC") {
        // pass data to next view
        let viewController = segue.destination as? MyOrderDetailsVC
        viewController!.OrderData = self.MyorderArray[selectedIndex]


    }
}

How to Call segue On specific Event(Like Button Click etc):

performSegue(withIdentifier: "OrderVC", sender: self)

this is one of the ways you can use this function, it is when you want access a variable of another class and change the output based on that variable.

   override func prepare(for segue: UIStoryboardSegue, sender: Any?)  {
        let something = segue.destination as! someViewController
       something.aVariable = anotherVariable
   }

Provided you aren't using the same destination view controller with different identifiers, the code can be more concise than the other solutions (and avoids the as! in some of the other answers):

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    if let myViewController = segue.destinationController as? MyViewController { 
        // Set up the VC
    }
}

Change the segue identifier in the right panel in the section with an id. icon to match the string you used in your conditional.


override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        if(segue!.identifier){
            var name = segue!.identifier;
            if (name.compare("Load View") == 0){

            }
        }
    }

You can't compare the the identifier with == you have to use the compare() method

참고URL : https://stackoverflow.com/questions/24040692/prepare-for-segue-in-swift

반응형