Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Segue in Swift

Tags:

ios

swift

ios8

@objc(SEPushNoAnimationSegue)
class SEPushNoAnimationSegue: UIStoryboardSegue {
    override func perform () {
      self.sourceViewController.navigationController.pushViewController(self.destinationViewController, animated:false)
    }
}

In the above code, I have 2 questions: 1). it has a compile error: 'UINavigationController!' does not have a member named 'pushViewController'

But in that class, it did has a pushViewController method.

2). I have to add the annotation: @objc(SEPushNoAnimationSegue), otherwise, in storyboard, it only recognize the random generated name, like, _tcxxxxSEPushNoAnimationSegue.

why these 2 issues happen here?

like image 892
user2909913 Avatar asked Jun 12 '14 13:06

user2909913


People also ask

How do I segue to another view controller?

Well, in addition to that, Ctrl+dragging also helps set up segues. So, Ctrl+Drag from the “Go to Other View Controller” button, to somewhere in the second View Controller. It can be anywhere in the main box of the second view controller. When you release, it will show you a box like the one below.

How do I create a segue identifier?

Assigning an identifier for the segue:Select the segue, from the attribute inspector you'll see "Identifier" text field, that's it! make sure to insert the exact same name that used in performSegueWithIdentifier .

What is a relationship segue?

Relationships segues are different from the other segues in that they are resolved at build time. When a UITabBarController is loaded from a storyboard, all of its constituent view controllers are already 'inside' of it in the same NIB that represents the scene with the tab bar controller.


1 Answers

Issue #1

UIStoryboardSegue has an irritating flaw: its sourceViewController and destinationViewController properties are typed as AnyObject! (that's the case even in Objective-C (Id type)) and not as UIViewController, as it should be.

That same flaw creates havoc in your perfect and simple code. Here's how to rewrite it in order to fix the compile errors:

@objc(SEPushNoAnimationSegue)
class SEPushNoAnimationSegue: UIStoryboardSegue {
    override func perform () {
        let src = self.sourceViewController as UIViewController
        let dst = self.destinationViewController as UIViewController
        src.navigationController.pushViewController(dst, animated:false)
    }
}

NOTE: Apple fixed this thing in iOS 9. sourceViewController and destinationViewController are now correctly declared as UIViewController.

Issue #2

The Swift compiler stores its symbols using its own name mangling, and good ol' Objective-C does not recognize it in Xcode. Using an explicit @obj() solves the issue.

like image 194
Jean Le Moignan Avatar answered Oct 01 '22 11:10

Jean Le Moignan