John-Lluch / SWRevealViewController

A UIViewController subclass for presenting side view controllers inspired on the FaceBook and Wunderlist apps, done right !
Other
4.52k stars 987 forks source link

How can I make SWRevealViewController to appear on the main Controller #711

Open DinosMa opened 7 years ago

DinosMa commented 7 years ago

I have cart an app news app and I am trying to make SWRevealViewController to work in the main controller (ListViewController - in my case). So far when I tap on the catogory on the menu it appears in the main Controller, but doesn't lead to any categories. It is obvious that something I have done wrong with the id.

`import UIKit

struct MenuList { var id: Int var title: String! }

class MenuController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var menuLists:[MenuList] = [

    MenuList(id: 0, title: "ΑΡΧΙΚΗ"),
    MenuList(id: 9, title: "ΕΠΙΚΑΙΡΟΤΗΤΑ"),
    MenuList(id: 17, title: "ΠΟΛΙΤΙΚΗ"),
    MenuList(id: 15, title: "ΟΙΚΟΝΟΜΙΑ"),
    MenuList(id: 8, title: "ΔΙΕΘΝΗ"),
    MenuList(id: 19, title: "ΠΟΛΙΤΙΣΜΟΣ"),
    MenuList(id: 11, title: "ΘΕΜΑ"),
    MenuList(id: 13, title: "ΚΟΙΝΩΝΙΑ"),
    MenuList(id: 13493, title: "GLOBAL GREEKS"),
    MenuList(id: 7, title: "ΑΡΘΡΟΓΡΑΦΟΙ"),
    MenuList(id: 5, title: "RITSMAS CORNER"),
    MenuList(id: 2, title: "LIFESTYLE"),
    MenuList(id: 7640, title: "ΑΝΥΠΕΡΘΕΤΩΣ"),
    MenuList(id: 25, title: "MEDIA"),
    MenuList(id: 10139, title: "ΑΠΟΧΡΩΣΕΙΣ"),
    MenuList(id: 47, title: "ΞΕΧΩΡΙΣΤΕΣ ΙΣΤΟΡΙΕΣ"),
    MenuList(id: 38, title: "ΠΡΟΣΩΠΑ"),
    MenuList(id: 12, title: "ΙΣΤΟΡΙΑ - ΓΕΩΠΟΛΙΤΙΚΗ"),
    MenuList(id: 10454, title: "ΜΕΡΕΣ ΒΟΥΛΗΣ"),
    MenuList(id: 7905, title: "ΕΠΙΧΕΙΡΗΜΑΤΙΚΑ ΝΕΑ"),
    MenuList(id: 7922, title: "ΕΡΓΑΣΙΑ"),
    MenuList(id: 27, title: "ΜΑΓΕΙΡΕΥΟΥΜΕ ΜΑΖΙ"),
    MenuList(id: 60, title: "ΕΥΖΩΙΑ"),
    MenuList(id: 24, title: "ΤΑΞΙΔΙΑ"),
    MenuList(id: 61, title: "ΕΠΙΣΤΗΜΗ"),
    MenuList(id: 20, title: "ΤΕΧΝΟΛΟΓΙΑ"),
    MenuList(id: 10, title: "ΠΕΡΙΒΑΛΛΟΝ"),
    MenuList(id: 18, title: "ΠΟΛΙΤΙΚΗ ΠΡΟΣΤΑΣΙΑ"),
    MenuList(id: 13335, title: "ΙΑΤΡΙΚΕΣ ΣΥΜΒΟΥΛΕΣ - Α.ΔΡΙΤΣΑΣ"),
    MenuList(id: 24, title: "ΥΓΕΙΑ"),
    MenuList(id: 6, title: "ΑΘΛΗΤΙΣΜΟΣ"),
    MenuList(id: 64, title: "ΚΗΠΟΥΡΙΚΗ"),
    MenuList(id: 16, title: "ΠΕΡΙΕΡΓΑ"),
    MenuList(id: 63, title: "ΖΩΔΙΑ"),
    MenuList(id: 11067, title: "WILD THINGS"),
    MenuList(id: 51578, title: "ΟΡΟΙ ΧΡΗΣΗΣ"),
]

var mainView: ListViewController? = nil

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.
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return menuLists.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell", for: indexPath) as UITableViewCell
    let menuList = self.menuLists[indexPath.row]
    cell.textLabel?.text = menuList.title
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let revealViewController: SWRevealViewController = self.revealViewController()
    let cell: MenuCell = tableView.cellForRow(at: indexPath) as! MenuCell

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let main = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController") //SWRevealViewController
        self.present(main, animated: true, completion: nil)
    }

}

screen shot 2017-07-19 at 09 55 33 `

iDevelopper commented 7 years ago

Did you set the tableview's delegate and dataSource to your list view controller?

DinosMa commented 7 years ago

yes to both Controllers menu Controller List View Controller - which is the main

when you tap on the menu it saw all the categories, when you tap then to random category .. it loads the main feeds and not the category feeds

simulator screen shot 19 jul 2017 12 16 40

iDevelopper commented 7 years ago

Sorry, I understood that your categories was not show in your table view!

In didSelectRowAt, you instantiate SWRevealViewController (which is already instantiated), then this new instance of SWRevealViewController show the front view controller...

If you want to show another view controller you have to instantiate and push it with SWRevealViewController:

            let controller = storyboard.instantiateViewController(withIdentifier: "YourController")
            let nc = UINavigationController(rootViewController: controller)
            revealViewController().pushMainViewController(nc, animated:true)
