Conditionally show settings page? - ios

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:)

Related

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
}
}
}

segue storyboard condition issue

I have this two controller liked by segue:
My problem is that:
when I tap on selected table view'cell in general I have to follow the segue and so go to other controller , but if a specific condition is true I have to show ad alert view and so I don't follow the segue and so don't go to those controller.
In this way when I tap on selected cell I go always on the other controller.
How do I solve this problem?
if you override this function segue wont continue if you return false, this gives you the oportunity to show a warning under certain conditions, after that you can performSegueWithIdentifier("segueidentifier", sender: self), and your good to go.
override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject?) -> Bool {
if identifier == "segueidentifier" {
//show warning and perform segue with this identifier on the accept button listener.
return false
}
}
return true
}
You can add a segue from one controller to another without specifying exact view which triggers the segue. Then just check your condition in didSelectRow method and call performSegue method if you need to show new view controller:
if condition {
performSegue(withIdentifier: "MySegueIdentifier", sender: nil)
}
else {
//show ad here
}
To add a segue between controllers just drag with right mouse button from one controller to another and pick "Show".
Then select segue and add a string identifier for it:
Make sure you use the same string in the code when you call performSegue method.

iOS: Is there a way to use conditional segue depending on which table view cell clicked

I am trying to create similar controls as the ones below the date time picker in alarm app. It appears these controls are tableview cells. I want to have several tableview cells, but when a user clicks on it, the cell's segue will take them to the View Controller of the cell clicked.
In other words, if the user clicks on the Alarm App's "Repeat" cell, the segue will take the user to the "Repeat" View Controller.
If the user clicks "Label", the cell's segue will take the user to the "Label" View Controller.
So, is it possible to have this type of conditional segue?
Any samples/links in Swift would be appreciated. Thanks in advance.
in your didSelectItemAtIndexPath call different segue identifier
if indexPath.row == 0 {
performSegueWithIdentifier("showPage1", sender: indexPath.row)
}
else if indexPath.row == 1 {
performSegueWithIdentifier("showPage2", sender: indexPath.row)
}
Then in your prepareForSegue, check the condition.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showPage1" {
// do something for page 1
}
else if segue.identifier == "showPage2" {
// do something for page 2
}
}
Your segue should drag from image below to destination controller

Remove tab from TabBarController with different users iOS Swift

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)

How to Segue to a specific view controller depending on the Segue identity, from a Collection view cell

Currently developing a small ios app in swift, I have populated a collection view cell with data from a .plist, each cell has a title and button, what i'm wanting is to segue to multiple view controllers in the storyboard depending on the segue identity once the button is pressed. For example if the segue has the id set as food, i want it to navigate to the view called food?
or if it is easier for the segue to pull the title from the cell then navigate to the view with the same title as the cell?
ill try and explain in code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "food"
{
//then navigate to the view controller called food
}
else if segue.identifier == "drink"{
{
//navigate to the view called drink
}
}
If food, drink or any other items have their own unique view in storyboard this is achievable.
You can assign a string to each button in UICollectionView and check which item in the collection was tapped and do a performSegueWithIdentifier: with button string.
performSegueWithIdentifier("toComments", sender: self)
Then prepareForSegue: method to pass data. Do another if for drink.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toFood" {
let ExchangeViewData = segue.destinationViewController as! FoodViewController
ExchangeViewData.foodMenuToShow = foodMenuID //This can be anything that you get your food items.
}
}
You can't do what you are asking for. A segue identifier is what determines which view controller gets invoked by the segue. If you want to go to a different view controller, invoke a segue with a different identifier. By the time you get to prepareForSegue it's too late. The segue to a specific view controller is already in progress.
You should explain what it is you're trying to do and we can help you solve your problem rather than trying to fight the APIs and do something that's neither possible nor appropriate. (Your question is an example of an "XY problem".)

Resources