H.Epstein
H.Epstein

Reputation: 731

Create tab bar item that triggers Alert view controller

I have an UITabBarController and I want to create an item that will trigger only an alert view controller.

I want the alert to present on top of the view controller from which the button was pressed.

This is my code right now but for some reason it doesn't work:

override func viewDidLoad() {
    super.viewDidLoad()
 self.tabBarController?.delegate = self
}

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

    if item.title == "Title"{
        let action = UIAlertAction(title: "OK", style: .default, handler: { (nil) in

            self.tabBarController?.selectedIndex = 0
        })
        let alert = UIAlertController(title: "Feature not active yet", message: "Will be added soon", preferredStyle: .alert)
        alert.addAction(action)
        present(alert, animated: true, completion: nil)

    }
}

it shows me the alert but will not move to index 0 in the OK action handler.

Upvotes: 2

Views: 2730

Answers (2)

Praveen Reddy
Praveen Reddy

Reputation: 81

@ Non-escaping And @Escaping Difference in swift 4

@Non-escaping

1.Non-escaping Is Default .

2.Pass the closure as Functions.

3.Function Before Returns @non-escaping Closure gets Called.

4.No Longer exists In the Memory.

5.Compiler knows the @non-escaping Closure Handle memory allocation. It will take care About memory.

6.Must and should Provide [Weak self] while calling function.Its safe.Because of @non-escaping closure gets called Before Function Returns.

@escaping

1 escaping Is not Default . Swift 1X ,2X @escaping is Default.

2.Pass the closure as Functions.

3.Function Returns After @escaping Closure gets Called.

4.@escaping Closure Memory exists while Execution.

5.Outside of the function @escaping closure also Executed.

6.Most Of the Developer Think Point No -3 in @escaping How to call @escaping closure after function Returns. Actually code called top to bottom think this top to bottom means like a .Sync. But In our Case works .Async.

If You Don't Know About .Sync And .Async.

.Sync means One By One its executed.

.Async. Randomely Executed. So Function Returns After @escaping closure
Executed.

Upvotes: 0

pkesaj
pkesaj

Reputation: 61

https://github.com/pkesaj/TabbarItemAlert

Here you are example how can it work.

EDIT:

You need to add a UITabbarControllerDelegate and in the viewWillAppear add:

self.tabbarController?.delegate = self

then in body of class:

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

    if tabBarController.selectedIndex == 2{
        tabBarController.selectedIndex = 0
        let alert = UIAlertController(title: "Do something", message: "With this", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "A thing", style: .default) { action in
        })
        self.present(alert, animated: true, completion: {
        })
    }
}

Upvotes: 2

Related Questions