Swift UNavigationItem button display menu labels - swift

I'm new in Swift and I would like to how to do this.
When I touch rightBarButtonItem button I would like the following to appear:
The Test and Test2 text should display in the same view controller.
If I don't touch rightBarButtonItem the Test and Test2 should not display. (Test and Test2 isHidden will be true.)
Is this possible or do I need another way?
I have been searching for a long time on the internet. But I have not been able to find anything. Please help or try to give some ideas of how to achieve this.

This is possible, you can add Test and Test2 in a view or stackView, then change the isHidden property of the view.
but as Matthew said, apple prefer to use tab bars.
set the view isHidden property to true in ViewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
customView.isHidden = true
}
#IBOutlet weak var customView: UIView!
#IBAction func rightBarButtonClick(_ sender: UIBarButtonItem) {
customView.isHidden = !customView.isHidden
}
you can also use SWReveal pod.
or you can create it by yourself in swift using this raywenderlich document

Related

UITapGesture not recognised on the simulator when tapping a UIImageView

I am trying to display a new image based on a user tapping the image view. I have added a UITapGestureRecognizer on top of the UIImageView which has been defined a "displayPhoto" and connected it as an outlet to the view controller.
#IBOutlet weak var displayPhoto: UIImageView!
#IBAction func changeImage(_ sender: UITapGestureRecognizer) {
displayPhoto.image = UIImage(named: "myimage")
}
The sent action is listed as follows:
sent actions
When I run the app and click on the image, nothing happens. I've even tried to make the first line of the IBAction function a fatal error but nothing happens. What am I missing?
Thanks in advance for any help.
The full view and connection
Tap Gesture Recognizer Window
Firstly you try; clean your project
command+option+shift+K
The first case cannot be empty
You should add a picture.
override func viewDidLoad() {
super.viewDidLoad()
displayPhoto.image = UIImage(named: "firsCase")
}

Adding views with IBAction to a NSStackView crashes application

I want to use the NSStackView to stack views above each other, I also want them to de able to expand so I can't use the NSCollectionView if i understood it correctly.
So, in storyboard, I've created a NSStackView(embedded in scroll view) in the main view controller and a view controller that I want to fill it with:
The button will fill the stack view with ten views:
#IBOutlet weak var stackView: NSStackView!
#IBAction func redrawStackView(_ sender: Any) {
for i in 0..<10 {
let stackViewItemVC = storyboard?.instantiateController(withIdentifier: "StackViewItemVC") as! StackViewItemViewController
stackViewItemVC.id = i
stackView.addArrangedSubview(stackViewItemVC.view)
}
}
And the ViewController on the right simply looks like this:
class StackViewItemViewController: NSViewController {
var id: Int = -1
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
#IBAction func buttonPressed(_ sender: Any) {
debugPrint("StackViewItemViewController" + id.description + "pressed")
}
Running this small application works fine, every time I press the button ten more stack view items appears. But, when I have the audacity to press one of the buttons to the right the application crashes:
Where am I going wrong?
I have tried to work around the IBAction to verify that this what breaks, and the application will not crash if I subclass the button and make a "buttonDelegate" protocol with a function being called from mouseUp.
I guess the problem is that the viewController objects, which you create in the loop, are released immediately.
Even though the view is attached to the stackView, it's viewController is destroyed.
You can fix this issue by keeping a reference to each viewController.
You can do this by creating a new variable
var itemViewControllers = [StackViewItemViewController]()
and then add each newly created viewController to it:
itemViewController.append(stackViewItemVC)

Enable user interaction of a button SWIFT

OK, so lets assume the button created below is greyed out and user interaction disabled.
How do I enable the button from anywhere else.
I know how to disable the button from inside the button function, sender.enabled = false, but I don't want to disable it from there.
I want to re-enable the user interaction from outside the button function.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func loadButton(sender: UIButton) {
//i wan to enable interaction of this button via override func and NSUserDefaults
}
}
In iOS and OS X development you have so-called outlets that are variables pointing to UI elements. You can read more about them here.
To declare an outlet, you prefix your variable with #IBOutlet like so:
#IBOutlet weak var button: UIButton!
and then you need to connect your outlet to the button in your xib file. That can be done in several ways, but if you have declared a variable like the above, you can:
go to your storyboard
in the document outline hold down the control button
while holding down the control button, you drag from your files owner or ViewController to the UIButton you'd like to connect to
select the outlet you'd like to connect to (button in this case)
As shown here
Once that is in place, you are free to use enable your button (and much more) in your code, for instance:
func viewDidLoad() {
super.viewDidLoad()
button.enabled = false
}
You can read more about IBOutlets here
And here is another way to connect outlets (this is how I prefer to connect outlets actually, you define and connect in one go)
Hope that helps

