How to access a Storyboard's segue through code? - swift

There are couple for segues in the storyboard, all with identifier. Is it possible to access them through code and change its anchor?
Something like this:
mySegue1.anchor = someOtherView

try this
self.performSegueWithIdentifier("YourSegueIdentifier", sender: self)

I think you may chnage anchor's view controller with the following codes, as I wrote in above comments, I believe you can create a Segue programmatically and the following code will create a new Segue for you not just change an old already drawn Segue's anchor programmatically.
Most of the good programmers think that you can not create a Segue by code, thats why changing a Segue anchor is not believed to be a performable job. But it is
It is an 'Objective C' version but you may easily convert it to 'Swift' if you like.
UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"OtherViewControllerId"];
MySegue *segue = [[MySegue alloc] initWithIdentifier:#"" source:self destination:toViewController];
[self prepareForSegue:segue sender:sender];
[segue perform];
However if you need to create a popover menu using a gesture recognizer, then just create a gesture recognizer and activate your pop over in gesture's action func ( handleTap() )
take a look at the following link, you may create your popover in storyboard and then write the rest of the code or completely do it by code, both are fully covered here.
http://gracefullycoded.com/display-a-popover-in-swift/
as I understood which I hope I get it right, you are about to create a pop over on your view controller which has a tab gesture defined on it. and you succeed to do the job but the pop over menu is mislocated. if this is the case You dont need to use a segue or a storyboard, all parts can be done by code easily.

What you can do is making an invisible anchor frame (ie a label ) and using that as anchor for the segue. I had som buttons and use this approach and it works fine. If you replace my buttons with you dynamically created objects.
#IBAction func rearHubActionUIButton(sender: UIButton) {
invisibleAncherLabel.frame = sender.frame
performSegueWithIdentifier("PopSelection", sender: sender)
}
The popover will then appear with your object as anchor.

Related

Segue from SKScene and back again cause game to "freeze"

I'm developing a SpriteKit game with two different scenes (MainMenu.sks and Settings.sks). I also have a UIViewController.
From the MainMenu the user can tap "Settings" which causes the app to load the Settings.sks scene and present it.
It's done using this code:
// Move to Settings.sks
let transition = SKTransition.flipVertical(withDuration: 1.0)
let loadScene = SettingsScene(fileNamed: "SettingsScene")
loadScene?.scaleMode = self.scaleMode
self.view?.presentScene(loadScene!, transition: transition)
From the Settings.sks scene the user can tap on the SKLabelNode "Change username" which causes the app to load the UIViewController by performing a segue.
The UIViewController contains a UITextField and a UIButton. The UIButton takes the user back to MainMenu.sks.
However, when I'm doing so the it's like a new scene of MainMenu is placed upon the other causing the app to sort of "freeze" and not responding to any touch gestures. I can see from the nodeCount that the nodes are doubled.
A workaround is loading the UIViewController directly from the MainMenu.sks (also via a segue). By doing so, the app works just fine.
I'm suspecting the problem to be the way I load Settings.sks.
What am I missing? Maybe unloading the entire SKView before exiting?
Performing a segue will present a new view on top of the stack. By using a segue to return to the main menu, the app will create a new main menu on top of the UIViewController. I can't say for certain why your app freezes. But I would guess that there are some properties or functions that don't work properly when a new main menu is created and presented from your UIViewController.
I recommend unwinding the segue used to reach the UIViewController instead. This way you can return to the previous view and conserve memory.
You'll need to write an IBAction in your destination view, the main menu in this case:
#IBAction func unwindToMainMenu(segue: UIStoryboardSegue) {
}
Then ctrl-drag from your button in the UIViewController to the exit icon in storyboard. You should see an option to use the IBAction unwindToMainMenu that you just wrote. This will create an unwind segue.
A complete guide to unwind segues and multiple examples can be found in this answer: What are Unwind segues for and how do you use them?

UISwipeGestureRecognizer implementation with Swift

I have a tabbed application with three tabs and I want to make the transition between them with a Swipe Gesture Recogniser.
I dragged and dropped one Swipe Gesture Recogniser from the Object Library to the first View and one to the third View. I added two on the second View so I can swipe both left and right.
I changed the Swipe to the Attribute Inspector so it will swipe right or left as needed
Using the Assistant editor I used Control+Drag to create Actions to the proper ViewController.swift files.
What else should I do so the swiping should work?
All of the guides I found online are in Objective-C. Does anyone know how to complete these Events in Swift?
You have to apply custom view controller transitions. Otherwise your tab transition will be neither animated (by default tab switches are without animation) nor interactive (you cannot control the speed with your gesture).
A good starting point is Colin Eberhardt's VC Transitions Library. It's in Objective-C but you can use it from Swift.
Just a warning: Custom view controller transitions are an immature part of iOS 7 or 8. If another view transition is initiated while a custom one is still running, the UI will more or less lock up. And it easily happens: user presses yet another tab before the transitions ends, your code switches tab and pushes a view controller at the same time, user manages to press a button during transition that opens another view controller etc.
EDIT: I have added a sample project to github and it fully works. Feel free to copy it or wok on it.
You don't need to add them in the story board. First add this code to your initializer (in most cases viewDidLoad):
var swipeGesture = UISwipeGestureRecognizer(target: self, action: "SwipeToNextViewController:")
swipeGesture.direction = UISwipeGestureRecognizerDirection.Left
view.addGestureRecognizer(swipeGesture)
var swipeGestureReverse = UISwipeGestureRecognizer(target: self, action: "SwipeToPreviousViewController:")
swipeGestureReverse.direction = UISwipeGestureRecognizerDirection.Right
view.addGestureRecognizer(swipeGestureReverse)
Now that your viewController has the gesture, SwipeToNextViewController() will be called if you swipe left, and SwipeToPreviousViewController() will be called when swiped Right.
Write the code for the two functions associated with the gestures:
func SwipeToNextViewController(gestureRecognizer:UISwipeGestureRecognizer)
{
performSegueWithIdentifier("next", sender: self)
}
func SwipeToPreviousViewController(gestureRecognizer:UISwipeGestureRecognizer)
{
performSegueWithIdentifier("previous", sender: self)
}
These two functions will be called when view has been swiped left or right. Add your code in it (in this case performSegue) and you can segue to another viewController.
Hope it helps :)

