Swift Changing View with performSegueWithIdentifier / programmatically - ios

So I am trying to programmatically change views when some condition is triggered.
I have created a simplistic project, whereby I want to change to View 2 from View 1, once View 1 has loaded. In reality, there is some logical operation that checks if some variable is true, then it invokes the performSegueWithIdentifier function, however to keep this as simple as possible I have removed that code.
The steps I have performed are, firstly to create two views:
I then click View 1, and inspect its properties and then created a manually triggered segue to View 2
I selected the 'Show' option, so now my Views are connected by a segue
Then under attribute inspector, I give the storyboard segue and identifier of "GoToView2".
From there I go to the ViewController.swift file, and in the viewDidLoad function. I insert performSegueWithIdentifier code:
self.performSegueWithIdentifier("GoToView2", sender: self)
However, when I then run the code. View 2 does not load, and only View 1 appears in the simulator.
Any help and explanation would be greatly appreciated.

I have had the same problem with attempting to perform a segue inside the viewDidLoad function, and the error that comes up seems to be that the view you are attempting to load with the segue is not in the window hierarchy.
Changing the call to viewDidAppear seemed to fix the issue as mentioned in this other post: whose view is not in the window hierarchy (note the language is objective-c not swift, but it can be translated).
It seems you have to be careful at what point in the view lifecycle you want to change it to another view controller - at viewDidLoad all the views in the storyboard are not available.
BTW, without having a conditional in there, the segue will be fired (and load the view controller) every time the view is loaded!

I am working on a project that does exactly what you want to do. Perform segue with identifier is the right thing to do. My project also does the check in viewDidLoad.
All your steps look fine, so I believe it's a subtle issue. Check the identity inspector to see if you connected the view controller with view 1

Related

UIStoryboard segue fail

I created a simple Show segue by dragging from a UIButton to another UIViewController in storyboard view. It's configured as all of my other VCs and has a custom class that is specified, with linked IBOutlets and IBActions (which appear to link up when I hover on them).
NOTE: When I first created it, there was no view controller class specified. I've recreated it several times since then, but I'm wondering if some value is cached that's pointing to a default view controller. Although I get print lines from the correct view did load method...
For some reason, when I run the app and click the button it links to a black, empty view controller. The navbar pops up without any back button (these views are embedded in UINavigationController).
After looking up other posts about this kind of issue, I've deleted and recreated the segue, cleaned my build folder and restarted Xcode. There's no source code involved in this, just storyboard configuration. Any help on next steps?
According to the code shown in your screen shot, the problem would be that NodeEnrollmentViewController is a subclass of UINavigationController. That's wrong. It should be a subclass of UIViewController.
You cannot (and do not need to) segue from a view controller within a navigation controller to a navigation controller.

Update UI from another controller

I would like to update my UILabel on click the button of ContainerView contains table ViewController. When I try to do this UILabel's outlets reference shows nil value exception. I am using Swift3 with Xcode8
Most probably the problem you are seeing is due to the fact that the view that owns this label on another view controller is still not loaded.
This happens often, basically because views owned by a view controller are instantiated in a lazy manner, this means that they are loaded only when required.
To fix that before setting the value on the label, just preload the view by doing something like.
_ = another_viewcontroller_instance.view
In this way you are forcing the destination view controller to load the view and creating all the necessary connection on the xib.
Even if this fix works, this is not a good way to deal with this kind of pattern (sending info from a VC to another), but since you didn't gave us any further detail this is the only solution I have.

UINavigationController for infinite navigation (nested folders)

I need to navigate inside folders and files in directory (from server). The problem is that I don't know the number of folders so it's not possible to use performSegueWithIdentifier statically. How can I use navigation controller with dynamically number of view controllers in swift? I want to "push" a new view controller every time a user tap on a folder in order to list files/folders inside it and I want to do it with UINavigationController so the user have the possibility to go back with "previous" button.
Both storyboard and programmatically approaches are ok.
Thanks you
Storyboards and segues are just a crutch. Think about how you would do this without them. At each level, to go down a level, you would just instantiate a new view controller and push it onto the navigation controller stack with pushViewController:animated:.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:
And in fact it takes only one view controller class to do this, since any instance can create and push another instance of its own class. The display of one folder is exactly like the display of any other.
So if you wanted to configure this notion in a storyboard, you would have a circular segue, that is, the view controller would have a push / show segue leading to itself.
I agree with #matt's answer, just create the controller and push it. For sake of completeness, you can do this in a Storyboard with a segue.
Here's how:
So that you can call the segue programmatically, add an additional prototype cell to your tableView. (You do this because you don't want the segue to be automatically triggered when the tableViewCell is selected. By using an additional prototype cell, the segue can be wired up, but it will never be triggered automatically since this prototype cell will never actually be instantiated.)
Control-drag from this prototype cell to the viewController icon at the top of the tableViewController. Select "Show" from the pop-up.
Find this segue in the Document Outline View and give it an identifier such as "showFolderSegue" in the Attributes Inspector.
Now, when you want to trigger the segue, call: self.performSegueWithIdentifier("showFolderSegue", sender: self)
You can use prepareForSegue to set up the new tableViewController as you normally would.
This method too works with a single tableViewController.

Whose View is not on hierarchy

I am getting this error from swift and I am unsure why there is only 1 view controller that actually has this occurring the error is whose view in not on the hierarchy. My question is why would this occur for one of the view controllers presented from this primary viewcontroller button but not the other view controller linked from this primaryview controller? Is there something that could cause this to occur specifically. I am trying to present this modally and it is being called programmatically so that I can pass data objects.
For me it just seems very strange that this would occur for one viewcontroller presented from this viewcontroller but not the other. The only thing I did which I didn't think would effect this was duplicating the primary viewcontroller but again why would one work but not the other? The problem is specifically happening with iOS.
Should have checked the buttons to make sure they were linked correctly. I realized after reading a few of the same questions that I should check the button selectors under the view controller references tab to see what was linked to what. I had 2 actions doing 2 different things for 1 button by accident I must have miss clicked the control drag function.

ios methods becoming zombies

I added uiviewcontroller subclass in my arc enabled project, i added a button created the ibaction for it and inside it contains no code, all done through interface builder no manual code, but when i am tapping the button i am getting exc_bad_acess. Instrumentation saying its a zombie attack when pressing button. I really dont understand whats going wrong. I created the new project and done the same thing it is working perfectly. Same thing happening when i am defining method to dismiss keyboard (resign first responder).
this generated when i crtl+dragged from button to interface implementation, i choose the ibaction and name and this code is generated.
- (IBAction)pushh:(id)sender;
and the implementation inside .m file
- (IBAction)pushh:(id)sender {
}
thats all ...
Just to wrap up the offline conversation, a couple of thoughts:
Your zombie is undoubtedly a result of the ecnObj falling out of scope. If it's an ivar of the view controller, then that premature release problem goes away.
You do not want to create a new view controller and then use its view in transitionFromView. If you want to transition between view controllers (in iOS5) you should use pushViewController or presentViewController (or if you're using a container view controller, you can pursue transitionFromViewController). See View Controller Programming Guide's discussion of "Presenting View Controllers from Other View Controllers".
My solution was to delete the button iboutlet in ib, also delete the ibaction in ib, and then connect it back.

Resources