Default UIView using Segmented Control? - ios

I'm using a UISegmentControl to switch between 2 UIViews, one to select a user and the second to show information about that user.
The issue I am having is the UIView that shows initially when navigating to this page is dependant on the placement in the storyboard hierarchy.
I would like the "selectionView" to always be the default screen that comes up when navigating to this page.
The segment tab itself is correct with selectedSegmentIndex = 0 and works as expected once I switch between them, but the only I've been able to correct this issue is to change the position of the views in the storyboard.
How would I be able to do this programmatically?
import UIKit
#IBOutlet weak var segmentController: UISegmentedControl!
#IBAction func segmentIndexChanged(sender: UISegmentedControl) {
switch segmentController.selectedSegmentIndex {
case 0: selectionView.hidden = false; infoView.hidden = true
case 1: selectionView.hidden = true; infoView.hidden = false
default: break
}
}
#IBOutlet weak var selectionView: UIView!
#IBOutlet weak var infoView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
segmentController.setTitle("Select User", forSegmentAtIndex: 0)
segmentController.setTitle("User Info", forSegmentAtIndex: 1)
segmentController.selectedSegmentIndex = 0
}

In viewDidLoad Add below code
override func viewDidload
{
......
selectionView.hidden = false
infoView.hidden = true
.......
}

Related

Switching between two strings after button is pressed

So I am working on a BlackJack game for iOS using Swift. I have two buttons, HIT and STAY. Here is where my problem lies: When I press the originally, it is player 1's turn. But after clicking the HIT button, I want the turn label to oscillate between player 2 and player 1. So after player 1 clicks HIT button, I want the turn label to read player 2 and then player 1. Here is what I got so far:
class ViewController: UIViewController {
var deck = PlayingCardDeck()
#IBOutlet weak var cardLabel: UILabel!
#IBOutlet weak var playerTurn: UILabel!
#IBOutlet weak var scorePlayerOne: UILabel!
#IBOutlet weak var scorePlayerTwo: UILabel!
#IBAction func btnHit(_ sender: Any) {
cardLabel.text = String(describing: deck.drawCard()!)
playerTurn.text = String(describing: "Player 2")
}
#IBAction func btnStay(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
deck.shuffle()
}
}
With the method I have, it mimics what I want to happen (switches to player 2)
You need a variable to track which player is currently playing and then change the label based on those variable value.
var isFirstPlayerTurn: Bool = true
#IBAction func btnHit(_ sender: Any) {
cardLabel.text = String(describing: deck.drawCard()!)
isFirstPlayerTurn = !isFirstPlayerTurn
playerTurn.text = isFirstPlayerTurn ? "Player 1" : "Player 2"
}
Create two labels, one for each player. Then hide player 2's label and flip it while hidden in viewDidLoad(). Once the turn changes, flip both labels while simultaneously showing and hiding the labels based who's turn it is.
To flip the label, use CGAffineTransform. Use the labels' alpha property to show and hide the labels.
func changeTurn(_ player: Int) {
UIView.animate(withDuration: 0.5) {
playerTurn1.transform = CGAffineTransform(scaleX: -playerTurn1.transform.a, y: playerTurn1.transform.d)
playerTurn2.transform = CGAffineTransform(scaleX: -playerTurn2.transform.a, y: playerTurn2.transform.d)
playerTurn1.alpha = player == 1 ? 1 : 0
playerTurn2.alpha = player == 2 ? 1 : 0
}
}
Usage:
#IBOutlet weak var playerTurn1: UILabel!
#IBOutlet weak var playerTurn2: UILabel!
private var turn = 1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
deck.shuffle()
playerTurn2.alpha = 0
playerTurn2.transform = CGAffineTransform(scaleX: -playerTurn2.transform.a, y: playerTurn2.transform.d)
}
#IBAction func btnHit(_ sender: Any) {
cardLabel.text = String(describing: deck.drawCard()!)
turn = turn == 1 ? 2 : 1
changeTurn(turn)
}
Make sure both labels are the same frames.

switch between views ios using UISegmentedControl without using alpha?

