0 Replies - 628 Views - Last Post: 14 April 2016 - 07:40 PM

#1 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 199
  • Joined: 18-May 11

Swift:PresentViewController presents compile error in static/class fun

Posted 14 April 2016 - 07:40 PM

I have a menu bar which I want to use on every page(UIViewController) in an app. So I figured I'd just write it once and use it on all the pages, instead of reproducing the same code on every page. To go about that, I created a new class which extends UIViewController(named it MenuBarViewController), with its own XIB file, created a function within that class which contains the code for generating the menubar, and just make a call to that function in any UIViewController where I want to place the menubar. Seems to work pretty seemlessly, but I run into problems when I tried to create some helper functions within the MenuBarViewController class, which trigger a navigation to another view, when one of the menu icons is tapped. I will include a simplified version of the code I'm working with below which illustrates the entirety of the problem.

    class MenuBarViewController: UIViewController {

        func menuBar(viewController:UIViewController){

            let menuBar = UIView(frame:CGRectMake(0,0,100,20 ))
            menuBar.backgroundColor = UIColor.blackColor()
            viewController.view.addSubview(menuBar) //Add the newly created view to the main view(self)
            //Add an icon view to the menubar
            let iconView = UIImageView(frame: CGRectMake(5,0,5,5))
            menuBar.addSubview(iconView)
       
            //Add a gesture recognizer for the icon view
            iconView.userInteractionEnabled = true
            let tapHomeButton = UITapGestureRecognizer(target: self, action:#selector(MenuBarViewController.goToHomeView(_:)/>/>/>))
            iconView.addGestureRecognizer(tapHomeButton)

            //Add an icon to the icon view
            let iconImageName = UIImage(named:"home.png")!
            iconView.image = iconImageName
        }


        //Helper function to handle the gesture recognizer, helps transition to another view
        func goToHomeView(sender: UIImageView!) {
            appData.lastView = "GameViewController"
            let settingsVC = SettingsViewController(nibName:     "SettingsViewController", bundle: nil)
            presentViewController(settingsVC, animated: true, completion: nil)
        }

    }







Now when I tap on the icon, there is no response. So I figured I'd declare the class functions(menuBar and goToHomeView) as either "class" or "static" funcs, and then call the menu bar directly with the class name. But then I get this compile error "extra argument 'animated' in call" on the this line:
presentViewController(settingsVC, animated: true, completion: nil)

So something must be wrong with the presentViewController method when it is declared within a static or class function because when I have the goToHomeView function just do something as simple as printing some text, it works fine. So I'm at a total loss at this point. What to do?? Am I going about this the completely wrong way?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1