when button pressed or when view Disappear in swift - swift

I have 3 view controller.
First view controller has a button and second view controller has a button.
My third view controller has 2 buttons (button 1 and button 2) when the button of first view controller or second view controller pressed it shows the third view controller, I want to write a code that:
If first view controller button pressed or view disappear my third view controller button 1 hide else second view controller button pressed or view disappear my third view controller button 2 hide.
I tried many ways in swift but it doesn't work at all, can any help me with that please...

Use like this : creates a new instance of ViewController.
class ViewController1 : UIViewController
{
...
func goToSecondView() {
var viewController = ViewController2()
viewController. isFrom = 1
self.navigationController.pushViewController(viewController, animated: true)
}
}
class ViewController3 : UIViewController
{
var isFrom : Int?
override func viewDidLoad()
{
if isFrom == 1
{
button.hidden = true Or button.enable = false
}
else isFrom == 2
{
button2.hidden = true Or button2.enable = false
}
}
...
}

Related

How to do popViewController from another class?

My app has a TabBarController. Each tabBarItem relates to a ViewController embedded in a NavigationController.
When in first tabBarItem and selecting another tabBarItem I want to do some stuff before moving to the selected ViewController. Therefore I created a class for my tabBarController and made it UITabBarControllerDelegate.
What I want to do is present an alert with two buttons; button A cancels the move to the selected viewController and button B lets the move happen.
My problem is that when button B is pressed, I want to popToRootViewController. I gave the navigationController a storyboardID and tried to instantiate it like shown below.
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let alert = self.storyboard?.instantiateViewController(withIdentifier: "ActiveSessionWarningAlert") as? ActiveSessionWarningAlert {
alert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
alert.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal
let alertWasDismissed: (Bool) -> Void = { userWantsToMoveToSelectedViewController in
if userWantsToMoveToSelectedViewController {
if let navContr = self.storyboard?.instantiateViewController(withIdentifier: "firstNavContr") as? UINavigationController {
navContr.popToRootViewController(animated: true)
}
tabBarController.selectedViewController = viewController
}
}
alert.alertWasDismissed = alertWasDismissed
self.present(alert, animated: true, completion: nil)
}
return false
}
Everything works as expected, but the popToRootViewController doesn't seem to occur; when selecting the first tabBarItem again the same viewController that was 'active' when we left the item is still showing.
I checked so that the viewController I want to pop is actually in the navigation stack and that the navContr != nil.
What am I missing?
You don't say so, but I'm assuming that the alertWasDismissed closure you pass to your alert view controller gets invoked when the user dismisses the alert.
The problem with your closure is this bit:
if let navContr = self.storyboard?.instantiateViewController(withIdentifier: "firstNavContr") as? UINavigationController
Any time you call instantiateViewController(withIdentifier:), you are creating a brand new, never before seen instance of a view controller (a navigation controller in this case.) That navigation controller has nothing to do with the one that belongs to the current tab that you are trying to dismiss. It doesn't have anything in it's navigation stack other than the root view controller that is defined in the storyboard.
What you need to do is find the navigation controller of the current tab in your tab bar controller's tabBarController(_:shouldSelect:) method, and pass that navigation controller to your alertWasDismissed closure.
At the time the tabBarController(_:shouldSelect:) method is called, the tab bar controller's selectedViewController should contain the current view controller. Replace the line above with:
if let navContr = tabBarController.selectedViewController? as? UINavigationController {}
That should work.

Switch tab bar controller view controller after dismissing a modally presented view controller

