switching between uiviewcontrollers without a UI navigator control - uiviewcontroller

I want to switch between 2 UIViewController classes programmatically without any extra UI control like a UITabBarController that adds a UI to the application.
My main loads the first view controller with addSubView.
vc1 = new viewc1();
window.AddSubview(vc1.View);
window.MakeKeyAndVisible ();
I can load my second viewcontroller from the first one with PresentModalViewController
vc2 = new viewc2();
PresentModalViewController(vc2, true);
but i need to switch back and forth, and to release the old viewControllers to save memory.
What is the best way to do this?
DismissModalViewControllerAnimated(false); in the 2nd view controller isnt releasing memory and I dont want modal "windows" as it doesnt seem optimal. I have a custom UI so the tabbar controller is not wanted.

You can do it in simple code. But you can't release the view controllers as it required to handle user interactions such as button tap events etc. Adding a view to window will only preserve view instance. If you release your view controller instance, you could get a bad access error or unrecognized selector error.
So let your main code be
if(vc1==nil)
vc1 = new viewC1();
window.addSubView(vc1.view);
window.MakeKeyAndVisible ();
And your switch code will be
if(vc2==nil)
vc2 = new viewC2();
if(vc1.view.superview!=nil){
vc1.view.removefromSuperView();
window.addsubview(vc2.view);
} else {
vc2.view.removeFromSuperView();
window.addsubview(vc1.view);
}
Now in dealloc method add
vc1.release();
vc2.release();
Thats it...Hope this helps...
I just followed your syntax

You can use a UINavigationController still, you don't need the extra UI that it provides. You can use the .Hidden property of the UINavigationBar to hide that. To switch between views you can just use PushViewController(controller, animated) to push the new view. If you want to release the old controller then you can just set the UINavigationController's .ViewControllers property using:
navigationController.ViewControllers = new UIViewController[1]{ vc2 };
this will remove the reference of the first controller and make the second controller the root. (this will also work the other way around!)

Related

transitionViewForCurrentTransition is not set error

Here is my project:
It only crashes in iOS8.
I have 5 view controllers:rootViewController,A,B,C and D.Every view controller has a button that present another view controller except D. Evert time presenting a view controller, the Manager singleton object will add the presented view controller into an array. The last view controller D, which has a dismiss button, will use the array to dismiss view controller,and here's the code:
while ([Manager sharedManager].viewCont.count) {
UIViewController *viewController = [[Manager sharedManager].viewCont lastObject];
[viewController dismissViewControllerAnimated:NO completion:nil];
[self removeViewCon];
}
But I meet a crash,which shows:
I use some manage object because I want to manage the view controllers in some case.
My question is why this crash occurs when in "while" statement? Is it about runloop or iOS8 has some features like UIPresentationController that will not allow this case? And how to fix this?
Thanks in advance.
I just hit this also. It seemes the UIPresentationController crashes if it's presenting view disappears before it is done using it. One fix is to keep the view controller around a little bit longer.

Swing JTabbedPane : addChangeListener or addContainerListener or both?

I have one swing code written by other person. For swing tabbed pane, he has added both change and container listener and both calls the same method:
addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent theEvent ) {
someMethod();
}
} );
addContainerListener(new ContainerAdapter() {
public void componentAdded(ContainerEvent theEvent) {
someMethod();
}
public void componentRemoved(ContainerEvent theEvent) {
someMethod();
}
} );
Whenever tab is removed from this tabbed pane, it internally calls JTabbedPane.removeTabAt(int index), which in turn calls fireStateChanged() causing new change event listened by change listener.
Now as new component (tab) is removed from tabbed pane, it also calls componentRemoved(ContainerEvent theEvent) method of container listener.
Both change even and container events, then calls same method someMethod(), which does set background and foreground colors.
I would like to know, if this kind code might cause some issues. Recently we are facing random IndexOutOfBoundException exeptions. I am just wondering, if this is causing this issue.
Also as per my understanding in swing, once event is listened, logic inside it should be executed using worker thread (e.g. SwingWorker). Please let me know if this is correct.
I am new to swing, thus any hint would be appreciated.
Thanks.
Whenever tab is removed from this tabbed pane, it internally calls
JTabbedPane.removeTabAt(int index), which in turn calls
fireStateChanged() causing new change event listened by change
listener.
This is true if the removed tab is also the selected tab. In the other cases, you won't be notified.
You need to choose what event you want to listen to:
Addition/Removal of components?--> go for ContainerListener
Selected tab? --> go for ChangeListener
I would like to know, if this kind code might cause some issues.
Recently we are facing random IndexOutOfBoundException exeptions. I am
just wondering, if this is causing this issue.
Since there is no line in your sample code that could throw that Exception, it is impossible to answer your question. Post an SSCCE that shows your issue.
Also as per my understanding in swing, once event is listened, logic
inside it should be executed using worker thread (e.g. SwingWorker).
Please let me know if this is correct.
It depends:
If you need to modify anything in the UI, anything related to Swing, it needs to be executed on the EDT (Event Dispatching Thread) and thus, SwingWorker is not an option.
If you need to perform business logic operations, and especially if they can be lengthy, then you should indeed use a SwingWorker or any other mechanism to execute that code in another thread than the EDT. Consider visiting the Swing tag wiki on "Concurrency"

