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.
Related
Now I'm learning swift using Xcode, but I don't know where to drag and drop and why drag and drop "label" to somewhere
should I drop no.1? or no.2? or no.3? and why I should drop there?
If you drop it to 1, Xcode automatically generate an #IBOutlet for you. If you drop it to 2 or 3, then #IBAction. There's no other reason for that except that Xcode tries to be smart and help you organize code more nicely: properties with properties in the top of the class, methods – in the methods area. And you can also move declarations to another place later: except for code style matters, it doesn't matter, where exactly within your class you put declarations.
as you are trying to take an IBOutlet for a label , its nice to keep it on top of the class (where you mention 1).
normally Xcode gives suggestion you , if you drug on top side it will be #IBOutlet
or you on bottom like 2 or 3 it will be #IBAction like you already took a button action in your code .
Your Solution On Code:
import UIKit
class CodePresentViewController: UIViewController {
#IBOutlet weak var YourLabel: UILabel! // insert #IBOutlet type property here
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func tapBackButton(_ sender: UIButton) {
//Yout Button COde
}
}
If you want to own the label in your code, and then make some configuration to the label by code, use "1". It will give you a #IBOutlet label object in your code.
If you want to set the label's action, use "2" or "3".
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 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 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
}