How to fix segue problems between ViewControllers using swift? - ios

I got 3 view controllers. On first page user defines some variables and when he executes it, it goes to second page with segue.identifier. On second page a time counter starts and an item price is decreasing over time. Whenever user uses buy button, the program prints on third page item number, updated time and price. Then if user uses show items button, he will go to the third page and see printed lines. However, I put a navigation bar with a back button on top of the third page, when I use back button, it crashes.
Here is my question: How can I fix it without changing updated price and time counter? With segue.identifier? If yes, how?

Here is what you have to do. Select the 2nd view controller, go to the edit from top and follow the path: edit -> embed in -> navigation controller.
This will automatically adds the navigation bars and back button to the 3rd controller.
Hope this helps .

Don't add your own UINavigationBar. Like Kutay Demireren said, you want to embed your first view controller in a UINavigationController, and then the segues will work by pushing each destination controller onto the navigation controller's stack.

Related

Updating view A that was behind view B after the back button in the navigation bar is pressed and view B is popped

I have two views A & B in order to go from A to B I'm just pushing the new view, in view B I preform some actions that update the badgeValue of a UIBarButton item of my navigation bar. (Like adding an item to the shopping cart)
When I go back to view A using the navigationBar Back button the UIBarButton item shows the previous badgeValue, instead of the updated one, I guess this happens because when you press the back button view B is popped revealing view A which is left as it was without any changes so, I need a way to tell view A to update itself after returning.
If instead of going back to view A I go back to the rootViewController the badgeValue is updated, I guess that when I go to the rootViewController the code in viewDidLoad is read and the badgeValue is updated.
Any ideas on how to update a view that was behind another that was just popped?
EDIT
#silicon_valley answer is correct but it won't work with MikeMTOL's library because it's buggy, for Swift users I'd recommend these extensions instead. -> link
You can add your code to update the badge value in viewWillAppear. That will make sure the value is updated every time the view will come back on the screen (and not just the first time it is created).

iOs 10 - SWIFT 3. Present modally and active controller when i tried to change my view controller

i have a problem about while change my view controllers. Here some steps what i do:
-> in my main view, i have a button to go second view. when I click the button it's okey can see second view.
-> in second view , same as first one I have a button to go back first view. when I click again can see the first view.
-> here is the problem when I came the first view second time , and click the button it's give me the following error message like:
'Application tried to present modally an active controller'.
As far as I can see, the problem could be connected with action you call on button press, you must ensure that you are always trying to present the SECOND viewcontroller from the FIRST one(not the FIRST again) and also ensure that when you come back from the SECOND one, you dismiss it(or pop it if you are using navigationController) but not simply present FIRST viewcontroller like you did before with the SECOND one.

How can I implement tab bar controller, navigation controller and table view controller?

I'm looking to create an application with 4 screens. The first screen will have a tab bar controller with two tabs. The second tab will go to 3rd screen. The 3rd screen will be a table view and when a cell is clicked, will push to the 4th screen and then I want to have a back button on 4th screen to navigate back to 3rd screen. The first screen will have a button that segues to the second screen and from the second screen there is a button that segues to 3rd screen which is the table view. The second screen should also have a back button to go back to first screen. How can I implement this via StoryBoard?
There are a lot of tutorials in the Apple developer site... but also on youtube... search for "Storyboard ios" you can find a step by step tutorial on how to manage the tableview controller and additional tableview to show details. I suggest also to look at http://www.raywenderlich.com/
What I understand so far from your question. You should first drag a Tab bar controller, that will by default have 2 tabs. Now on 1st tab or 1st view controller, place a button and make its push relation with the view controller you want. 2nd tab will open 3rd screen. Place a table view in it(view controller with 2nd tab or item). Make push relation from table view cell to the 4th screen(drag view controller and embed this view with navigation Controller). Embed all those views with navigation controller form where you want to turn back to previous view controller. Hope this is what you wanted.
I have figured out my issue. I wasn't using push to segue with my navigation controller and that was throwing everything off. Thanks for the help!

iOS navigation best practice for adding items

I cannot find the answer to this although I have implemented this rather a few times already, but maybe the wrong way.
Say I have an App for iOS, it has a main screen, which goes to a list, that list has a < back (to main) and an add button. Now when I click < back, I go back to main as that's the pop() from the stack. No issues so far.
Now when I click the add button, that is added to the stack as well; when I click back on that screen I go back to the list which is fine.
The problem is; when I save the new item, I want to go to the detail screen, but I don't actually want to have the add screen on the stack anymore while it will be there. I want the < back button for the detail item pointing to the list.
I know how to do this, but what is actually the best to implement this with the navigation stack?
Well for adding elements the best practice is to present an ModalViewController.
In this way it is not added to the stack.
Update
Let's take as examples simple apps that apple provide with iOS, Contacts app. When you want to add a new contact a VC is presented.
You'll need to implement "Done" or "Save" button that will dismiss the modalViewController and if you want to take the user into detail screen you could post a notification or other mechanism on dismissViewController method's completion block that will push the detail page from the list. But be careful on animations if you dismiss the modal VC animated and push the detail page animated you could get some unexpected behaviour. My proposal is to dismiss the Modal VC animated and push the detail page without animation.
You can override UINavigationController's viewControllers property after you pushed detail view controller. Just get current viewControllers property array, iterate and find the one you don't want to see and remove it. Remember to set viewControllers array again.
https://developer.apple.com/library/ios/documentation/Uikit/reference/UINavigationController_Class/index.html#//apple_ref/occ/instp/UINavigationController/viewControllers

How can I make one Tab Bar button refer to two views/controllers in iOS

I'm developing an iOS app just now with a Tab Bar navigation.
I have two screens which show the same information but in different formats (say, list and grid).
The two screens are different enough that they require separate controllers.
Users can toggle between the two views from a shared control bar button (toggle) at the top.
Scenario:
User presses the 'Places' button for the first time and it shows the places as a list.
They press 'grid' to see the same places displayed as a grid.
The user presses another tab bar button to navigate to a different screen.
When they press the "Places" button again, the app remembers their last viewed screen for places was the grid so the grid view is shown.
The user may then toggle back to list view. etc...
Can anybody recommend the best approach to achieving this?
One approach is to use one view controller that manages both views. That way, you don't have to bother with synchronizing data or subverting the normal function of UITabBarController -- there's just one controller. Also, don't try to overload the meaning of the tab for that controller. Instead, add a button to both views that tells the controller to switch to the other view. That'll be easier for your to build, and (more importantly) easier for the user to understand. It's not nice to make familiar controls do unfamiliar tricks.
If your view controllers are such that combining them into one would be complicated, then you can use two controllers and simply swap them in and out of the tab bar by modifying the tab bar controller's viewControllers array. You can still avoid having to sync data between them by having both controllers refer to the same data model.
I was trying to achieve this same thing, and it can actually be done with a basic setup of a TabBarController, NavigationControllers, ViewControllers, push segues, and unwinds.
TabBarController
|==> NavigationController --> PlacesController(grid view) --(push segue from nav bar)--> PlacesController(list view)
|==> NavigationController --> OtherController
|...
Make sure to have an unwind segue back from the list view controller to the grid view controller.
If you toggle between views then go to another tab (e.g. otherController) and come back, you'll return to the last view you were seeing because that's what is at the top of the stack of the NavigationController.

Resources