after searching, looking and experimenting I continue to fail at a specific problem.
I am a programming novice, so by chance I simply did not use the right search term - so please bear with me in case I created a duplicate.
Here is the situation I'm struggling with.
ViewController 1 has two UIToolBars. The upper Toolbar Button displays a value, the lower Toolbar Button triggers an action in ViewController 2 which happens to be a TableViewController embedded via a container view.
The reason for that is - I need the keep the ToolBars in place while scrolling the TableView.
I use a delegate protocol to send the value from VC2 to the upper Toolbar in VC1, works fine.
But I fail to trigger the action in VC2 using the lower toolbar in VC1.
I kindly want to ask three question:
1.) What is the best practice to trigger the action / solve the problem ?
2.) How would the code look actually like when using a method named "theButtonWasTapped" on VC2 ?
3.) Is the method to embed the TableView via a container view the right way or is there a better way?
I tried to read through various documentations, but I don't even know where to start in this case.
Any help would be greatly appreciated since I'm sitting on this frustrated for days already and don't know where my mental block ends and my absent knowledge starts.
Thank you so much!
If VC2 is embedded in a container view, then it will be a child view controller of VC1. Therefore you can access it from VC1 with self.childViewControllers[0]. So you should be able to do something like this in VC1:
-(IBAction)toolBarButtonTapped:(id) sender {
SecondViewController *vc2 = self.childViewControllers[0];
[vc2 methodInVC2];
}
Related
I have been searching all over the web but I can't seem to find the answer to this.
Currently i am using presentViewController to start new ViewControllers, but on certain view controllers i do not dismiss it and call over it. I currently am not using any navigation controllers or anything like that.
I am just worried that if I call the same viewController again via presentViewController, that the same viewController would have 2 running instances.
Is it possible? Or does the iOS framework automatically reuse the idle viewController?
If so, how do i remove the idle view controllers?
Thank you! (I was holding back my question and tried to find it all over the web, so if you can point me in the right direction, it would be very helpful thanks!)
iOS will not reuse your view controller, you can easily check it yourself by printing your view controller in viewDidLoad, you will notice first that viewDidLoad is called every time, and next that all objects have different addresses.
Unless you create thousand of them, or the navigation of your app doesn’t let you come back to an “idle” view controller, I would not say this is an issue though.
I don’t see any clean way to remove a view controller from the memory without calling “dismiss”. You could try to:
- “refresh” your view with new data.
- use something like UIPageViewController if the workflow of your app allows this kind of behaviour.
- rework the navigation so you can dismiss the view before calling another one
Good luck
I have the following storyboard structure:
Essentially I have a UIButon that I want to link to the "TargetTableViewController" via an IBAction - NOT by 'Ctrl+Dragging' but programatically by way of some code instead.
I believe I have to present "NavViewControllerTwo" as a NavigationController (as it 'holds' "HoldingViewController") and have tried various renditions of this via the many threads at StackOverflow. But this did not work.
I suspect it is because the actual "TargetTableViewController" is embedded in a ContainerView of "HoldingViewController" and that causes the problems.
I have tried implementing other renditions of presenting "TargetTableViewController" as a child of the "HoldingViewController", but that caused me more problems.
I realise I may have over-complicated things here, but the reason for the container is I want to display a BannerView at the bottom of my eventually presented view.
If anyone can be kind to point me in the right direction, I will be very grateful. Thank you for your time.
** CLARIFICATION **
I am actually trying to present "HoldingViewController" as that would effectively display my "TargetTableViewController".
Please refer to amended image below:
Apologies for the confusion and lack of clarification.
Setup a segue from MainViewController to NavViewControllerTwo in the storyboard, give it an identifier and then perform that segue from your code on the button press action using [self performSegueWithIdentifier...
As someone who usually used separate xibs in the past I thought I'd give storyboard a go as it seemed a lot simpler to use and much easier to develop with. I've been writing an application where the essential set up is this:
At the top of all this is a UINavigationController (first level). Then I have Multiple UIViewControllers (second level) with buttons in them which you can tap to switch between the second level UIViewControllers.
However a problem occurs when I start switching between the second level UIViewControllers. I first thought this was an initialisation problem with the NSMutableArrays because in my code I have a NSTimer set to loop periodically and found when I set a breakpoint during it, when I went forward to the next timer tick event there appeared to be different instances of the same NSMutableArrays and it seemed a gamble to try and insert new values into these array with it sometimes working, sometimes not (as it may or may not insert into the correct instance).
Then, looking at the memory usage under Debug Navigator I found the issue. Each time I "switched" between the UIViewControllers a new UIViewController was being initiated, along with all new variables.
The code I am using to switch between them is
-(void) perform {
[[[self sourceViewController] navigationController] pushViewController:[self destinationViewController] animated:NO];
}
Or essentially a push segue transition. This also explains why when I tried to switch back to my view, the data on that view was lost as it is a complete new view.
Does anyone know how to switch between multiple ones of these UIViewControllers (say 5) essentially like a UITabViewController would except without the tab bar being present?
First option you can do this: You can use a tabbarcontroller for switching viewcontroller and hidden the tabbar. Then on buttonclick setthe tabbar index.
Second option you can do this: Create one more view controller and in this viewcontroller subview the all switching viewController and when you want to switch viewcontroller just bring that viewcontroller view to front by delegate.
Do you need the navigation bar and other features provided by your top level navigation controller?
If not, you could use a UIPageViewController instead.
You set up all your second level view controllers and then just have to tell the page view controller which one to display.
If you implement the associated delegate methods, it will automatically provide swipe gestures to switch between them and nice animations to get them on and off screen.
You can also get it to put a UIPageControl at the bottom showing a dot for each VC with the dot for the current VC highlighted.
I created a storyboard for my app which contains the following:
Initial view controller on my storyboard is a Tab Bar Controller (let's call it myTabCtrlr)
myTabCtrlr has forward segues pointing to several other controllers:
a. First segue points to a custom UIViewController (let's call it vc1) on which I create an interactive UIView (let's call it popview1) which is initially hidden. There's a button (let's call it showPopView1) on vc1's view which when clicked would show popview1
b. Second segue points to a navigation controller, which embeds a view controller with 3 buttons, each pointing to an (end) controller.
c. Third segue points to another navigation controller with a similar setup as (b)
On several of these (end) controllers, there's a button similar to vc1's showPopView1 that when tapped, I'd like to switch back to vc1 and programmatically bring up popview1, which I'm doing as follows (but it's not working):
myTabCtrlr.selectedIndex = 0;
//I get a handle to vc1 then
vc1.popview1.hidden = NO;
When I do that, it goes back to the first tab and shows vc1 view (which is good) but it does not show popview1. I tried many different ways to do it but no luck.
Note that if I'm actually on vc1 and I tap the showPopView1 button, then popview1 comes up normally.
Does anybody know why that is the case? This only started after I transitioned to using storyboard. thanks.
After spending hours looking at various ways to solve this problem, and focusing on reverse segues and similar methods, I was able to solve it using a totally different method.. I wanted to share this with others so nobody has to waste so much of their time (although I noticed slow response to my post. Maybe I didn't make the subject attractive enough :)..
It's a really simple solution, but quite effective. I used a Singleton pattern object. When coming from the rest of the tabs, I set a flag in the singleton that vc1 checks in its viewWillAppear method, shows popview1, and immediately resets the flag.. works like a charm!
I'm moving my App to Storyboards and, so far, so good.
However, I've found something that I don't really understand and worries me. I would appreciate if someone can provide some insight on this.
My app uses a normal Navigation Controller. For moving "forward" to new View Controllers, I'm using custom segues; no problems there. However, there's a point in the App where I want to move back to the beginning of the Navigation Stack. I have also configured that "navigation" using a custom segue, for that, I created the segue in Interface Builder by dragging the last view controller to the first one (that already looks weird to me), and I've implemented the custom segue perform method in the following way:
-(void)perform
{
UIViewController *src = (UIViewController *)self.sourceViewController;
UIViewController *dest = (UIViewController *)self.destinationViewController;
[src.navigationController popToRootViewControllerAnimated:NO];
// Custom animation code here
}
... It works great. However, I don't understand why it works. In my mind, the custom segue should be instantiating a new instance of my first view controller and assign it as "dest", but it looks like the segue is smart enough to realize I want to navigate to a previous, existent, instance of a View Controller and, instead of creating a new instance, it assigns to "dest" the existing one.
Does anybody know if using segues in this way is ok? Is it possible that it works by chance but might stop working in the future? Am I wasting memory in anyway as the segue is instantiating a View Controller I'm not going to use?
Thanks a lot in advance!
Am I wasting memory in anyway as the segue is instantiating a View
Controller I'm not going to use?
Yes sir! By using a segue, you effectively allocate a new view controller as it's needed to set the DestinationController property for your custom segue. Test by yourself : add a static counter into your root controller, increment it each time this class is initialized and display it in your view : you'll see it getting incremented every time you pop to root using this trick.
Does anybody know if using segues in this way is ok?
As long as you're effectively wasting memory, no!
There's at least one solution to this problem : release the DestinationController of the segue in your (void)perform implentation. This is really quick to implement, but kinda ugly since you allocate and immediately release your view controller every time... even if it's better than just leaking it, it's not what I'd call a good practice!
To my mind, a better way to achieve what you want would be to not use a segue for that transition, just to use a button or whatever and call popToRootViewController:animated when getting a touch on this button.
Is it possible that it works by chance but might stop working in the
future?
For both the first solution I suggested and the way you're currently doing it, I see absolutely no reason : these are not complicated tweaks, just 'bad-implemented' standard navigation. The second solution is perfectly normal so no worries.