I can't figure out how to set the correct button to be unhidden using swift - ios

I'm trying to create a quiz app for a project, I'm having a problem when I try to set another button to be revealed when you select the correct answer. ex.
1+2=?
2
3
5
9
When they select 3, I want the continue button to appear to go to the next page.
#IBAction func Seaturtle(sender: UIButton) {
if sender.tag == 2 {
7.hidden = false
}
}

Go into your Storyboard and find the Continue button you wish to hide/unhide. Use Xcode's split screen view to have both your Storyboard and the associated code file open. Select the button, then Ctrl+Drag from the button into the code, creating an IBOutlet instead of an IBAction. Say you give it the name continueButton. Then, when you want to hide/unhide just call
continueButton.hidden = true
or
continueButton.hidden = false

Related

How can I disable some of the tabs of the moreNavigationController of my UITabBarController?

I'm using XCode 13.0, Swift 5.5 and Storyboard. This is a mobile app for iPhone with iOS 15.
I have altogether 7 tabs, all of them with icons. I have 4 tabs and a "More" tab in the tab bar of my app.
3 additional tabs show up after the user clicks on the "More" tab. I'd like the first 2 of these additional tabs to be disabled. They need to be grey coloured and when the user clicks on any of these 2, I'd like to app to do nothing (not to show any page, not to navigate anywhere). I'm using the original UITabBarController and moreNavigationController defined by Apple, I didn't subclass any of them.
On my storyboard I added 2 UIViewControllers to these 2 disabled tabs, but I set them disabled this way:
This didn't work at all. When the user clicks on the 2 disabled tabs, they show 2 empty ViewControllers. I'd expect them not to show anything.
I also tried accessing the moreNavigationController's tabs from the page my UITabBarController first opens. I tried setting these 2 tabs disabled programmatically. However I didn't manage to access these 2 tabs, I only manage to access the main tabs that show up in the tab bar (I don't need to access these). How can I disable and colour grey the tabs that show up after clicking on the More tab?
let moreControllerItems = tabBarController?.moreNavigationController.toolbarItems
if let tabArray = moreControllerItems {
let tabBarItem1 = tabArray[0]
let tabBarItem2 = tabArray[1]
tabBarItem1.isEnabled = false
tabBarItem2.isEnabled = false
}`
This code doesn't work, the moreControllerItems variable is nil. The tabBarController variable isn't nil, I can access my UITabBarController from here, but it doesn't help me much.
I googled this issue lots of different ways but I couldn't find the solution. Any help would be appreciated. I really need to solve this, please write a comment if you have any idea how to solve this. Thank you!
You can disable tabBar items from TabBarController.
class TabVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
func disableTabBar(itemNo: Int) {
if let items = tabBar.items, itemNo < items.count {
items[itemNo].isEnabled = false
}
}
And you can access this function from
any child Viewcontroller attached to TabBarController
class MoreVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let tabBarVC = tabBarController as? TabVC {
tabBarVC.disableTabBar(itemNo: 1)
}
}
In short: You cannot disable it. If you want to disable the item in the More Viewcontroller, you have to use a custom Viewcontroller in the fifth Tab
I am explaining here why we cannot do it and what limitations we have.
Apple will show the More tab if items are more than five in the UITabBarController. And more tab holds the UINavigationController which is attached to a kind of UIMoreListController. you can check by
print moreNavigationController.viewControllers[0]
UIMoreListController is not accessible. In your case, the remaining three items are listed in the UIMoreListController. Although we can still access the Viewcontroller's tabBarItem and we can disable it. You can check by clicking the edit option.
But still, users can click on it as long as it is in the UIMoreListController. Because we are not interacting with the tabBar instead we are interacting with the items on the list. Users can't click once you move the item to any of the first four positions using the edit option in the MoreViewcontroller.

How to enable/disable navigation bar buttons [XCode] (swift)

I was wondering how I would be able to go about changing the status of buttons. I want to make it if one of two text fields has text in them then the button will become useable. I have currently turned off the button from the storyboard. The code I have to check if there is text inside of the text fields is as follows:
Disclaimer:
The code to check if the text field has any text in it works perfectly fine.
#IBAction func textFeildEditingChanged(_ textField: UITextField) {
if FirstName.hasText == true {
self.navigationItem.rightBarButtonItem?.isEnabled = true
print("First name isn't empty")
}
}
The current code that I have in there to set the button to enabled and disable doesn't work however the code to test if the text field has content does work. I just need to figure out how to disable and enable the button of a navigation item.
code that doesn't work is below:
self.navigationItem.rightBarButtonItem?.isEnabled = true
Any help would be greatly appreciated.
Edit: I am using a navigation controller, don't know if that's important or not.
If you are using the storyboard, just connect the outlet then disable it.
#IBOutlet var navigationItemButton: UIBarButtonItem!
then
navigationItemButton.isEnabled = FirstName.hasText

Pressing one UIButton triggers another UIButton: I don't want this to happen

I am new to swift and xcode and am having a little trouble with creating this simple timer app with laps. When I press on the 'lap' button, the 'reset button is also triggered. Can anyone please help me with this? The buttons are right next to each other but I don't think this has anything to do with it. Additionally, I also commented out the entire body of the lap function and the same thing happened.
#IBAction func Reset(_ sender: Any)
{
min = 0
sec = 0
mil = 0
timer.invalidate()
createLabel()
var i = 0
while i < list.count
{
list[i].removeFromSuperview()
i = i + 1
}
running = false
}
#IBAction func Lap(_ sender: UIButton)
{
let word = UILabel()
list.append(word)
word.font = UIFont.systemFont(ofSize: 50)
word.text = make()
word.sizeToFit()
word.frame.origin = CGPoint(x:187.5,y:700)
self.Scroll.addSubview(word)
word.center = CGPoint(x:187,y:last)
last = last + 50
Scroll.contentSize.height = Scroll.contentSize.height + 50
}
It sounds like you wired up two #IBActions to your Lap button. Control-click on your Lap button in the Storyboard and delete the connection to Reset() by clicking on the x next to the action.
I'm guessing you copied the Reset button to make your Lap button. If the #IBAction is connected when you copy a button, you get that connection as well with the new button. So if you add a connection to the new button, it doesn't replace the previous action but instead becomes a second action.
I think you might have hooked up both buttons with same IBAction function. Make a print statement inside both IBAction functions and see what happens. Or, delete both IBAction functions (and referencing outlets from the storyboard) and re-connect. Also, check in the connection inspector (top right in storyboard) to see if you have any dead outlets.
Start debugging and you'll find the solution. Start from the first point. Like adding buttons in the storyboard and then connecting them to View Controller. I think you have connected one button to multiple actions.

Swift Radio Buttons - CheckBoxes - Swift 3

I work on Swift 3, and I'm trying to make checkboxes that behave like radio buttons. I explain myself : I have 8 buttons ; each has an "unchecked" image and a "checked" image. I want only 1 button to be selected (so, showing ITS "checked" image) at the time. The principle of radio buttons.
So I tried using the #IBAction with each button, I've this :
#IBAction func tona1_changed(_ sender: Any) {
if test_tona1.isSelected == true
{
test_tona1.setBackgroundImage(UIImage(named: "0b_unchecked"), for: UIControlState.normal)
test_tona1.isSelected = false
}
else
{
test_tona1.setBackgroundImage(UIImage(named: "0b_checked"), for: UIControlState.normal)
test_tona1.isSelected = true
}}
It actually make the image switch between "uncheck" and "check" but I don't know at all how to make it interact with the other buttons.
I tried using an array with the buttons's tags inside but I didn't have something working. I also tried to make a class but I don't know how it can work.
Thanks!
You can achieve that in several ways. One of the most trivial would be to:
Have a datasource. Most of the time an array.
Create an UIButton for each item in your datasource, and insert each button into another array. Each button would have a tag corresponding at the
index of the array. Also set different images for selected state and normal.
Add an action for each button, with the target function being something like:
func buttonPressed(sender:UIButton) {
for button in buttons {
button.isSelected = false
// deselect your model datasource[button.tag]
}
sender.isSelected = true
// select your model datasource[button.tag]
}
I leave the abstraction / improvements / safety to you. The key point is just to use a collection of buttons, iterate through them, and select / deselect them accordingly, and not using a big if/else checking for the button tag everytime.

Disable user interaction on tabBar

I am displaying an image right after the app didFinishLaunchingWithOptions, the app consists in a tab bar, and in the first view i have some buttons.
The user can only continue to use the app after he press the button in that first image, the problem is, some users can interact with the tab bar, and the buttons in the first view even with the image above all.
How i can completely disable the user interaction on those buttons and in the tabBar, and enable then only when the button is pressed and the image disapear?
To disable:
UITabBarController.tabBar.userInteractionEnabled = NO;
To enable:
UITabBarController.tabBar.userInteractionEnabled = YES;
if let items = self.tabBarController?.tabBar.items {
for i in 0 ..< items.count {
let itemToDisable = items[i]
itemToDisable.isEnabled = false
}
}
somehow i didn't fully understand what you described,but have you tried disabling your buttons using following code?
button1.Enabled=false;
also you can disable images click function using
image1.Enabled=false;

Resources