DinosMa commented 7 years ago

thanks but I get some errors

screen shot 2017-07-19 at 13 20 49

iDevelopper commented 7 years ago

Sorry: pushFrontViewController (pushMainViewController is for my library!)

DinosMa commented 7 years ago

thanks again but it crash I am kind confused .. not so much experinece anyway,

should I use like both?
screen shot 2017-07-19 at 14 08 12

or to delete the first one anyway .. weird error

screen shot 2017-07-19 at 14 11 00

iDevelopper commented 7 years ago

It is clear: Your controller in Main.storyboard does not have an identifier or it is not equal to "ListViewController":

Example: 2017-07-19_13-22-25

iDevelopper commented 7 years ago

You have to use only the 3 last lines of code.

DinosMa commented 7 years ago

thanks again it works but again it loads the main feeds and not the category feeds.. I think with the category_id might something might be wrong.. I dunno

screen shot 2017-07-19 at 14 35 36 screen shot 2017-07-19 at 14 35 17

screen shot 2017-07-19 at 14 37 35

iDevelopper commented 7 years ago

Because you have to instantiate and push DetailViewController instead of ListViewController, no?

iDevelopper commented 7 years ago

If you want you can share your project here and I will have a look.

DinosMa commented 7 years ago

yes .. thats the better thing to do I will share it as you might see there is also a other menu.. which I will delete it when I fix the SW the thing also is that the project is 52mb how to share it here?

iDevelopper commented 7 years ago

Ah, you can use https://wetransfer.com/

patrick.bodet4@wanadoo.fr

iDevelopper commented 7 years ago

Ok, got it

DinosMa commented 7 years ago

is kind messy project .. but is working .. let me know merci

iDevelopper commented 7 years ago

When you select a category (feed) in left menu you want to show detail view controller. And where do you want to go back after? In ListViewController I suppose? You want the back button to go back to ListViewController?

DinosMa commented 7 years ago

the main scene of the app is the ListViewController.. so when you tap the left menu .. it should shows the feed of the categories in the ListViewController .. try to see how it works on the right menu

iDevelopper commented 7 years ago

Ah you don't want to go directly to detail? You want to scroll the table view (ListViewController) as when you select on right menu?

DinosMa commented 7 years ago

yes .. thats what I am trying to do all this hours

DinosMa commented 7 years ago

also I will try to add a UIsearch in the top of the menu like this img_0026

iDevelopper commented 7 years ago

ListViewController.swift:

    func refresh(afterRefresh: (() -> Void)? = nil) {
        reloadPosts(onLoad: {() -> Void in
            self.refreshControl?.endRefreshing()
            afterRefresh!() // Added completion handler call
        })
    }

MenuController.swift:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let menuList = menuLists[indexPath.row]

        let revealViewController: SWRevealViewController = self.revealViewController()

        let nc = revealViewController.frontViewController as? UINavigationController
        let controller = nc?.topViewController as? ListViewController

        controller?.category_id = menuList.id
        controller?.refresh(afterRefresh: {() -> Void in
            self.revealViewController().revealToggle(animated: true)
        })
    }
DinosMa commented 7 years ago

thanks .. I really appreciate it! it working one last thing is when you go to the category to flip back the menu

iDevelopper commented 7 years ago

I don't understand your last question, sorry: "to flip back the menu"

DinosMa commented 7 years ago

ok .. mistake.. it was delay problem ... anyway thanks again .. for your help I need now to clean and update to Apple Store thanks

iDevelopper commented 7 years ago

Yes I think it is too long. Do you need to reload from Internet each time?

iDevelopper commented 7 years ago

You don't want to add your search bar before update to Apple Store?

iDevelopper commented 7 years ago

For the delay to close the menu, I forgot the main queue: Add DispatchQueue.main.async to revealToggle

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let menuList = menuLists[indexPath.row]

        let revealViewController: SWRevealViewController = self.revealViewController()

        let nc = revealViewController.frontViewController as? UINavigationController
        let controller = nc?.topViewController as? ListViewController

        controller?.category_id = menuList.id
        controller?.refresh(afterRefresh: {() -> Void in
            DispatchQueue.main.async {
                self.revealViewController().revealToggle(animated: true)
            }
        })
    }
iDevelopper commented 7 years ago

For your search bar:

MenuController.swift.zip

And embed your MenuController in Navigation controller (uncheck "Shows Navigation Bar") in storyboard

DinosMa commented 7 years ago

hey thanks .. sorry for the delay i was out !! let me try them how to manage all this and then i have to ask one more last thing about the images ... in the deital view.. that it cut the images half from the article

iDevelopper commented 7 years ago

I'am not a specialist of html but I think this is the server side responsibility. Or try with a Web View...

DinosMa commented 7 years ago

is it about html? I have put this `extension Data { var attributedString: NSAttributedString? { do { return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil) } catch let error as NSError { print(error.localizedDescription) } return nil } } it works but just cut the images !! hmm anyway in the next update i need to work for notifications and to put weather on nav ..

img_0029

`

iDevelopper commented 7 years ago

Είναι ωραίο σπίτι; Αγαπώ την Ελλάδα!

DinosMa commented 7 years ago

yeap we all do .. the weather and the sea ... not so much the rest so that why i am trying to learn swift and create apps so i can leave to go Holland as junior or medium iOS Developer as i saw you know python? this something that i have to learn also .. about Data Science, big data so are you in France? Paris?