Cocoa-Touch – Dismiss modal viewcontroller and pop viewcontroller at the same time - objective-c

How can I achieve this? Basically I have first a view controller that lists some items, if I touch one Item it will push another viewcontroller on the stack for a detailed view of that item, then if I choose "edit" it will present a modal viewcontroller, in this modal view controller I have the option to delete the item. If I choose to delete I want to dismiss the modal view controller and be taken back to the lists of items.
So for clarity ListViewController, DetailedViewController, EditViewController.

I would recommend using the DetailedViewController for editing as well, like the Contacts.app.
If you don't want to omit the EditViewController, then you could solve it with a delegate pattern, which pops the DetailedViewController before you dismiss the edit controller.

Related

Create back button

How do I create a custom back button on one of my storyboards, when I do a segue and I say push it creates a back button on Mac with but when I do a modal or model it does not create a back button?
Modally presented view controllers do not automatically get close buttons.
self.navigationController.leftBarButtonItem = ...
A pushed view controller will automatically create a back button if the navigation controller is shown.
You will have to create your own back button. In the view controller that you have presented via the modal transition, you have to put a toolbar on it. Put it at the top and if using autolayout set the constraints top, both sides and height. Then place a barbuttonitem in the toolbar. You can select a system button like done or cancel. Make sure the new view controller is the class that you created. Now you can control drag from the barbuttonitem to your .h file and connect an IBAction. Call it dismiss or something like that. In that method call [self dismiss viewcontroller:animated completion:nil]. This will bring you back to the original view controller. I am not a my computer right now so I am not sure the exact wording of the dismiss method, but it will auto fill for you. Good luck.
As Douglas explained, you need to create property of the button (UIButton or BarButtonItem) and in the viewcontroller .m file connect
- (IBAction)backButton:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
with the button. This will dismiss the current view controller and bring you back to the previous one :)

Bar Button Item : IBAction not fired in View COntroller

At the bottom of the Detail View Controller of a Split View Controller, I have added a Toolbar in the Storyboard. I have added three Bar Button Items in this Toolbar. Each Bar Button Item is hooked to its IBOutlet in the private interface of the Details View Controller, and also hooked to its IBAction implementation in the same controller, for example:- (IBAction)shouldPresentView:(UIBarButtonItem *)sender
The main view contains:
A Webview.
A small rectancgle view at the bottom of the screen, below the webview, created and added programmatically, and containing various buttons. All the buttons stay operational.
When touching any of the Bar Button Items, the button is temporary highlighted, but none of the IBAction methods is triggered (a NSLog() statement in every method is never executed).
After searching and reading equivalent topics, I went through several solutions:
I added manually the Details View Controller as the delegate of the Toolbar in the View Did Load method of the Details View Controller.
I emptied the cache of the Derived Data of the Product.
I checked that every IBOutlet has an address and is not nil.
I tried to add an action SEL manually in the View Did Load method for every Bar Button Item.
I removed the rectangle bottom view to be sure that it was not interfering with the responder chain.
I added a Bar Button Item in the Navigation Bar to check if the same problem occurs: no, the Bar Button Item fires the IBAction method.
Any help would be appreciated.
Developing with iOS 7 SDK in Xcode 5.0.2
The issue was coming from a Tap Gesture that I added to the main view to collect touches on the web view. So the touch on the Bar Button Items was probably captured somewhere by the main view before reaching the UIButtonBarItem. I changed the target of the Tap Gesture to the web view – more logical – and the issue has been solved.

How to modify objects within a modal view?

