ios


In a Tab Bar Controller, how to find out which View Controller in my Tab Bar called the current View Controller in Swift 2.x


I have a Tab Bar Controller with 3 Tab Bars.
Is a Main View Controller
Is a Summary View Controller
Is a Menu View Controller
The Summary View Controller can either be called from just the Tab Bar options at the bottom or it can also be called from a menu button in the Menu View Controller. When called from the Menu View Controller's button all I am doing is calling:
tabBarController?.selectedIndex = 2
Here’s my issue, I have a CLOSE button available in the Summary View Controller to go back to the calling View Controller. That is the effect I would like to create.
In other words, if the user clicks on the Tab Bar options at the bottom of the view to get to the Summary View Controller and then clicks the CLOSE button available in the Summary View Controller I would like to send the user back to the Main View Controller using selectedIndex = 1.
However, if the user clicks on the menu button available in the Menu View Controller to get to the Summary View Controller and then clicks the CLOSE button I would like to send the user back to the Menu View Controller.
Thus, how can I find out which View Controller in my Tab Bar called the Summary View Controller to be able to switch to the corresponding View Controller once they click CLOSE. Thanks
How about you use NSUserDefaults, create a custom Subclass of the UITabBarController, or just simply present a new UIViewController instance instead of switching tabs to get your desired output?
There are many ways to tackle this problem you're having.
NSUserDefaults: Before switching tabs save the current tabBarController's selectedIndex
For example:
let key = "SomeTabBarControllerSelectedIndexKey"
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(tabBarController!.selectedIndex, forKey: key)
// then set the new index
tabBarController?.selectedIndex = 2 // this is the new index assuming the previous index was 0 or 1
// then when **CLOSE** is clicked you just set the tabBarController's index to the one you set in the UserDefaults
if let savedIndex = defaults.integerForKey(key) {
tabBarController?.selectedIndex = savedIndex
}
UITabBarController Subclass: Create a subclass then use as you will
// first we create the new class
class SomeTabBarController: UITabBarController {
var previousIndex = 0
}
In your Storyboard/XIB subclass the UITabBarController to SomeTabBarController
(tabBarController as! SomeTabBarController).previousIndex = tabBarController!.selectedIndex
// then set the new index
tabBarController?.selectedIndex = 2 // this is the new index assuming the previous index was 0 or 1
// then when **CLOSE** is clicked you just set the tabBarController's index to the one you set in the the class
tabBarController?.selectedIndex = (tabBarController as! SomeTabBarController).previousIndex
presentViewController: Create a new instance of the ViewController you want to present all the time
self.presentViewController(SomeViewController())
// when you click the close button inside `SomeViewController` just call
dismissViewControllerAnimated(true, completion: nil)

Related Links

How do I change the pitch of recorded voice audio? I'm getting the same voice as I recorded
How to remove white spaces inside a string? [duplicate]
Can't export development iOS App after archiving it
WatchOS2 Event Complication, how to step to next event at endTime of prior event? (getTimelineEntriesForComplication)
Swift more complex array dictionary
Change google maps location button image [duplicate]
ios view transition is getting distorted with setNavigationBarHidden=YES
How to implementent videoGravity for CAMetalLayer?
Display two images in didFinishPickingImage - iOS Swift 2.2 [duplicate]
Set CGPath width to view width
Best practice to define enums? [closed]
Sending picked image to another view
Jail broken iOS: Communication between tweak and application
Download data from iCloud if app is not running
Move function void to boolean Swift
How to use the side buttons control the volume instead of ringing APP

Categories

HOME
validation
wpf
printing
module
android-wear
spi
android-bluetooth
google-analytics-api
apache-spark-sql
mobilefirst-adapters
copy-paste
avplayer
obiee
axure
union
oracle-agile-plm
rebus
qualtrics
afnetworking-2
prerender
orientdb-2.1
abaqus
windows-applications
amazon-cloudfront
nice-language
core-location
android-arrayadapter
azure-api-apps
detach
pymssql
data-science
xaf
zipline
rights-management
angular-xeditable
cookiestore
message-hub
php-5.5
ulimit
karnaugh-map
android-studio-2.1
integer-programming
spring-form
html-lists
g-wan
maven-versions-plugin
billing
easyhook
zbar
lcov
jcuda
manifoldcf
kie-workbench
wxhaskell
rsh
singly-linked-list
dms
jca
schematiq
js-of-ocaml
occam-pi
xelement
readline
python-dragonfly
uikeyboard
spl
monkeyrunner
user-registration
realm-list
boost-coroutine
simperium
mkpolyline
conditional-statements
laravel-validation
sendto
data-quality
wmv
acts-as-taggable-on
junit-runner
appstore-sandbox
libtorrent-rasterbar
google-chrome-frame
jmdns
cakephp-2.1
timestamp-with-timezone
asp.net-session
vim-fugitive
noindex
httpcontext.cache
soapheader
xmltextwriter
nstreecontroller
noaa
pinging
help-authoring
database-dump

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App