I want to pass a delegate like var delegate: Diary?from my HeaterViewController to my DetailViewController to get the core data attributes of it. So I have set the delegate in func didselectItem view.delegate = diarysbut it don´t run I have no segue to pass the data between the view controllers. So the question was how I can pass the delegate between the view controllers to get the title from the core data entry.
The other thing is I want to get access to the func from the headerviewcontroller from a other viewcontroller. I have tried with
let vc = storyboard!.instantiateViewController(withIdentifier: "DiaryController") as! DiaryController
vc.loadDiarys()
but it don´t run so I need the func from the DiaryController (loadDiarys) in this viewcontroller. I have no segue to use.
Code from DetailViewController:
var delegate: Diary?
AddTitle.text = delegate.title
Code from HeaderViewController:
var diary = [Diary] () {
didSet {
self.DiaryCollectionView.reloadData()
}
}
var mgdContext = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext
load new Entry:
func loadDiarys () {
let loadRequest: NSFetchRequest<Diary> = Diary.fetchRequest()
diary = try! mgdContext.fetch(loadRequest as! NSFetchRequest<NSFetchRequestResult>) as! [Diary]
print("loadD")
}
hand over delegate to other ViewController (DetailDiaryController)
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let diarys = diary[indexPath.row]
let view = DetailDiaryController.instantiateFromNib()
view.delegate = diarys
let window = UIApplication.shared.delegate?.window!
let modal = PathDynamicModalPan()
modal.showMagnitude = 200.0
modal.closeMagnitude = 130.0
view.closeButtonHandler = {[weak modal] in
modal?.closeWithLeansRandom()
return
}
view.bottomButtonHandler = {[weak modal] in
modal?.closeWithLeansRandom()
return
}
modal.show(modalView: view, inView: window!)
}
}
Related
I am trying to recreate the Notes app in iOS. I have created an initial View Controller which is just a table view. A user can go to a Detail View Controller to compose a new note with a Title and Body section. When they click Done, I want to manipulate the tableView with note's details.
I am struggling saving the details of what the user entered to use on my initial view controller.
Here's my Notes class which defines the notes data:
class Notes: Codable {
var titleText: String?
var bodyText: String?
}
Here is the Detail View controller where a user can input Note details:
class DetailViewController: UIViewController {
#IBOutlet var noteTitle: UITextField!
#IBOutlet var noteBody: UITextView!
var noteDetails: Notes?
var noteArray = [Notes]()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(updateNote))
noteTitle.borderStyle = .none
}
#objc func updateNote() {
noteDetails?.titleText = noteTitle.text
noteDetails?.bodyText = noteBody.text
noteArray.append(noteDetails!) // This is nil
// not sure if this is the right way to send the details over
// let vc = ViewController()
// vc.noteArray.append(noteDetails!)
if let vc = storyboard?.instantiateViewController(identifier: "Main") {
navigationController?.pushViewController(vc, animated: true)
}
}
}
I also have an array on my initial view controller as well. I think I need this one to store note data to display in the tableView (and maybe don't need the one on my Detail View controller?). The tableView is obviously not completely implemented yet.
class ViewController: UITableViewController {
var noteArray = [Notes]()
override func viewDidLoad() {
super.viewDidLoad()
print(noteArray)
self.navigationItem.setHidesBackButton(true, animated: true)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(composeNote))
}
#objc func composeNote() {
if let dvc = storyboard?.instantiateViewController(identifier: "Detail") as? DetailViewController {
navigationController?.pushViewController(dvc, animated: true)
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
noteArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
return cell
}
Just using Delegate:
First create delegate protocol with a func to send back note to your viewController
protocol DetailViewControllerDelegate: AnyObject {
func newNoteDidAdded(_ newNote: Note)
}
Next add the delegate variable to DetailViewController, and call func noteDataDidUpdate to send data back to viewController
class DetailViewController: UIViewController {
weak var delegate: DetailViewControllerDelegate?
#objc func updateNote() {
....
delegate?.newNoteDidAdded(newNote)
}
}
finally, set delegate variable to viewController and implement this in ViewController
class ViewController: UIViewController {
....
#objc func composeNote() {
if let dvc = storyboard?.instantiateViewController(identifier: "Detail") as? DetailViewController {
dvc.delegate = self
navigationController?.pushViewController(dvc, animated: true)
}
}
}
extension ViewController: DetailViewControllerDelegate {
func newNoteDidAdded(_ newNote: Note) {
// do some thing with your new note
}
}
I have a collection view controller. In collectionView cell I have label which I made clickable to push to the nextViewController.
I know that problem in navigationController. But I'm new in swift so can't fix. Hope you guys can help me.
Here's my SceneDelegate:
let layout = UICollectionViewFlowLayout()
// Create the root view controller as needed
let nc = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
let win = UIWindow(windowScene: winScene)
win.rootViewController = nc
win.makeKeyAndVisible()
window = win
and my label:
let text = UILabel()
text.text = "something"
text.isUserInteractionEnabled = true
self.addSubview(text)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PopularCellTwo.labelTapped))
text.addGestureRecognizer(gestureRecognizer)
}
#objc func labelTapped() {
let nextVC = NextViewController()
self.navigationController?.pushViewController(nextVC, animated: true)
print("labelTapped tapped")
}
I also added screenshot. When I click on "Something" It should go next page.
[1]: https://i.stack.imgur.com/4oYwb.png
You can use delegate or closure to do this
class ItemCollectionViewCell: UICollectionViewCell {
var onTapGesture: (() -> ())?
}
Then in your function you do
#objc func labelTapped() {
onTapGesture?()
}
And in your controller
class HomeController: UICollectionViewController {
//...
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = // dequeue cell
cell.onTapGesture = { [unowned self] in
let nextVC = NextViewController()
self.navigationController?.pushViewController(nextVC, animated: true)
}
return cell
}
}
self.navigationController?.pushViewController(nextVC, animated: true)
what self are you referring to ? because you cant make push in child class
you have HomeController i assume its your parent controller .
just try to debug what self is this could attempt by debugging or debug by condition
print (self)
if (self.isKind(of: YourParentController.self)) {
// make push
}
or try to check , see if navigationcontroller somehow has nil value
Here is how you do it using closures. I've created a closure parameter in UICollectionViewCell sub-class. When the label gesture target is hit I call the closure which then executed the navigation in HomeController.
class HomeController: UICollectionViewController {
//...
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = // dequeue cell
cell.labelTap = { [weak self] in
guard let self = self else { return }
let nextVC = NextViewController()
self.navigationController?.pushViewController(nextVC, animated: true)
print("navigated")
}
return cell
}
}
class CollectionViewCell: UICollectionViewCell {
var labelTap: (() -> Void)?
#objc func labelTapped() {
print("labelTapped tapped")
labelTap?()
}
}
I have a Segmented control with a UIView in it as shown below:
I have added two custom views inside UIView by the following code:
class NotificationViewController: UIViewController {
#IBOutlet weak var viewContainer: UIView!
//create a variable for view
var views : [UIView]!
override func viewDidLoad() {
super.viewDidLoad()
//initialize the view
views = [UIView]()
//appened the view inside the views array
views.append(ImportantNotification().view)
views.append(GeneralNotificaton().view)
//start the loop to add the subviews inside the view
for v in views{
viewContainer.addSubview(v)
}
//bring the default view to the front while we launch it
viewContainer.bringSubview(toFront: views[0])
}
#IBAction func notificationSegemntsPressed(_ sender: UISegmentedControl) {
//finally bring the subview inside the segmented view
self.viewContainer.bringSubview(toFront: views[sender.selectedSegmentIndex])
}
}
And it Works!
The sub views with its .xib file are as follows:
I have kept a UI Table view inside the first sub view as shown below:
And, I have made a custom cell for the first subview as shown below:
I loaded the Json data from api and wanted to show it in the table view with the custom cell and the JSON data successfully loads but it doesnot populate in the table view.
My code for loading the data in table view is shown below:
import UIKit
class ImportantNotification: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var importantNotificationTableView: UITableView!
var nontificatonData = [NotificationDataModel]()
override func viewDidLoad() {
super.viewDidLoad()
importantNotificationTableView.delegate = self
importantNotificationTableView.dataSource = self
let nib = UINib(nibName: "TableViewCell", bundle: nil)
importantNotificationTableView.register(nib, forCellReuseIdentifier: "customCell")
downloadJSON {
self.importantNotificationTableView.reloadData()
}
}
func downloadJSON(completed: #escaping () -> () ) {
guard let url = URL(string : "http://www.something.com/notice/get") else {return}
var request = URLRequest.init(url: url)
request.httpMethod = "POST"
request.addValue("cf7ab8c9d4efae82b575eabd6bec76cbb8c6108391e036387f3dd5356a582171519367747000", forHTTPHeaderField: "app_key")
let postDictonary = "school_id=1"
//send value directly to server without chaging to json
request.httpBody = postDictonary.data(using: .utf8)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if error == nil{
do{
self.nontificatonData = try JSONDecoder().decode([NotificationDataModel].self, from: data!)
print(self.nontificatonData)
DispatchQueue.main.async {
completed()
}
}catch{
print(error)
}
}
}.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nontificatonData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TableViewCell
cell.lblTitileNotification.text = nontificatonData[indexPath.row].notice_title
cell.lblDiscriptionNotificaiton.text = nontificatonData[indexPath.row].notice_desc
return cell
}
}
My Struct is as follows:
import Foundation
struct NotificationDataModel : Decodable{
let notice_id : String
let notice_title : String
let notice_desc : String
let notice_date : String
let content_name : String
let notice_link : String
let is_important : String
let parent_availability : String
let is_pinned : String
let created_at : String
}
I'm sorry, but prototype cells are available only in storyboard-based projects. Hopefully you won't have gone too far down the xib approach and can try multiple storyboards instead. If you find them a bit overwhelming, it's OK; they get better – note that you don't have to have one storyboard for all your view controllers. You can have several, either linked in code or using storyboard references.
If you want to stick with xibs, another option is to use registerNib in code. So you design your prototype cells in xibs, then register them in code. Not quite as smooth, but it might suit your needs.
I have an empty view with a tab bar pictured below, when i load a routine a table appears containing the contents, however it seems to overlay the tab bar killing off app navigation. Its not sized in the storyboard to overlay it and its constraint locked to not do so, so im unsure why this is happening, pics of the issue and VC's code below:
VC Code:
import Foundation
import UIKit
import CoreData
class RoutineController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// MARK: - DECLARATIONS
#IBAction func unwindToRoutine(segue: UIStoryboardSegue) {}
#IBOutlet weak var daysRoutineTable: UITableView!
#IBOutlet weak var columnHeaderBanner: UIView!
#IBOutlet weak var todaysRoutineNavBar: UINavigationBar!
#IBOutlet weak var addTOdaysRoutineLabel: UILabel!
let date = Date()
let dateFormatter = DateFormatter()
let segueEditUserExerciseViewController = "editExerciseInRoutineSegue"
//This is the selected routine passed from the previous VC
var selectedroutine : UserRoutine?
// MARK: - VIEWDIDLOAD
override func viewDidLoad() {
super.viewDidLoad()
setupView()
daysRoutineTable.delegate = self
daysRoutineTable.dataSource = self
view.backgroundColor = (UIColor.customBackgroundGraphite())
dateFormatter.dateStyle = .short
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateStr = dateFormatter.string(from: date)
todaysRoutineNavBar.topItem?.title = dateStr + " Routine"
}
// MARK: - VIEWDIDAPPEAR
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.daysRoutineTable.reloadData()
self.updateView()
}
// MARK: - TABLE UPDATE COMPONENTS
private func setupView() {
updateView()
}
// MARK: - TABLE SETUP
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let count = self.selectedroutine?.userexercises?.count
{
print("exercises: \(count)")
return count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? TodaysRoutineTableViewCell else {
fatalError("Unexpected Index Path")
}
cell.backgroundColor = UIColor.customBackgroundGraphite()
cell.textLabel?.textColor = UIColor.white
configure(cell, at: indexPath)
return cell
}
// MARK: - VIEW CONTROLER ELEMENTS VISIBILITY CONTROL
fileprivate func updateView() {
var hasUserExercises = false
if let UserExercise = self.selectedroutine?.userexercises {
hasUserExercises = UserExercise.count > 0
}
addTOdaysRoutineLabel.isHidden = hasUserExercises
columnHeaderBanner.isHidden = !hasUserExercises
daysRoutineTable.isHidden = !hasUserExercises
}
// MARK: - SETTING DATA FOR A TABLE CELL
func configure(_ cell: TodaysRoutineTableViewCell, at indexPath: IndexPath) {
if let userExercise = selectedroutine?.userexercises?.allObjects[indexPath.row]
{
print("\((userExercise as! UserExercise).name)")
cell.todaysExerciseNameLabel.text = (userExercise as! UserExercise).name
cell.todaysExerciseRepsLabel.text = String((userExercise as! UserExercise).reps)
cell.todaysExerciseSetsLabel.text = String((userExercise as! UserExercise).sets)
cell.todaysExerciseWeightLabel.text = String((userExercise as! UserExercise).weight)
}
}
}
requested table constraints
Debug hierarchy
The Segue that sends the user back to the view that looses its tab bar
if segue.identifier == "addToTodaySegue" {
let indexPath = workoutTemplateTable.indexPathForSelectedRow
let selectedRow = indexPath?.row
print("selected row\(selectedRow)")
if let selectedRoutine = self.fetchedResultsController.fetchedObjects?[selectedRow!]
{
if let todaysRoutineController = segue.destination as? RoutineController {
todaysRoutineController.selectedroutine = selectedRoutine
}
}
}
I also feel perhaps the viewDidAppear code may cause the issue, perhaps the super class?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.daysRoutineTable.reloadData()
self.updateView()
Updated storyboard image
I suspect you need to embed your viewController in a UINavigationController.
Consider the following setup:
I suspect your setup is like the upper one:
TapBar -> ViewController -show segue-> ViewController
Which results in a hidden tapbar, like in your description:
While the bottom setup:
TapBar -> NavigationCntroller -rootView-> ViewController -show segue-> ViewController
results in:
which is what you want, how I understood.
Update
It's hard to see. The screenshot of your Storyboard is in pretty low resulution, but the segues look wrong. Double check them. A Segue of type show (e.g push) looks like this:
Also clear project and derived data. Segue type changes sometime are ignored until doing so.
Try calling this self.view.bringSubviewToFront(YourTabControl).
The previous suggestion should work. But the content at the bottom part of tableview will not be visible as the tabbar comes over it. So set the bottom constraint of tableview as the height of tabbar.
I am working in Xcode 8 - Swift.
I have 2 viewcontrollers: MAin Screen (first) and View Options (Second ViewController).
In the Main Screen I have a map with my location. In this screen I have a button so that the user can go to Views Screen and select what type o map he wants (this is done by a segue). The user can choose from three options: Normal, Satellite and Terrain. I implemented the second view controller has a table view of buttons. Whatever I choose it sends me to the home screen (first view controller).
I dont have another segue to send me to first view controller has I implemented a navigation controller with back buckon.
Tested this so far. Everything working ok : maps, and other things.
I created a protocol/delegate so that, when I choose what type of map I want to see in the first view controller, it sends me the information to the first. I tried send a string from the first view controller to the second and a managed to do that.
The thing is, I cannot change the map type... I dont know why but it was shows me the standard map...
Can anyone help me please?
Here is a sample of me code. Main Screen First View Controller:
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, DataSentDelegate {
var locationManager = CLLocationManager()
#IBOutlet var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.map.showsUserLocation = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
let latitude = userLocation.coordinate.latitude
let longitude = userLocation.coordinate.longitude
let latDelta: CLLocationDegrees = 0.05
let longDelta: CLLocationDegrees = 0.05
let span = MKCoordinateSpanMake(latDelta, longDelta)
let location = CLLocationCoordinate2DMake(latitude, longitude)
let region = MKCoordinateRegionMake(location, span)
self.map.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
//Protocol
func userSentView(data: String) {
switch (data) {
case "Satellite":
self.map.mapType = .satellite
break
case "Terrain":
self.map.mapType = .hybrid
break
default:
self.map.mapType = .standard
break
}
}
//Protocol
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SegueView" {
let TVCView: TVCView = segue.destination as! TVCView
TVCView.delegate = self
}
}
}
In the second viewcontroller I have this:
protocol DataSentDelegate {
func userSentView(data: String)
}
class TVCView: UITableViewController {
var ViewNames = [String]()
var identities = [String]()
//***
var delegate: DataSentDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
self.title = "View"
//globalView = 1;
ViewNames = ["Normal","Satellite","Terrain"]
identities = ["Normal","Satellite","Terrain"]
self.tableView.tableFooterView = UIView()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ViewNames.count
}
//Add names to cells
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellView", for: indexPath)
cell.textLabel?.text = ViewNames[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let viewController = storyboard?.instantiateViewController(withIdentifier: "Home")
self.navigationController?.pushViewController(viewController!, animated: true)
//To know which view was selected
let vcName = identities[indexPath.row]
//***
if delegate != nil {
let data = vcName
delegate?.userSentView(data: data)
dismiss(animated: true, completion: nil)
}
}
}
It looks like the problem is that you are pushing on a whole new (Home) ViewController rather than popping off the stack to the existing one.
You should remove these lines:
let viewController = storyboard?.instantiateViewController(withIdentifier: "Home")
self.navigationController?.pushViewController(viewController!, animated: true)
And replace this line:
dismiss(animated: true, completion: nil)
with:
let _ = navigationController?.popViewController(animated: true)