Remove tab from TabBarController with different users iOS Swift - ios

As you seen in the picture above, there're one login screen, tab bar will appear after login successfully.
What I want to do is, let say there are two different user such as Admin and Staff, they have different login credentials, the Admin login will show all the tab at the bottom, however, the Staff login can just see the first tab. How could I do that? Any idea or code example to refer?

In login screen you can override prepareForSegue as shown below
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let destinationTabBar = segue.destinationViewController as? UITabBarController {
if (!isAdmin) {
destinationTabBar.viewControllers?.removeAtIndex(adminScreenIndex)
}
}
}
In the code above, you check if the user is admin in prepareForSegue method for tab bar controller. If the user is not admin, you remove needed screen (by adminScreenIndex index)

Related

Is it possible to transfer data retrieved from Firebase in the tab bar controller class from the tab bar controller to a view controller? Xcode, Swift

So, I want to prefetch some of the data needed in the view controllers associated with the tab bar controller as the user moves from the login page to the home page (tab bar controller exists between these two view controllers). I'm fetching the data in a custom TabBarController class and using the following code to send it (doesn't work):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is Profile { // Profile = one of the view controllers in the tab bar
let vc = segue.destination as? Profile
vc?.uid = self.userID
vc?.name = self.name
vc?.skills = self.skillSet
}
}
What is the best way to do this? Please note I can't prefetch the data before the tab bar controller as that is the login page. If I prefetch the profile page data on the home page (tab bar controller {home, search, profile}), how can I transfer it to another view controller (to profile) in the same tab bar?
What is the best way to do this?
I believe once you perform login call (Inside login screen) you will have userID and other data related to that user, so inside Login controller you have added code for making Tabbar controller as initial controller.
So based on that best thing is to store userData into tabbar controller.
So once you sore data into tabbar controller (from either login or Home Screen) you can fetch user data in any root controller (home, search, profile) which is inside tab-bar by below code.
struct User { var userID: String? }
class TabbarController: UITabBarController {
var userData: User
}
class HomeController: UIViewController {
var userData: User?
override func viewWillAppear(_ animated: Bool) {
if let tabBar = self.tabBarController as? TabbarController {
self.userData = tabBar.userData
}
}
}
There are multiple ways you can achieve this.
Using SuperViewController,
Using SingleTone class if you want to access in other than TabbarViewVontroller subclass.
Saving in a common location like in constants if data is small.
Prefetching data in one VC e.g Home and then passing to the required VC on click.

go back to a certain viewController

my Storyboard looks like:
User can go directly from the 1st screen to the 4th. The fourth screen contains tableView, with XIB cell design. I want user to be able to tap on a cell and get to the 3rd screen and send some data with that. I know this should be done in didSelectRowAt. But is it even possible?
Yes it is possible.
Create a segue. In your storyboard, ctrl-drag from the first button on top of your 4th screen to anywhere on your second screen. You should see a new segue created in your storyboard.
Give the segue an id. Click on the newly created segue, in the right panel, set the indentifier attribute under the Indentity Inspector tab.
Perform the segue. In your didSelectRowAt, add the following line:
self.performSegue(withIdentifier: "THE_ID_YOU_ASSIGNED_IN_STEP_2")
Send data to the destination segue. In your screen 4 view controller, add the following function:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "THE_ID_YOU_ASSIGNED_IN_STEP_2" {
if let destinationVC = segue.destination as? YOUR_SCREEN_3_VC {
// Send data to your VC, such as assigning values
}
}
}

Conditionally show settings page?

I want to check if the user is a moderator before displaying a certain page in my settings. Here's how my storyboard is laid out:
When the user taps the "Invite users to pack" I want to check if a certain variable is true before pushing the view.
The problem is that if I hook up the cell to the UIViewController to its right then it automatically transitions when the cell is pressed. How would I implement some sort of code like?
if (isMod == true) {
// Show page
} else {
print("ACCESS DENIED")
}
If you're using segue, check it in override func prepare(for segue: UIStoryboardSegue, sender: Any?) in viewClass with tableView. Or you're using code to push (to navigationController), you can check before .pushViewController(viewController:, animated:)

NavBar in home screen not appearing after sign in

I'm making an app that requires login, so if you are not logged in, it redirects to the login screen, and after log in, it doesn't display the whole "Home Screen" Completely. It is missing the nav bar. If you close the app and open it again, the navBar is there.
Do you guys know why? Any help is appreciate it.
I'm using 2 different navigation controllers. 1 for the login and sign up, another for the home screen and the rest of the app.
If I'm not logged in, I'm redirected to the login screen with:
override func viewWillAppear(animated: Bool) {
if (PFUser.currentUser() == nil){
performSegueWithIdentifier("notSignedIn", sender: self)
}
}
and it goes to the login screen with no issues. After logged in, I tried this two different method and both do not display the navigation bar after redirected to home screen:
Method 1
if (user != nil) {
let homeScreen = self.storyboard!.instantiateViewControllerWithIdentifier("homeScreen") as! TimelineTableViewController
self.navigationController!.pushViewController(homeScreen, animated: true)
}
Method 2
After login I redirect back to the screen that lets you login or sign up with:
self.navigationController?.popViewControllerAnimated(true)
and once you are in the I use the same with a siege to go back to Home Screen, but either way it doesn't display the navigation bar in the Home screen.
Have you used:
navigationController?.navigationBarHidden = Bool
If it's not appearing in your VC screen use:
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBarHidden = false
}

How to show TabbarController from a button click segue?

I have a tabbarcontroller and it has 3 tab like Home,Cam and Profile. It is working perfectly. Also i want to show profile view from a different profile button. So if i clicked this button it shows profile view but tabbar is disappearing but i dont want this.
Actually system like an instagram clone. In instagram if you click a username then you can see his/her profile view and tabbar not disappearing. Also you can go profile view with profile tab. This not working in my project.
I can easily going to the profile view from TabBarVC. There is no problem in here. But in table view controller. I have custom cell in table view and there is a button in this cell. I wanna go to profile view when i click this button. It works but when i clicked this button, tabbar in the profile view is disappearing.
func profileSelected(index : Int) {
selectedUser = objects[index].valueForKey("user") as PFUser
performSegueWithIdentifier("profile", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.destinationViewController is ProfileVC {
var profileVC = segue.destinationViewController as ProfileVC
profileVC.user = selectedUser
}
}
How can i fix this?
You need to provide coding examples. Your description is not enough for anyone to be able to assist you.
If you don't want to share any of your code:
Check out public projects on GitHub or look at CocoaControls to see if they have what you are looking for.

Resources