I'm quite new in swift programming but couldn't find an answer to the question how to change the title of the navigation bar when the user taps on it (see example app "ToDo" from Microsoft [former Wunderlist]). So first I choose the "Item A" out of a tableview and in the following screen the title stands in the navigation bar - so far so good. With a tap on this title "Item A" it should be possible to change the title (also for the previous tableview with the list of items by saving the changes in Realm). For now I just use the commands
title = selectedItem!.title
in my viewDidLoad in the upcoming view controller where .title equals the title from my "Item"-class
class Item: Object {
var parentCatergory = LinkingObjects(fromType: Category.self, property: "items")
#objc dynamic var title: String = ""
}
Any ideas? Or do I have to use a customized navigation bar, if so how?
Thanks in advance!
According to the UIViewController lifecycle, anything that you add to the method viewDidLoad() will be executed once when the view is loaded. If you want to change the title property on demand on a ViewController, is necessary invoke the navigation bar title property again from your tab action method. Check this post for more information: How to set the title of a Navigation Bar programmatically?
Related
I am trying to add the "Back button" to a couple of my view to a specific view that come before them.
View A has 2 buttons, to view B and C, I want B and C (And all following views) to have the "back button", but I don't want it to appear once I'm in view A.
In this example, I'll only show what I've done with view A and B.
I added the navigation controller between A and B, and as you can see, "Shows Navigation Bar" is ticked on. The segue between A and B is "present modally".
For some reason it doesn't show in my storyboard, but view A is a tabbed view connected to a tabbarController. What am I doing wrong ?
Your root view controller (first one, basically) needs to be a UINavigationController in order to have that navigation bar and back button work natively.
Also, presenting a UIViewController modally does not show a navigation bar (and subsequently the back button). Only when you push a UIViewController onto the navigation stack will the navigation bar and back button show.
Here is an example of pushing a view controller from another view controller that is within a UINavigationController:
func buttonPressed() {
let nextViewController = UIViewController()
self.navigationController?.pushViewController(nextViewController, animated: true)
// As long as your navigation bar is not set to hidden,
// doing this will push nextViewController onto the navigation stack
// and show the back button + navigation bar.
}
I know programmatically I can set a child view controller's title and it will updated its related tab bar item's title but how do I set that up in Interface Builder?
Interface Builder doesn't have an exposed property for a view controllers title but it does allow you to set properties in the "User Defined Runtime Attributes" section under Identity Inspector tab. Just set the key path to title, type to String, and Value to your desired title. Also check out Apple's documentation. Or check out this blog for a visual walkthrough: http://ios-blog.co.uk/tutorials/user-defined-runtime-attributes/
You can also have finer grain control by setting these different properties:
self.navigationItem.title = #"my title"; //sets navigation bar title.
self.tabBarItem.title = #"my title"; //sets tab bar title.
self.title = #"my title"; //sets both of these.
I've tried to build on a Cocoa app which uses storyboard and Swift in Xcode 6. However, when I tried to alter the title of window from within NSViewController, the following code doesn't work.
self.title = "changed label"
When I wrote the above code in viewDidLoad() function, the resultant app's title still remains window.
Also, the following code causes an error, since View Controller doesn't have such property as window.
self.window.title = "changed label"
So how can I change the title of window programmatically in Cocoa app which is built on storyboard?
There are 2 problems with your code:
viewDidLoad is called before the view is added to the window
NSViewController does not have a window property
To fix the first one, you could override viewDidAppear(). This method is called after the view has fully transitioned onto the screen. At that point it is already added to a window.
To get a reference to the window title, you can access a view controller's window via its view: self.view.window.title
Just add the following to your view controller subclass, and the window title should change:
override func viewDidAppear() {
super.viewDidAppear()
self.view.window?.title = "changed label"
}
This worked for me, currentDict is NSDictionary passed from previous viewController
var currentDict:NSDictionary?
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if let myString:String = currentDict?["title"] as? String {
self.title = myString
}
}
Sencha adds a nice Back button to the navigation bar. As shown below.
So my questions are:-
How can I dynamically add the Next button to the navigation bar?
How do I style the Next button to have a triangular edge, similar to what the Back button has?
You can change appearance of button to look alike forward button by setting ui : "forward" config for button.
You can add buttons to navigation bar with following way -
navigationBar : {
items:[
{
xtype:'button',
text:'Forward',
ui:'forward'
}
]
},
You can also add buttons dynamically to navigation bar from controller. First get instance of navigation view and then navigation bar. Create some buttons and add them to it.
var navBar = this.getMyNavView().getNavigationBar();
var button = Ext.create('Ext.Button', {
text: 'Button'
});
navBar.add(button);
update
Found this issue for ui : 'forward' on sencha forum. Have a look at it.
http://www.sencha.com/forum/showthread.php?253899-When-using-ui-forward-and-back-the-buttons-are-not-being-rendered-properly
In my OS X app, using Interface Builder, I have a window that looks like this:
I'd like to add a button to the right-hand side, to achieve this:
If this is possible, how can I do it?
It is not possible to do with Interface Builder, however you can get it done with little bit of coding :
NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton]; // Get the existing close button of the window. Check documentation for the other window buttons.
NSView *titleBarView = closeButton.superview; // Get the view that encloses that standard window buttons.
NSButton *myButton = …; // Create custom button to be added to the title bar.
myButton.frame = …; // Set the appropriate frame for your button. Use titleBarView.bounds to determine the bounding rect of the view that encloses the standard window buttons.
[titleBarView addSubview:myButton]; // Add the custom button to the title bar.
Swift 2.2 and Auto Layout, Create an "OK" button to the right of the title bar:
let myButton = NSButton()
myButton.title = "OK"
myButton.bezelStyle = .RoundedBezelStyle
let titleBarView = window!.standardWindowButton(.CloseButton)!.superview!
titleBarView.addSubview(myButton)
myButton.translatesAutoresizingMaskIntoConstraints = false
titleBarView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[myButton]-2-|", options: [], metrics: nil, views: ["myButton": myButton]))
titleBarView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-1-[myButton]-3-|", options: [], metrics: nil, views: ["myButton": myButton]))
With auto layout, you don't need to hard-code button's frame. And it is always on the right of the title bar even you resize the window.