I've looked through a few online tutorials, but nothing is working.
That's the code of my viewController:
import UIKit
class ViewController: UINavigationController {
let textView = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// tried this
self.navigationItem.title = "AAA"
// and this
self.title = "AAA"
// and finally this
self.parent?.title = "AAA"
}
}
I don't understand why this isn't working (I haven't used a navigation bar before)
I didn't change anything in the main.storyboard.
Thanks for your answers.
First of all in your storyboard select your view controller and then
Editor -> Embed In -> Navigation Controller
then in your ViewController class add
self.title = "AAA"
in your viewDidLoad method and your code will look like:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "AAA"
}
}
You need to replace UINavigationController with UIViewController
Select ViewController from the storyboard.
Go to the Editor and Embed with Navigation Controller
1) Select Navigation Item and set title from the storyboard.
2) By Programmatically
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Your Title"
}
}
Related
I use navigation bar with tabbarcontroller. When i push one of my tabs my navigationbar right items are hiding automatically.
How i can move my items to childs controllers?
You can create base view controller and inherit your children classes from base view controller then call super.viewDidLoad()
1- Base controller
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myButton = UIBarButtonItem(title: "LogOut", style: .done, target: self, action: #selector(self.logoutTapped(_:)))
self.navigationItem.rightBarButtonItem = myButton
}
#objc func logoutTapped(_ sender: UIBarButtonItem) {
print("Logout clicked :) ")
}
}
2- VC one
class ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
3- VC two
class ViewController2: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
Result
I'm trying to change the title in Product but somehow the navigationItem is different. How come the navigationItem in Container is different compared to the one in Product?
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let controller = Container()
let navigation = UINavigationController(rootViewController: controller)
navigationController?.pushViewController(navigation, animated: true)
}
}
class Container: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(navigationItem)
navigationItem.title = "test"
let controller = Product()
controller.didMove(toParentViewController: self)
self.addChildViewController(controller)
view.addSubview(controller.view)
}
}
class Product: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(navigationItem)
navigationItem.title = "" // Doesn't remove the title
}
}
I'm just reading the documentation for navigationItem, and it says this:
This is a unique instance of UINavigationItem created to represent the view controller when it is pushed onto a navigation controller.
However, in your case, the embedded VC is not a direct child of a navigation controller.
So, I tried the following code and it worked. The key part is I overrode navigationItem to return the parent's navigation item if there is a parent view controller.
override var navigationItem: UINavigationItem {
if let parentItem = parent?.navigationItem {
return parentItem
} else {
return super.navigationItem
}
}
I can't seem to find the proper code to remove the title(s) on my navigation bar. Any ideas?
Programmatically set navigation bar title empty string in viewDidLoad() of your UIViewController() instance:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = ""
}
You have probably set the title in Storyboard. Remove it in Storyboard or set the title to nil in viewDidLoad() of your UIViewController instance:
override func viewDidLoad() {
super.viewDidLoad()
self.title = nil
}
The mechanism to remove the title in code is always like .remove().
In Swift5, we have titleView?.removeFromSuperview() to remove the title view and title?.removeAll() to remove the text in title:
//M: find your controller -> tabBarController -> navigationItem -> titleView/title -> remove
self.tabBarController?.navigationItem.titleView?.removeFromSuperview()
self.tabBarController?.navigationItem.title?.removeAll()
I have been struggling to pass data between view controllers using segues. I have watched many youtube video tutorials and searched around on forums but I can't figure out why it won't work. I don't get any error messages but when I click the 'Button' the simulator crashes. I would love some help! Thanks!
Here is the code :
ViewController1 :
import UIKit
class ViewController: UIViewController {
#IBOutlet var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destViewController : ViewController2 = segue.destinationViewController as! ViewController2
destViewController.labelText = textField.text!
}
}
ViewController2 :
import Foundation
import UIKit
class ViewController2 : UIViewController {
#IBOutlet var Label: UILabel!
var labelText : String = ""
override func viewDidLoad() {
Label.text = labelText
}
}
StoryBoard Image :
storyboard image
As stated on the comment the problem was that you were using a push segue on a UIViewController.
Please note that push and modal segues are deprecated... Please take a look at this post entry and let me know if you have any doubts
What's the difference between all the Selection Segues?
Make sure to enter some text in your label textField before tapping the button since you are passing that label's text to next scene by using force unwrap.
destViewController.labelText = textField.text! //here compiler expects that your textField has the value
I made a sample project using your same code and didn't get an error. There's nothing wrong with your code.
Try this: Go to your storyboard and check outlets inspector for each view controller and make sure you don't have an outlet to nowhere.
In a storyboard i have a view controller which has a segue to another viewcontroller, with the "Present As Popover" applied. If i don't add any code, this works as id expect.
Issue:
I need to get the delegate working so i can get data back from the popover. I have created the delegate in the popover.
I have added
class LoginView: UIViewController,UIPopoverPresentationControllerDelegate, UIPopoverControllerDelegate,KeypadDelegate
I have added the functions to my main view controller.
Issue i have is how to set the delegate on the segue.
i have tried
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "KeyPadLoad"
{
let popoverViewController = segue.destinationViewController as! UIViewController
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.popoverPresentationController!.delegate = self
}
}
The segue is defiantly called KeyPadLoad but when the delegate functions should be called in the popover nothing happens.
What am i doing wrong
Thanks
You have to implement the UIPopoverPresentationControllerDelegate in the class that presents the Popover. The methods of the UIPopoverPresentationControllerDelegate protocol let you customize the behavior of a popover-based presentation.
If you have set a segue to go to the Popover and you set the segue as Present as Popover then your class has to be like this :
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var popOver = segue.destinationViewController as! PopOverViewController
popOver.popoverPresentationController!.delegate = self
}
// Tells the delegate that the popover was dismissed.
func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
println("dismissed")
}
}
Where the class PopOverViewController is just an UIViewController that handle the Popover, no more, something like this code :
class PopOverViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
With the above code you should see the message:
dismissed
Every time that the PopOverViewController is dismissed.
I hope this help you.