switch between views ios using UISegmentedControl without using alpha? - ios

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
}

Related

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

How do i get the Id of text view with radio Buttons clicked

For the radioButtons I have used Class in the button and for the indicator to buttons i have used the labels.
These are My TextView
#IBOutlet weak var TextViewEveryNewMessage: UILabel!
#IBOutlet weak var TextViewWeekly: UILabel!
#IBOutlet weak var TextViewDaily: UILabel!
#IBOutlet weak var TextViewMonthly: UILabel!
These are my Radio Buttons
#IBOutlet weak var RadioBtnMonthly: SSRadioButton!
#IBOutlet weak var RadioBtnWeekly: SSRadioButton!
#IBOutlet weak var RadioBtnDefaultChecked: SSRadioButton!
#IBOutlet weak var RadioBtnDaily: SSRadioButton!
As soon as i press the ok button i need to save the checkedRadio Button text somewhere and i have done it like this.
#IBAction func OkButton(sender: UIButton) {
SwiftSpinner.show(kLoadingText)
if RadioBtnDefaultChecked.selected{
preferencesConditions.notificationFrequency = self.TextViewEveryNewMessage.text
}else if RadioBtnDaily.selected{
preferencesConditions.notificationFrequency = self.TextViewDaily.text
}else if RadioBtnWeekly.selected{
preferencesConditions.notificationFrequency =
self.TextViewWeekly.text
}else{
preferencesConditions.notificationFrequency = self.TextViewMonthly.text
}
Is this correct way i am doing.Or there are any other approach. Please suggest me.
Instead of assigning a value for preferencesConditions.notificationFrequency in OkButton(sender: UIButton), you should do it in each of the SSRadioButton buttons and that's by calling SSRadioButtonControllerDelegate - didSelectButton:
func didSelectButton(aButton: UIButton?) {
print(aButton)
if aButton === RadioBtnDefaultChecked {
preferencesConditions.notificationFrequency = self.TextViewEveryNewMessage.text
}else if aButton === RadioBtnDaily {
preferencesConditions.notificationFrequency = self.TextViewDaily.text
}else if aButton === RadioBtnWeekly {
preferencesConditions.notificationFrequency =
self.TextViewWeekly.text
}else{
preferencesConditions.notificationFrequency = self.TextViewMonthly.text
}
}
Don't forget to conform the delegate to the viewController:
var radioButtonController: SSRadioButtonsController?
override func viewDidLoad() {
radioButtonController = SSRadioButtonsController(buttons: RadioBtnDefaultChecked, RadioBtnDaily, RadioBtnWeekly)
radioButtonController!.delegate = self
radioButtonController!.shouldLetDeSelect = true
}
The IBAction should be like:
#IBAction func OkButton(sender: UIButton) {
SwiftSpinner.show(kLoadingText)
}
For more information, check SSRadioButtonsController.
Hope this helped.

Default UIView using Segmented Control?

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

UIActivityIndicator shows for a split second

I am trying to show an spinning activity indicator for my app when the user taps on a button before they are presented with the next view - it kind of works, I see it for a split second
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewWillAppear(animated: Bool) {
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Refactor your code. You don't need to stop the activity indicator in viewWillAppear. You can hide it in viewDidLoad. Here's the code:
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
myActivityIndicator.hidden = true
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Make sure that the IBOutlet for the activity indicator and IBAction for getting events are correctly connected in Interface Builder.
I changed it and it doesn't work, until I go back in the navigation
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
viewDidAppear is probably a more appropriate place to stop the animation and hide the spinner.

Array of UIButtons or method UIButton(named: "\(string)")

Is it possible to make an array of UIButtons?
let buttonArray: [UIButton] = [UIButton(Button1)!, UIButton(Button2)!, UIButton(Button3)!]
To reference later as
buttonArray[0].setImage(UIImage, forState: UIControlState.Normal)
Or somehow set a
var button = UIButton.whoseName = "Specific String"
button.setImage(UIImage, forState: UIControlState.Normal)
VC1:
import UIKit
class VC1: UIViewController {
#IBOutlet weak var Card1: UIButton!
#IBOutlet weak var Card2: UIButton!
#IBOutlet weak var Card3: UIButton!
let Cards: [UIImage] = [UIImage(named: "Default")!, UIImage(named: "2s")!,UIImage(named: "2h")!,UIImage(named: "2c")!,UIImage(named: "2d")!,]
// var CardCaller: Array<UIButton> = [Card1, Card2, Card3]
let CardCallers: [UIButton] = [Card1, Card2, Card3] //ERROR: 'VC1.Type' does not have a member named 'Card1'
var caller = ""
var Index2 = Int()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let KVC = segue.destinationViewController as VC2
KVC.source = segue.identifier!
}
#IBAction func unwind(unwindSegue: UIStoryboardSegue){
//Card1.setImage(Cards[Index2], forState: UIControlState.Normal)
//let button = UIButton(named: "Card1")!
if caller == "Card1" {Card1.setImage(Cards[Index2], forState: UIControlState.Normal)} else if caller == "Card2" {Card2.setImage(Cards[Index2], forState: UIControlState.Normal)} else if caller == "Card3" {Card3.setImage(Cards[Index2], forState: UIControlState.Normal)}
}
#IBAction func text(sender: AnyObject) {
println(caller)
println(Index2)
}
}
The IBOutlets are wired properly to Storyboard buttons; I created them with control/drag to the view controller.
This shouldn't be a problem, but it may have something to do with when you're accessing the buttons. Try creating the array globally and adding the buttons to it once they've loaded:
class VC1: UIViewController {
#IBOutlet weak var Card1: UIButton!
#IBOutlet weak var Card2: UIButton!
#IBOutlet weak var Card3: UIButton!
var CardCallers: [UIButton] = [UIButton]() // Empty UIButton array
override func viewDidLoad() {
self.CardCallers = [self.Card1, self.Card2, self.Card3] // Buttons have now loaded in the view
}
}
You can.
Two ways to do it:
var buttonsArray: Array<UIButton> = [button1, button2]
Or
#IBOutlet var buttonsArray: Array<UIButton> = []
For the later one, it can be filled from the storyboard as an IBOutletCollection
Please note that views can be assigned to an IBOutlet and an IBOutletCollection at the same time.

Resources