I have UISegmentedControl and I use it to switch between 2 UI views but I have problem with my views make the app to chatty with web APIs.each view will call its API to bring data for each view. I want to make the app parent view the load each view without user alpha of child
import UIKit
class UsersGroupsViewController: UIViewController {
#IBOutlet weak var usersView:UIView!
#IBOutlet weak var groupView:UIView!
#IBOutlet weak var segmentedControlViews: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let font = UIFont.systemFont(ofSize: 15)
segmentedControlViews.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
}
#IBAction func switchViews(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
usersView.alpha = 1
groupView.alpha = 0
} else {
usersView.alpha = 0
groupView.alpha = 1
}
}
}
instead of using alpha use isHidden
#IBOutlet weak var segmentedControlViews: UISegmentedControl!
#IBOutlet weak var usersView: UIView!
#IBOutlet weak var groupView: UIView!
#IBAction func indexChanged(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
usersView.isHidden = true
groupView.isHidden = false
case 1:
usersView.isHidden = false
groupView.isHidden = true
default:
break
}

Not woking UIView ishidden after change UILabel text value in swift

I want to change uilabel value and show uiview that has hide like a popup windows.
When I touch a button, that code print "setPopupView 0".
but doesn't show settingView.
and I touch a button again.
print "setPopupView 0" and show settingView.
so When "settingLabelValueUnit text" has changed not show settingView,
but when "settingLabelValueUnit text" has not changed show settingView.
(It means "settingLabelValue text" is same as input value)
I don't know why.
Anyone can help me?
Thank you
here is my swift code.
class ViewController: UIViewController {
#IBOutlet weak var workoutScrollView: UIScrollView!
#IBOutlet weak var settingView: UIView!
#IBOutlet weak var settingLabelValueUnit: UILabel!
#IBOutlet weak var imgSettingBGcal: UIImageView!
#IBOutlet weak var imgSettingBGdis: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.imgSettingBGcal.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(calSettingClick)))
self.imgSettingBGdis.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(disSettingClick)))
}
func calSettingClick(sender: UITapGestureRecognizer) {
setPopupView(0)
}
func disSettingClick(sender: UITapGestureRecognizer) {
setPopupView(1)
}
func showPopup (_ settype:Int) {
switch settype {
case 0:
print("setPopupView 0")
self.settingLabelValueUnit.text = "Set1"
self.workoutScrollView.isHidden = true
self.settingView.isHidden = false
case 1:
print("setPopupView 0")
self.settingLabelValueUnit.text = "Set2"
self.workoutScrollView.isHidden = true
self.settingView.isHidden = false
}
}
}

Sidemenu using swift - reference taken https://github.com/evnaz/ENSwiftSideMenu

I have created common sidemenu for all main view controller using reference https://github.com/evnaz/ENSwiftSideMenu
Now the issue is i have created sidemenu view controller from storyboard instead of using code itself,it will not show anything on side menu.
Ideally it has to show the page which design from story board.
Actually only TableViewController work with this example. i need to work with UIViewController.
Anyone have idea about this ?
Check out the latest version, I added precisely that functionality a few weeks ago: you can now use a UIViewController, no need to use a UITableViewController.
But other than that, I can't tell without more information why it doesn't show up.
I'm using it in several apps and it works ok.
I've got a UINavigationController, which uses a subclass of ENSideMenuNavigationController, and a UIViewController for the menu itself.
This is it, basically:
class MainNavigationController: ENSideMenuNavigationController, ENSideMenuDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var mainMenuViewController: MainMenuViewController = storyboard?.instantiateViewControllerWithIdentifier("MainMenuViewController") as! MainMenuViewController
mainMenuViewController.navController = self
sideMenu = ENSideMenu(sourceView: self.view, menuViewController: mainMenuViewController, menuPosition:.Right)
//sideMenu?.delegate = self //optional
sideMenu?.menuWidth = 240.0 // optional, default is 160
sideMenu?.bouncingEnabled = false
sideMenu?.animationDuration = 0.2
// make navigation bar showing over side menu
view.bringSubviewToFront(navigationBar)
}
// MARK: - ENSideMenu Delegate
func sideMenuWillOpen() {
println("sideMenuWillOpen")
}
func sideMenuWillClose() {
println("sideMenuWillClose")
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
super.didRotateFromInterfaceOrientation( fromInterfaceOrientation )
sideMenu?.updateFrame()
}
}
Then I have the menu view itself, also in the Storyboard, which is a UIViewController. Here is a fragment:
class ERAMainMenuViewController: UIViewController {
weak var navController: ERAMainNavigationController?
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var exitButton: UIButton!
#IBOutlet weak var headImage: UIImageView!
let kInset:CGFloat = 64.0
override func viewDidLoad() {
super.viewDidLoad()
// Customize apperance of table view
tableView.contentInset = UIEdgeInsetsMake(kInset, 0, 0, 0) //
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
tableView.backgroundColor = ERAssistantTheme.sideMenuItemBackgroundColor
tableView.scrollsToTop = false
// Preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = true
// tableView.selectRowAtIndexPath(NSIndexPath(forRow: selectedMenuItem, inSection: 0), animated: false, scrollPosition: .Middle)
}
}