UIModalPresentationStyle.PageSheet does not display ViewController content

I currently have a segue that presents a VC modally in PresentationStyle.PageSheet. I have done this both programmatically and with just the storyboard. I get the same result with both approaches, the modal pop over presents itself but does not show any content inside the UIView from the VC. It will only show the background color of the view and that is all. I also want to point out that everything is displayed if I do a default modal segue (full screen) but fails with page sheet presentation style or with using UIPopoverController. Here are some screen shots that show what I am talking about.
This is what it looks like in the storyboard:
This is what it looks like in the simulator and on an actual ipad:
Here is what my coding approach looked like:
#IBAction func addPickUp(){
var addPickupVC = self.storyboard?.instantiateViewControllerWithIdentifier("pickup") as AddPickupViewController
addPickupVC.modalPresentationStyle = UIModalPresentationStyle.PageSheet
self.presentViewController(addPickupVC, animated: true, completion: nil)
}
This written in swift for ipad ios8.
What am I missing here? Any constructive feed back is appreciated!
EDIT: Here is the document outline of the VC that is to presented modally.
Your code for presenting popover is correct.
Probably there is problem with AutoLayout constraints.
As you can see your popover is presented but label is missing.
Remove your AutoLayout (they will be auto generated) and see if the label will be visible now
Try to add new label. Drag and drop it, and don't specify any constraints
Debug your view
Click "Debug view hierarchy" button at the debug panel
Now you can see your view hearty. Select your label, if it's present and see it's constraints.
Check your AutoLayout constraints and Label is present in correct Size Classes
Size classes is shown bellow UI designer. In my case it's (Any Any).
It means that it's for all sizes and all devices.
Also check that your constrains is not removes at runtime.
You can see it on inspector in right side.
In the storyboard, you can simply control drag a connection from your button to your presented Viewcontroller. There's no reason to have a #IBAction for this. If you use the InterfaceBuilder approach you can optionally set the presentation style on the segue to PageSheet.

How to return from "partial curl" type segue

I am attempting to use a partial curl segue to reveal an options view underneath my main view. This is identical to touching the "page curl" button in the bottom right corner of the Maps app.
I can successfully execute the segue to the option view, my problem is returning to the main view. I'm hoping to replicate the reverse animation effect of the Maps app as the edge curls back flat and the main view is shown.
It doesn't seem logical to create another segue to return back to the main view (what I would do if I were not using the partial curl segue) because the return segue is associated with the original segue.
Thanks in advance for any help!
try
-(IBAction) buttonPressed{
[self dismissModalViewControllerAnimated:YES];
}
This should work!
Or i believe you can press the back side of the turned page and it will return automatically with out adding any code! Hope that helps!
In Swift dismissModalViewControllerAnimated has been renamed:
#IBAction func handleAddButton(_ sender: Any) {
self.dismiss(animated:true, completion: {})
}

three20 PhotoVC: editing rightBarButton

I've created a subclassed TTPhotoViewController and presented modally in a navigation controller.
When I try to set the rightBarButtonItem, it won't change away from the "show all" button that would take me to the gallery.
I can set leftBarButtonItem but not right.
I tried created my own view controller which held the photo view controller, initialized it in VDL then set the frame and added subview, but I only saw white.
I want to use T20's functionality of auto-rotation, swipe to scroll, etc.. but I want to only show this detail view without the gallery, and let the user close it with a button on the nav bar.
Any suggestions?
it's too late but here is my two cents: subclass and override the didLoadModel
-(void)didLoadModel:(BOOL)firstTime;
{
[super didLoadModel:firstTime];
self.navigationItem.rightBarButtonItem = nil;
}
Looking at TTPhotoViewController.m, it appears that the "See All" button is created in a function called updateChrome; and it looks like there is no elegant way to override the creation of that button. So the best option I can think of, although this is somewhat clumsy, would be to just subclass TTPhotoViewController, and override that function. Either copy the code into your own function and tweak it (and don't call super), or call super but then assign a new button to rightBarButtonItem.