-1

I have written a function in Appdelegate. I want to override this function in ViewcontrollerA , ViewcontrollerB. Because same method has different behaviour in ViewcontrollerA , ViewcontrollerB.

How to call this function in Viewcontroller, I don't understand.

Update

class NaviBarVC: UIViewController {

override func viewDidLoad() {
        super.viewDidLoad()
}

func click_save() {



}
}


class ViewcontrollerA: NaviBarVC {

override func viewDidLoad() { super.viewDidLoad() }

override func click_save() { // Error 
super.click_save()

    }

 }
Aditya Srivastava
  • 2,630
  • 2
  • 13
  • 23
swiftuser123
  • 13
  • 2
  • 11
  • 4
    I don't think "override" is the word you are looking for. Your view controllers are surely not subclasses of your app delegate? – Nicolas Miari Sep 05 '17 at 11:00
  • If you want to **call** a method of your app delegate **from** your view controllers, use something like this: `if let appDelegate = UIApplication.delegate as? AppDelegate { appDelegate.myMethod() // change to your actual method }` – Nicolas Miari Sep 05 '17 at 11:02
  • Make a BaseViewController and write your method in that instead of AppDelegate and then inherit ViewControllerA and ViewControllerB from the BaseViewController. Then you can override that method. – Jitendra Solanki Sep 05 '17 at 11:02
  • Check this link https://stackoverflow.com/questions/24046164/how-do-i-get-a-reference-to-the-app-delegate-in-swift – KethanKumar Sep 05 '17 at 11:03
  • Write method() in extension of UIViewController then it is possible to override. – Sanjay Shah Sep 05 '17 at 11:03

3 Answers3

0

You can't override function that declare in AppDelegate but you can access function that added in AppDelegate by use of declare global variable.

let appDelegate = UIApplication.shared.delegate as! AppDelegate

and you can access AppDelegate's property anywhere in any viewController by

appDelegate.yourFuncName...() 
Govaadiyo
  • 5,644
  • 9
  • 43
  • 72
  • But i want to change its behaviour in my Viewcontroller, is it not possible? – swiftuser123 Sep 05 '17 at 11:05
  • Actually I have created my own Navigationbar in Appdelegate, because I want this navigation bar to be present in every viewcontroller. Save button in this navigation bar has an action. I want to call this save function in other viewcontrollers and save different values as per viewcontroller. – swiftuser123 Sep 05 '17 at 11:10
0

You cannot override that AppDelegate method. As ViewController heirarchy doesn't have it's superclass as AppDelegate. It inherits from UIViewController. So if you want to override, you have to create a viewController class and from this class you can override that method by inheriting

Like this----

 class BaseViewController : UIViewController{

func yourMethod(){
   }

}

Now, class which overrides this method will be like this

class YourController : BaseViewController{

override func yourMethod(){
super.yourMethod()
// write your code here

   }
}

And yes if you just want to access the method of AppDelegate and don't want to override it. You can do like this.

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.yourMethod()
Aditya Srivastava
  • 2,630
  • 2
  • 13
  • 23
-1

make class method in AppDelegate.h

   @interface
   +(AppDelegate*)Getdelegate;
   -(void)testfunction;

AppDelegate.m

+(AppDelegate*)Getdelegate
 {
   return (AppDelegate*)[UIApplication sharedApplication].delegate;
  }

in ViewControllerA

  [[AppDelegate Getdelegate] testFunction];
Vikas Rajput
  • 1,754
  • 1
  • 14
  • 26