Swift - Segmented control - Switch multiple views

Until now I still can't figure how to switch multiple views in one view controller. My storyboard is like this one.
Right now I want to embed two views inside my view controller.
My code for segmented control to switch two views in one view controller so far.
import UIKit
class PopularHistoryViewController: UIViewController {
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBAction func indexChanged(sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex
{
case 0:
NSLog("Popular selected")
//show popular view
case 1:
NSLog("History selected")
//show history view
default:
break;
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Another thing, If I put two views inside my controller, what is best practice to differentiate it?
If you want to do UI layout in Xcode for the two overlapping subviews, a better solution is to use two UIContainerViewController, and use the same way of setting the hidden property as suggested in the above answer.
You can use the isHidden property of the UIView to show/hide your required views.
First you have to link both views to IBOutlets through the Interface builder
#IBOutlet weak var historyView: UIView!
#IBOutlet weak var popularView: UIView!
#IBAction func indexChanged(_ sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0:
historyView.isHidden = true
popularView.isHidden = false
case 1:
historyView.isHidden = false
popularView.isHidden = true
default:
break;
}
}
Note: it was named hidden in Swift 1 and 2.
First of all create two outlets and connect hose to the views in your ViewController.
#IBOutlet weak var firstView: UIView!
#IBOutlet weak var secondView: UIView!
And Change the code like:
#IBAction func indexChanged(sender: UISegmentedControl)
{
switch segmentedControl.selectedSegmentIndex
{
case 0:
firstView.hidden = false
secondView.hidden = true
case 1:
firstView.hidden = true
secondView.hidden = false
default:
break;
}
}
If you don't want to create Outlets, assign the views individual tags (Say 101 and 102) and you can do it like:
#IBAction func indexChanged(sender: UISegmentedControl)
{
switch segmentedControl.selectedSegmentIndex
{
case 0:
self.view.viewWithTag(101)?.hidden = false
self.view.viewWithTag(102)?.hidden = true
case 1:
self.view.viewWithTag(101)?.hidden = true
self.view.viewWithTag(102)?.hidden = false
default:
break;
}
}
Add both views to the view controller in the story board and set one of them to be hidden = yes or alpha = 0. When your index changed function gets called set the current view on screen to hidden = yes/alpha of 0 and set the previously hidden view to hidden = no/alpha = 1. This should achieve what you want.
If it is a simple view, not a part of the screen, you can indeed use isHidden property of two subviews of your view controller view. But I don't like this approach because it's hard to understand latter what is going on with your nib when all of the subviews are in one pile.
I would add and remove those two views as child view controllers programmatically. It's the cleanest way there is, in my opinion.
But even if you decided to go with just views, don't put them directly on view controller's view. Use nibs, preferably with owner class. And in many cases consider adding and constraint them programmatically. It's more code, but also cleaner and conserves resources.
#IBAction func acSegmentAction(_ sender: Any) {
switch acSegmentedControl.selectedSegmentIndex {
case 0:
// print("addressview selected")
addressView.isHidden = false
contactProviderView.isHidden = true
case 1:
//print("contact provider selected")
addressView.isHidden = true
contactProviderView.isHidden = false
default:
break;
}
}
So what is written above does not work for me, so I figured out myself in Xcode 11 with Swift 5.
(view1 = historyView, view2 = popularView)
#IBOutlet weak var view1: UITableView!
#IBOutlet weak var view2: UITableView!
#IBAction func segmentedControlChanged(_ sender: Any) {
func showView1() {
view1.isHidden = false
view2.isHidden = true
}
func showView2() {
view1.isHidden = true
view2.isHidden = false
}
guard let segmentedControl = sender as?
UISegmentedControl else { return }
if segmentedControl.selectedSegmentIndex == 0 {
showView1()
}
else {
showView2()
}
}
Maybe this helps anyone.

Resources