I have a tap to begin button in my main view controller. It is linked to an IBAction that will detect when it is touched. However, I am wondering what would be the best way of hiding the button: deleted, moved off screen, or hidden completely - and what code should I put in my IBAction to do this?
Thanks
The easiest way is to just hide it. Create an outlet connection so you can reference the button as an class instance property, then set its hidden property to true from the tap handler:
#IBOutlet private weak var btnBegin: UIButton!
#IBAction func didTapBegin() {
...
btnBegin.hidden = true
}
Related
How do I programmatically hide or show the START SESSION button in a scene's view controller that navigates to the next scene?
create an outlet to your button in the view controller class:
#IBOutlet weak var startButton: UIButton!
and then in whichever function you want you can show hide it, I assume you want it hidden by default So in view did load you can do
override func viewDidLoad()
{
startButton.isHidden = true
}
and then show it somewhere else
func doSomethingAndShowButton()
{
// Do some other stuff
...
// Show the button
startButton.isHidden = false
}
#Doug Null To connect the #IBOutlet. First, follow what #AngrayDuck said. After that do as following:
Go to the storyboard.
Open "Connection inspector" on the right side. Can open through shortcut "Command + Option + 7".
Follow the steps highlighted in the below image.
Hope this helps.
I am having a problem when trying to disable button's user interaction of 1 view controller in another view controller.
I have searched similar questions here, but some seems outdated or does not work for me:
How to access an IBOutlet from another class.
My scenario is as follows:
class ViewControllerA() {
#IBOutlet weak var btnFirst: UIButton!
#IBOutlet weak var btnSecond: UIButton!
#IBOutlet weak var btnThird: UIButton!
override func viewDidLoad() {
var vcB = ViewControllerB()
vcB.closure = {
// Meet some condition, want to disable buttons of ViewControllerA here
}
}
}
class ViewControllerB() {
var closure: () -> Void = {}
// Do something with closure here
}
My problem is that i set the breakpoint in the closure and try to use directly IBOulet in closure to disable buttons like:
btnFirst.isUserInteractionEnabled = false
Or try to set a property of ViewControllerA in closure of ViewControllerB and use property observer, whenever this property changes, enable or disable buttons of ViewControllerA.
My problem is that, i can still click the buttons as if it's enable. Sorry, i cannot post the code, please help me!
Thanks
You can post notification from second view controller and add observer for that particular posted notification in first view controller.
In that observer method, you can do your stuff like disabling user interaction for first view controllers' button.
Since you did not post any relevant code, I can only guess what might have happened:
I assume that you are not accessing the btnFirst of the correct viewcontroller. In ViewControllerA.viewDidLoad you are creating an new Instance of ViewControllerB and set the closure. Are you also showing exactly this view controller's view? Or how will the user navigate to B? If you are using storyboard segues, those will create a new B instances and show its view. Now when you execute the closure in A, this will disable the button of the first B, not of the B that is displayed.
But this is still just a guess...
I want to have three VC's (A, B, C look at picture). And I want each VC to have three same buttons. And when user will press left button it will show A VC, when center button will be pressed - "B" VC will be shown and the same with the last one. Also I don't need view controllers to be created each button press, they have to save their state.
The same functionality has UITabBarController but I dont know how to implement it with buttons.
Can some one help me with my issue or show the right way to accomplish this?
Try this approach:
In your main view, add three overlapping container views.
Add buttons to the main view to switch between views (by bringing them to the front).
Configure the viewControllers however you want. Embed them in a NavigationController if desired.
Add #IBOutlets to the container views.
In the #IBAction for each button, bring the corresponding view to front`
class ViewController: UIViewController {
#IBOutlet weak var containerA: UIView!
#IBOutlet weak var containerB: UIView!
#IBOutlet weak var containerC: UIView!
#IBAction func pressA(sender: UIButton) {
view.bringSubviewToFront(containerA)
}
#IBAction func pressB(sender: UIButton) {
view.bringSubviewToFront(containerB)
}
#IBAction func pressC(sender: UIButton) {
view.bringSubviewToFront(containerC)
}
}
You can achieve it as follows:
Add three buttons on the bottom of the Parent Controller.
Add three Container View's and make their respective separate classes so that your code is modular. Make sure height and width of all the containers are the same and it covers the full view leaving your buttons on the bottom. (Container Views are very easy to manage using storyboards)
Now make three outlets for all the three container views and make three actions for all the respective three buttons.
On button press hide the two container views you dont want to show and unhide the one you want to see.
With this you can get Tab Bar Controller like functionality.
PS: You should not use these kind of approach and one should use tab bar controllers where required.
At the moment I am setting up my buttons for my keyboard with the following code:
func setupButtons() {
for subview in self.view.subviews {
if subview.isKindOfClass(UIButton) {
setupButton(subview as! UIButton)
}
}
}
func setupButton(btn: UIButton) {
btn.addTarget(self, action: #selector(KeyboardViewController.keyPressed(_:)), forControlEvents: .TouchUpInside)
}
But is there anyway I can skip this and all the target inside the layout builder so I can save a little bit of time looping through buttons on each keyboard view?
Sure, there are two ways to connect objects to Interface Builder, through outlets and actions.
class ViewController: UIViewController {
#IBOutlet weak var doStuffButton: UIButton!
#IBAction func doStuff(sender: UIButton) {
//do stuff
}
}
After adding that code look in interface builder and click on the view controller for this class.
Click on the connections inspector
Under outlets you will now see doStuffButton, however over the circle to the right of that, press control and click with your mouse, and drag it over to the button you want to connect it to and release. Setting outlets is NOT required for enabling actions. I just wanted to show this to you as well.
In the same pane you will also see received actions and the doStuff: method. Click and drag in the same way and release on the button. Select which type of event you want to process this action (normal is touch up inside).
Once you're all hooked up it should look like this:
There are many other variations of how to do this, but I thought this would get you a quick start.
If you're trying to ask how to do this without coding anything, just go into the assistant editor view of Xcode and Ctrl-drag from your button to the controllers class file. Then when the pop up displays, change outlet to action and give it a method name. This creates the IBAction method for you.
But in reality, the way you are doing it now with the for loop is far better. Especially if you have many buttons.
I am dealing with UISegmentControl in my project. I am able to create outlet & outlet collection of UISegmentControl with the help of drag & drop. But not able to create IBAction of Segment Control.
I think I am missing very small thing. Any Idea how should I proceed now?
For more clarity you can refer this screenshot.
Try this code in your controller :
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBAction func segmentedControl(sender: AnyObject) {
if segmentedControl.selectedSegmentIndex == 1 {
// Do something with the first button
}
else {
// Do something with the second button, etc...
}
}
Then drag it from your code to your segmentedControl
This is same as button actions the only difference is instead of selecting TouchUpInside or other button events you should use "ValueChanged"
UISegmentControll will respond to value changed action
You need to click assitant Navigator in the top corner right hand side to open Implementation file then drag segment control to implementation file after the #implementation directive that will pop the dialog screen.