How to navigate between views in storyboard without using Navigationbar or Tab Bar

I have three UIViews in my storyboard, how can I navigate between them with push like animation without using Navigationbar or Tab Bar? I have my custom buttons on views.
Control-click and drag does not work with this error:
Terminating app due to uncaught exception 'NSGenericException', reason: 'Push segues can
only be used when the source controller is managed by an instance of UINavigationController.
You can present/dismiss VCs without using Navigation Controller.
Notice the terminology: push/pop is used for VCs on the Navigation Controller stack, present/dismiss is for VCs on other VCs (not on a Navigation Controller stack).
There are a couple of way to present a VC on top of another VC and it depends on whether that VC exists on your SB as well:
1- If the VC does not exist in your IB and you are just creating and presenting the VC programmatically, you can do:
MyUIViewControllerSubclass *myLittleSubclass=[[MyUIViewControllerSubclass alloc]init];
myLittleSubclass.view.frame=self.view.frame; //this is for exmaple only
[self presentViewController:myLittleSubclass animated:YES completion:nil];
With the above, you obviously would have to add the MyUIViewControllerSubclass Objective C class first to your project (using add files) and import it to whichever class you are putting the above code.
Later to dismiss it, you can use the following code from the MyUIViewControllerSubclass class itself.
[self.parentViewController dismissViewControllerAnimated:YES completion:nil];
2- If you have already added the MyUIViewControllerSubclass Objective C class to your project (using add files) but you intend to use the IB to design it and you have added the VC in the IB and changed its class in the IB to MyUIViewControllerSubclass, you can then use this code:
mySubclass *myLittleSubclass=[self.storyboard instantiateViewControllerWithIdentifier:#"theVC"];
[self presentViewController:myLittleSubclass animated:YES completion:nil];
You can dismiss same as above.
Make sure that you click on the VC in SB and in the identity inspector, put the SB ID as "theVC" and check "Use SB ID".
Hope this helps.
The push animation is only supported when using a UINavigationController inside the parent view. You can embed your view controller in one edit->embed in->navigation controller in IB, and hide the navbar. This may provide the functionality you desire.

Adobe flex popup single instance

I need to create a flex popup which would be a single instance but we can make it visible and invisible whenver we want to display it. I am not sure we can implement this kind of functionality using createPopup or addpopup method. Instance must be one and need to update it every time some event happen and make it visible or invisible.
thanks
createPopUp requires a class name. All the internals of creating the popup are in that method. You won't be able to use createPopUp with an existing instance of a window. However, when you call createPopUp, the results you get will be the instance of the new popup you just created.
However, addPopUp does accept an instance of an already creating component. You'll want to proceed in one of a few ways:
1) if the popup instance exists; use addPopUp; otherwise use createPopUp:
if(myPopUp){
PopUpManager.addPopUp(myPopUp, etc...)
} else {
myPopUp = PopUpManager.createPopUp(this, myPopUpClassName, etc..);
}
2) Create the popup yourself and always use addPopUp
if(!myPopUp){
myPopUp = new myPopUpClass();
}
PopUpManager.addPopUp(myPopUp, etc...);
Whenever you want to hide the pop up, do so using the removePopUp() method. This method will not destroy the pop up instance, just remove it from view.
PopUpManager.removePopUp(myPopUp);
You're going to have to figure out how to store the reference to your popup outside of the PopUpManager.
And I warn you that all the code I wrote here is psuedo code.

Flex Mobile persist view data

I built a simple hello world app to check out the Flash Builder 4.5 mobile capabilities.
Here's how it works:
The Default View asks for name in an textinput and has a continue button
When you click the continue button it pushes a new view in the viewNavigator which just displays "Hello " + name in a label.
When you click anywhere in this view, it pops a view (i.e. itself) from the viewNavigator, to go back to the default view
I see only 1 issue with this:
When I get back to the default view, it is in its initial state, i.e. the textInput is blank. It seems as if the viewNavigator created a new view of the default view's class and pushed this, instead of just removing the top view and displaying the previous one.
I see this being especially problematic for programs which display data in a grid and you can click the data to view the detail...when you get back, the grid will be empty.
Any ideas or gotchas to solve this?
EDIT:
Project name: HelloWorld
Code below:
HelloWorldDefaultView.mxml
protected function button1_clickHandler(event:MouseEvent):void {
navigator.pushView(HiView, tName.text);
}
HiView.mxml
protected function view1_clickHandler(event:MouseEvent):void {
navigator.popView();
}
protected function view1_creationCompleteHandler(event:FlexEvent):void {
lblHello.text="Hello " + data;
}
Screenshots
Initial screen
Screen 2
Clicking on screen 2 gets us back to initial screen. Notice the blank textInput
Have you tried to set destructionPolicy="never" and then
protected function button1_clickHandler(event:MouseEvent):void {
data = tName.text;
navigator.pushView(HiView, tName.text);
}
to store the data in current View's data - before changing to another one?
That's the way it is supposed to work for mobile applications.
Check out this article: Flex 4.5 (Hero) – Persistant Data in MobileApplication
As they write:
Each time a View is removed from the display list (via popView() or pushView()) its instance is destroyed, but its data model is stored in memory.
In order to save session state for a View, you must modify the data property. This property will be requested when destroying the current instance of the View class. And the data property value will be assigned back to a newly created instance of the same View class when navigating back to that view.