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 to get last frame from video?
Share content on an iOS native app via messages or email with long enough trial
UIPaveViewController with page indicator overlaying content
UITableView in Storyboard hidden first cell when it back it will OK ? why?
iOS: ActionSheetStringPicker successAction: wrong selection
Swift calling delegate from didSet
iOS 8 and segues throwing unexpected exception
<TextInput keyboardType=“numeric”/> is not working in react native
UITableViewController scrollViewDidScroll: not called during cell removal
Using NSURLProtocol with NSURLSession
Uploading file to Amazon S3 with KMS key in xcode
UIlabel multiline in UITableView
unable to set custom font in NSMutableAttributedString
How to make different search field for every sections in UITableview?
Im having trouble with using a custom navigation bar in SWIFT
Error playing embedded youtube video iOS 9

Categories

HOME
math
bootstrap-4
apt-get
apache-pig
path
runtime-error
open-source
sweetalert
apple-mail
r-caret
player-swift
struts2-jquery
sql-server-express
google-weather-api
frequency
yii2-advanced-app
oracle-agile-plm
facebook-opengraph
export-to-excel
jsqmessagesviewcontroller
slowcheetah
teamcity-9.0
cadvisor
naturallyspeaking
rets
expression-trees
concurrenthashmap
bluebird
petrel
html-select
ose
subtotal
pymssql
dwm
extraction
apartment-gem
spring-saml
amazon-elasticsearch
essbase
consistency
btle
program-slicing
kendo-chart
federation
database-cleaner
tpl-dataflow
unityscript
sshd
javascript-security
haste
angular-directive
tastypie
mysql-error-1050
ifc
ipmi
atmosphere.js
ubuntu-15.04
post-increment
sizeof
agents-jade
activity-diagram
ng-annotate
visual-build-professional
pager
target-platform
jquery-load
xlform
illegalstateexception
java-gstreamer
sqlj
multiscreen
bonfire
android-print-framework
smarty2
newsql
modern-ui
zeroclipboard
keyboard-navigation
mouseenter
wmv
grunt-contrib-copy
windward
requiredfieldvalidator
modalpopupextender
modx-evolution
location-client
hresult
jflow
tempo
shebang
wpf-4.0
teamcity-7.0
jsr223
replay
iequalitycomparer

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