In my project you can create a post from a modal view.
When the modal view is dismissed (user presses on save post) I want to switch the tab bar controller to the second tab (post feed screen).
This topic is similar to my problem. The only difference being this is presented from a modal view. I can't figure out how to implement it in my code (tab bar is nil)
Switch tab bar programmatically in Swift
I have added 3 images to make this issue clearer
code screenshot
console message
#objc func saveAction(sender: UIButton) {
print ("> save pressed")
print(presentingViewController?.tabBarController)
print(presentingViewController)
presentingViewController?.tabBarController?.selectedIndex = 1
dismiss(animated: true)
}
edit: sorry stack overflow doesn't allow me to add images yet
You can do this using delegate pattern. But if you prefer not to add a delegate for this, you can do as shown below;
You can switch the tabbar by changing the selectedIndex property of tabBarController
if let presenter = presentingViewController as? LibraryViewController {
presenter.tabBarController?.selectedIndex = 1
}
dismiss(animated: true)
If you are presenting the modal on navigation controller in tabbar, use:
if let tabBar = presentingViewController as? UITabBarController {
tabBar.selectedIndex = 1
}
dismiss(animated: true)

Display title on modally presented view in Swift 5

I am using Xcode 11.2.1, Swift 5, and have a view that is presented Modally. Via storyboard I added a navigation bar and have two navigation items, left, cancel, right, save. I am trying to get the title of the view to display. I have tried via viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "New " + costumeChoice
imageFromFileButton.alpha = 0.5
imageFromFileButton.isEnabled = false
costumeImageView.image = UIImage(named: "PauPlaceholder")
}
I have tried "self.navigationItem.title = "New " + costumeChoice" through viewWillAppear and through viewDidAppear, but no luck.
What am I missing?,
You have to present the NavigationController which embedded your ViewController, that way a navigationBar will be presented.
For example:
Assuming that your current ViewController is CurrentViewController
and viewController which you want to present is DestinationViewController
In order to present the destinationController you have to do this: -
// self ==> CurrentViewController
self.present(UINavigationController(rootViewController:DestinationViewController(), animated: true)

Segue removes tabBarItem.badgeValue

I have a custom TabBarControllerand on one of the Tab Bar Items I have a round red counter. The first Tab Tab Item presents a summary View Controller. The second Tab Bar Item presents a menu embedded in a Navigation Controller The Tab Bar Counterworks fine across the board, except when I segue back from the summary View Controller. Then the counter disappears. I have tried to load the counter in the customTabBarController:
import UIKit
class CustomTabBarController: UITabBarController {
override func viewDidAppear(_ animated: Bool) {
if tabBarCounter != 0
{
let vc = self.tabBarController?.viewControllers?.last
vc?.tabBarItem.badgeValue = "\(tabBarCounter)"
vc?.tabBarItem.badgeColor = UIColor.red
}
}
}
Screendump of storyboard. (The bottom ViewController is the one I´m having trouble with. Segue highlited)

Navigation and Tab bar disappear with Push Segue

I'm developing a swift app that has a Tab Bar Controller and one of the tabs has a Navigation Controller. The tab with a Navigation Controller has a Table View, and the cells in the Table View segue to a regular View Controller. The information appears in the destination view controller as desired, put when the destination view controller is presented, both the tab bar and the navigation bar disappear.
Here is my prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "entryCellToEntryView" {
if let destination = segue.destinationViewController as? EntryDetailViewController {
if let index = customTableView.indexPathForSelectedRow {
if let cell = customTableView.cellForRowAtIndexPath(index) as? EntryTableViewCell {
destination.entryToDisplay = cell.entry
} else {
print("prepareForSeque: Unable to cast cell as EntryTableViewCell")
}
} else {
print("prepareForSeque: Index cannot be established")
}
} else {
print("prepareForSeque: Destination cannot be downcast to EntryDetailViewController")
}
}
}
Here is a link to a photo of my storyboard:
http://grantbroadwater.com/storyboard.png
The navigation bar and tab bar disappears, because you are using model segue.
Change segue to push are show.
In model segue destination ViewController appears on the top of current viewController.
So destination view controller does not have any idea about your navigation stack or tab bar.
If you use push segue, or show segue now a days, then only it will be pushed into navigation stack.