I have a navigation bar with tableview, when I choose one of the cells of tableview it leads me to another table view with title of the cell chosen, and also I use page view controller for the these tableviews.
I set title of navigation bar for the root table in viewcontroller of the first tableview as:
override func viewDidLoad() {
self.navigationItem.title = "TableView Title"
}
I set the title of bar after choose the cell in the view controller of second tableview as:
override func viewDidLoad() {
self.navigationController?.navigationBar.topItem?.title = "chosen cell text"
}
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "chosen cell text"
}
With these codes, it sets root tableview's title properly.
But when I choose a cell. root table's title is appear in back button. And there is no other title.
When I go next page of second tableview, the title of the second tableview appear properly. Therefore why the second tableview's title doesnot appear, when it first appears. How can I invoke it?
Solution: Firstly thanks for your help. I try to change the title in page viewer controller by self.navigationItem.titleView = "chosen cell text"
and it works.
Set the title property of the Child View Controller itself.
Example:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Chosen Cell Text"
}
Related
Guys I want to make this screen for my App. I don't know how to make this scrollview into navigation using programmatically. please I want to make this scrollview programmatically similar design.
This is a collectionview you can acheive this by adding a collectionview in NavigationBar
weak var collectionView: UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
let navigationitem = UINavigationItem(title: "") //creates a new item with no title
navigationitem.titleView = collectionView //your collectionview here to display as a view instead of the title that is usually there
self.navigationController?.navigationBar.items = [navigationitem] //adds the navigation item to the navigationbar
}
For more assistance check this answer
I have following code in class homeVC: UICollectionViewController to display the title in the navigation bar and it works as expected.
override func viewDidLoad() {
super.viewDidLoad()
.
.
.
self.navigationItem.title = PFUser.current()?.username?.uppercased()
.
.
}
But when I clicked on the button to go to another view controller and then came back to homeVC view controller self.navigationItem.title is not displaying anything. I'mm wondering why that happened?
The following code is to go to another view controller
#objc func followersTap() {
category = "followers"
// make references to followersVC
let followers = self.storyboard?.instantiateViewController(withIdentifier: "followersVC") as! followersVC
// present
self.navigationController?.pushViewController(followers, animated: true)
}
This is the view controller to go to , and in this view controller i don't want to show the title from previous view controller so I'm using self.navigationController!.navigationBar.topItem!.title = "" to make it empty and when I go back to previous view controller the title is also empty, why?
class followersVC: UITableViewController{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController!.navigationBar.topItem!.title = ""
self.tableView.reloadData()
}
I am trying to not show the first view controller title on second view controller top left corner where back button is and keep first and second view controller title shows.
If your plan is to not show the title from a previous view controller, the solution is like this:
Change:
self.navigationItem.title = PFUser.current()?.username?.uppercased()
to:
self.title = PFUser.current()?.username?.uppercased()
And change:
self.navigationController!.navigationBar.topItem!.title = ""
to:
self.title = ""
Try to move your self.navigationItem.title = PFUser.current()?.username?.uppercased() in viewDidAppear(), this should help
I have a tab bar controller with four tabs. I want to show the first item at the beginning. With a button click from the first item (view), when it is clicked, I want to show the second tab. How can I do that?
I created a custom tabbarController class and tried to give tabbarindex like below. I checked at the beginning without a button click but it didn't work. It always loads the first tab bar item.
class HTabViewController: UITabBarController, UITabBarControllerDelegate {
var controllerArray : [UIViewController] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.selectedIndex = 2
self.tabBar.tintColor = UIColor.red
// defineViewController()
}
}
Note : can we show specific tab item with a button click?
Since self is the UITabBarController, you need to set the selectedIndex on self, not self.tabBarController.
override func viewDidLoad() {
super.viewDidLoad()
selectedIndex = 2
tabBar.tintColor = UIColor.red
}
job done, add self.selectedIndex = needed_index at viewDidLoad() method
I am working on setting up basic CRUD operations for a list using Table Views and Navigation Controllers. When I click the plus button in my initial Table View, it is supposed to use a segue to present modally the next Table View where I can add an item to the list in a text field that is in a table cell.
As it is now, when I click the plus button, it takes me to the right page, and the navbar buttons work on that page. When I click done, the text I have in my textField that I entered in the storyboard gets added to the list so I know it's there somewhere. I have auto layout set up so that shouldn't be the problem. It looks like there is something covering the table cell up (all darker grey), but I can't figure out what it is. See images below for clarification:
this image shows the storyboards, the last board is the one I having trouble with
this images shows the progression of the app as it is now, the middle frame should be showing the text field
Code for second view:
class MissionDetailsTableViewController: UITableViewController {
#IBAction func cancelBarButtonPressed(sender: UIBarButtonItem) {
cancelButtonDelegate?.cancelButtonPressedFrom(self)
}
#IBAction func doneBarButtonPressed(sender: UIBarButtonItem) {
delegate?.missionDetailsViewController(self, didFinishAddingMission: newMissionTextField.text!)
}
#IBOutlet weak var newMissionTextField: UITextField!
weak var cancelButtonDelegate: CancelButtonDelegate?
weak var delegate: MissionDetailsViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Protocols in second file:
protocol CancelButtonDelegate: class {
func cancelButtonPressedFrom(controller: UIViewController)
}
protocol MissionDetailsViewControllerDelegate: class {
func missionDetailsViewController(controller: MissionDetailsTableViewController, didFinishAddingMission mission: String)
}
I am still not sure what was going on but this worked for me:
I uncommented this default line of code in my second view controller, and saw that my table cell appeared. I recommented it out, and it still works.
self.navigationItem.rightBarButtonItem = self.editButtonItem()
I've an UITabBarController connected with an UINavigationController related with an UITableView and finally connected with an UIViewController.
I've connected programmatically the UITableView with the UIDetailViewController using that code:
...
navigationController?.pushViewController(detail!, animated: true)
In my UIDetailViewController if I try to change the navigationItem.title like that:
self.navigationController!.navigationItem.title = "Hello"
Nothing happens. The navigation item doesn't change title.
You can change title of navigationItem title using this :-
self.navigationItem.title = "YourTitle";
or other way is :-
In UIDetailViewController write this line of code :
override func viewDidLoad()
{
super.viewDidLoad()
self.title = "ok"
}