I have an app with several views. Taking into consideration the large main view, called MyView1, it is controlled by MyView1Controller. Within MyView1, there is a button that causes a modal segue to another view, whose controller is also MyView1Controller. This modal view has a couple UILabels, and a button that terminate the modal view, bringing the user back to MainView1.
Here is the problem... Let's say in my modal view there is a UILabel called sampleLabel. While in MyView1, a button is pressed, which executes the code:
sampleLabel.text = #"changed";
Since the UILabel named sampleLabel is not on screen for MyView1, and instead is part of the modal view from MyView1, nothing happens. However, when I click on the button to view the modal view from MyView1, the UILabel hasn't changed.
This is even more puzzling since the main MyView1 and the modal view that segues off of MyView1 are controlled by the same view controller, MyView1Controller.
Can someone please tell me how I can make code that executes during the user's interaction with MyView1 change things in the modal view, so that when they press the button and segue to the modal view, the UILabel's have already been changed?
Thanks!
First of all, Apple recommends (and it makes life a lot easier) to have one view controller for each view. So you should have a second view controller. In the second view controller you would have a property called sampleLabel. In the first view controller you could use different methods to set the sampleLabel.text. I would probably create a separate sampleLabelText property in the first view controller (could be an NSString *) and set it to the text you want when the user presses a button. Then in your
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
you would get your second view controller and set its property like this:
SecondViewController *svc = [segue destinationViewController];
svc.sampleLabel.text = self.sampleLabelText;
That's it. Hope this helps.
So I have had a similar issue that I resolved through 'delegation' but not through a segue schema. Here is a link to the stackoverflow question and my answer. Delegation
Hopefully this gets you going in the right path. Instead of modally presenting a view, I push a new viewcontroller onto a navigation stack but the same answer should apply, hopefully :P

Tab Bar controller disappearing when moving to another view (iOS SDK, Using storyboards)

I am building an iPhone app using storyboards and I have a problem with the tab bar controller. On one of the views that is linked from the tab bar controller (view1), there is a button that leads to another view (view2). On View2, there is a button that leads back to View1. Very straight forward. But when I go from view1 to view2, the tab bar disappears, and even worse, when I go back to View1, the tab bar is still gone...
How can I fix that? (I have yet to put ANY code in the app, there is only the storyboard and the apple provided AppDelegate Class (and also a main file I suppose, but I am not intending on touching that).
Any Reply is Highly appreciated!
If you do a modal segue from a view that is a tab bar view, it will get rid of the tab bar for the modal view you are presenting.
Secondly, when you segue you are creating a new instance of the view controller. So I am guessing you are segueing from view1 to view2 and losing the tab bar, then you are segueing back to view1. At this point you have created view1, view2, and a second copy of view1 that does not have a tab bar.
I would suggest one of two things.
1.) If you want to keep the tabs at the bottom when you segue from view1 to view2, then click on view1, at the top of the screen select Editor/Embed In/ Navigation Controller. This will embed your view1 in a navigation controller. Then if you change your segue from Modal to Push it will keep your tab bars at the bottom. The navigation bar at the top also make it easy to go back from view 2 to view 1 the correct way (by popping the view) rather than creating a new segue. If you do not like the navigation bar, then you can change the "Top Bar" property to "None" in the inspector. You will then need to create some other way in view2 to get back to view1. (BY POPPING THE CONTROLLER, NOT BY SEGUEING)
2) If you don't want to set up a navigation controller you will have a little bit harder time keeping the tab bar stuff at the bottom of the view2 controller. In fact, I'm not sure you can do it at all with a modal segue, you'd probably have to write some type of custom segue. Either way, If you want to transition back to view1 and get to the correct controller (not a new version without the tabs) then you need to attach an action to whatever button you are using to segue and use the following code (I also attached the code for navigation controller push segues, in case you create a navigation controller and get rid of the navigation bar.)
For Modal Segue:
[self dismissModalViewControllerAnimated:YES];
For Push segue:
[self.navigationController popViewControllerAnimated:YES];
Your best bet is to use the navigation controller method, as you are assured to keep your tabs. You can then either use the navigation bar to return (the easy way, no code needed) or you can get rid of it and use a button and the code above.
Good luck!
I had the same problem, i know this is an old question but [self dismissModalViewControllerAnimated:YES]; is deprecated in iOS 6.
What i used is:
[self dismissViewControllerAnimated:YES completion:nil];

How to temporarily hide UIPopoverController

I have a UIPopoverController which i want to drag items from it and drop them on another view.
I implemented the mechanism for Drag & Drop from a popover but i want it to be even better: while you dragging, i think the popover should be hidden so you could drop items right underneath it.
Since UIPopoverController is not a "Classic" view controller i dont know how to hide it temporarily without dismissing it irreversibly.
BUT since UIPopoverController presents something on the screen, there got to be a "View" somewhere inside that popover controller that i can hide and show...
Any suggestions?
Would it work to "hold into" the view controller in the popover in some way (like an ivar of the object that presents the popover) and then just dismiss the popover? That way, the view controller will still be there and when you want to show it again, you can.