IBOutlet and IBAction in Swift

I connected a IBOutlet and IBAction to my button variable from Interface Builder to my View Controller. How do I add an action method to the button in Swift?
This code doesn't seem to work.
#IBOutlet var OK: UIButton!
#IBAction func OK(sender: UIButton){}
The Objective-C equivalent I found is:
#interface Controller
{
IBOutlet id textField; // links to TextField UI object
}
- (IBAction)doAction:(id)sender; // e.g. called when button pushed
When you attach a button to the viewController and create an action (IBAction) using ctrl-drag, you create a method that looks likes this in Swift (if it doesn't have arguments):
#IBAction func buttonAction() {}
In Objective-C the same thing will look like this:
- (IBAction)buttonAction {}
So that means that #IBAction func OK(sender: UIButton){} is an action method.
If you want to know about the sender argument, I would recommend this SO post.
Edit:
For what you want to do, I create an IBOutlet and an IBAction, that way I can change its attributes with the outlet variable, and have the action side of things with the IBAction, like what you show above:
#IBOutlet var OK: UIButton!
#IBAction func OK(sender: UIButton){}
For example, if I want to hide the button, I would put this code in the viewDidLoad
OK.hidden = true
The OK in that code is for the outlet variable, if I wanted to print "You pressed me" to the console when the button is pressed, I would use this code:
#IBAction func OK(sender: UIButton){
println("You pressed me")
}
Above I am using the action to print "You pressed me" to the console.
A few things to note:
When Swift 2.0 gets released println will get changed to print. Also with you action and outlet, I would suggest giving them differing names, to make it easier to differentiate the two, something like this:
#IBOutlet var okOutlet: UIButton!
#IBAction func okAction(sender: UIButton){}
Along with that, you should use camel case when naming variables, constants, functions, etc.
One way to do it, is control-drag from your button to your viewcontroller and choose action:
If you have connected your button's action, your code should work just fine.
Here are the steps you can follow-
For #IBOutlet
1.Declare Your Interface builder Element property right after class name
class SomeViewController: UIViewController{
#IBOutlet weak var aTextField : UITextField! ////Your Interface builder Element
2.Hook the IB Element From Storyboard.
For #IBAction
1.Write A method inside your class(say SomeViewController)
#IBAction func anAction(_sender : AnyObject){
}
2.Hook the method from Storyboard.
Hope it might helps.
You can simply add action from your storyboard. See the image.

UILabel Swift/Storyboard returns nil

I created a navigation controller with a view controller inside, containing a label. This label should be changed, when the view controller is loaded on tapping a button.
class ViewController: UIViewController {
#IBOutlet weak var btnEmergency: UIButton!
override func viewWillAppear(animated: Bool) {
self.btnEmergency.backgroundColor = UIColor.redColor();
}
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btnEmergencyTapped(sender: AnyObject) {
let emergencyViewController = storyboard?.instantiateViewControllerWithIdentifier("EmergencyViewController") as! EmergencyViewController
emergencyViewController.emergencyLabel?.text = "Notruf"
navigationController?.pushViewController(emergencyViewController, animated: true)
}
}
But the label text does not change when loaded. I am relatively new to SWIFT so I try to learn my ropes at the moment using optionals. So I guess the reason is, that the label is not yet instantiated when the view is being loaded on pressing the button. Therefore the default text is being shown and not the text I want to have appear on screen.
I am a little bit stuck at the moment, how to correctly change the label text, when this is an optional. So I guess I cannot expect the label to be there, when I call it. But how do I handle this correctly?
Instead of this, the correct way is to set a property of your emergencyViewController.
In your emergencyViewController viewDidLoad set your label text according to the property set previously.
Anything that you do between initialize of a viewController to viewDidLoad will not take effect.