UIViewControllers sharing 'generic' IBAction












2















I have an app with 6 UIViewControllers.



ANY viewcontroller features a function like this one:



@IBAction func onHelp(_ sender: UIBarButtonItem) {

DispatchQueue.main.async(execute: { () -> Void in
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = "MapHelp"
helpVC.helpSubtitle = "Map"
self.present(helpVC, animated: true, completion: nil)
})

}


Any IBAction in any viewcontroller presents the same HelpViewController but passing different parameters (starter and helpSubtitle).



Since I don't like to repeat code, first of all I thought this function should be converted to something more generic.



But: is there any way to create a generic IBAction, working for every viewcontroller?










share|improve this question


















  • 1





    UIBarButtonItem, isn't that a NavigationController button? What about subclassing the NavigationController, add that method, and on each ViewController, add an protocol extension to retrieve the parameters?

    – Larme
    Jan 3 at 10:02













  • Why not create a BaseViewController, have all your ViewControllers inherit from that one, Create IBActions in each VC, call the same parent class method from all the children. That way the code is reused, you don't have to write it everywhere and you don't have to worry about creating a generic IBAction. Make the method generic, keep the IBActions separate. You can provide the parameters in the base class method's signature too, which can vary from VC to VC

    – NSNoob
    Jan 3 at 10:16








  • 2





    Also any reason why you are specifically doing it in the main thread? IBActions are by default performed on the main thread.

    – NSNoob
    Jan 3 at 10:17











  • @Larme : yes, it is a Navigation Controller, but, please, could you expand a little? I'm not sure I understood your solution.

    – user5273262
    Jan 3 at 10:35








  • 1





    I have deleted my answer. It would not work really because the protocol method could not be an @IBAction.

    – Sulthan
    Jan 3 at 10:57
















2















I have an app with 6 UIViewControllers.



ANY viewcontroller features a function like this one:



@IBAction func onHelp(_ sender: UIBarButtonItem) {

DispatchQueue.main.async(execute: { () -> Void in
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = "MapHelp"
helpVC.helpSubtitle = "Map"
self.present(helpVC, animated: true, completion: nil)
})

}


Any IBAction in any viewcontroller presents the same HelpViewController but passing different parameters (starter and helpSubtitle).



Since I don't like to repeat code, first of all I thought this function should be converted to something more generic.



But: is there any way to create a generic IBAction, working for every viewcontroller?










share|improve this question


















  • 1





    UIBarButtonItem, isn't that a NavigationController button? What about subclassing the NavigationController, add that method, and on each ViewController, add an protocol extension to retrieve the parameters?

    – Larme
    Jan 3 at 10:02













  • Why not create a BaseViewController, have all your ViewControllers inherit from that one, Create IBActions in each VC, call the same parent class method from all the children. That way the code is reused, you don't have to write it everywhere and you don't have to worry about creating a generic IBAction. Make the method generic, keep the IBActions separate. You can provide the parameters in the base class method's signature too, which can vary from VC to VC

    – NSNoob
    Jan 3 at 10:16








  • 2





    Also any reason why you are specifically doing it in the main thread? IBActions are by default performed on the main thread.

    – NSNoob
    Jan 3 at 10:17











  • @Larme : yes, it is a Navigation Controller, but, please, could you expand a little? I'm not sure I understood your solution.

    – user5273262
    Jan 3 at 10:35








  • 1





    I have deleted my answer. It would not work really because the protocol method could not be an @IBAction.

    – Sulthan
    Jan 3 at 10:57














2












2








2








I have an app with 6 UIViewControllers.



ANY viewcontroller features a function like this one:



@IBAction func onHelp(_ sender: UIBarButtonItem) {

DispatchQueue.main.async(execute: { () -> Void in
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = "MapHelp"
helpVC.helpSubtitle = "Map"
self.present(helpVC, animated: true, completion: nil)
})

}


Any IBAction in any viewcontroller presents the same HelpViewController but passing different parameters (starter and helpSubtitle).



Since I don't like to repeat code, first of all I thought this function should be converted to something more generic.



But: is there any way to create a generic IBAction, working for every viewcontroller?










share|improve this question














I have an app with 6 UIViewControllers.



ANY viewcontroller features a function like this one:



@IBAction func onHelp(_ sender: UIBarButtonItem) {

DispatchQueue.main.async(execute: { () -> Void in
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = "MapHelp"
helpVC.helpSubtitle = "Map"
self.present(helpVC, animated: true, completion: nil)
})

}


Any IBAction in any viewcontroller presents the same HelpViewController but passing different parameters (starter and helpSubtitle).



Since I don't like to repeat code, first of all I thought this function should be converted to something more generic.



But: is there any way to create a generic IBAction, working for every viewcontroller?







swift function viewcontroller dry ibaction






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 9:59







user5273262















  • 1





    UIBarButtonItem, isn't that a NavigationController button? What about subclassing the NavigationController, add that method, and on each ViewController, add an protocol extension to retrieve the parameters?

    – Larme
    Jan 3 at 10:02













  • Why not create a BaseViewController, have all your ViewControllers inherit from that one, Create IBActions in each VC, call the same parent class method from all the children. That way the code is reused, you don't have to write it everywhere and you don't have to worry about creating a generic IBAction. Make the method generic, keep the IBActions separate. You can provide the parameters in the base class method's signature too, which can vary from VC to VC

    – NSNoob
    Jan 3 at 10:16








  • 2





    Also any reason why you are specifically doing it in the main thread? IBActions are by default performed on the main thread.

    – NSNoob
    Jan 3 at 10:17











  • @Larme : yes, it is a Navigation Controller, but, please, could you expand a little? I'm not sure I understood your solution.

    – user5273262
    Jan 3 at 10:35








  • 1





    I have deleted my answer. It would not work really because the protocol method could not be an @IBAction.

    – Sulthan
    Jan 3 at 10:57














  • 1





    UIBarButtonItem, isn't that a NavigationController button? What about subclassing the NavigationController, add that method, and on each ViewController, add an protocol extension to retrieve the parameters?

    – Larme
    Jan 3 at 10:02













  • Why not create a BaseViewController, have all your ViewControllers inherit from that one, Create IBActions in each VC, call the same parent class method from all the children. That way the code is reused, you don't have to write it everywhere and you don't have to worry about creating a generic IBAction. Make the method generic, keep the IBActions separate. You can provide the parameters in the base class method's signature too, which can vary from VC to VC

    – NSNoob
    Jan 3 at 10:16








  • 2





    Also any reason why you are specifically doing it in the main thread? IBActions are by default performed on the main thread.

    – NSNoob
    Jan 3 at 10:17











  • @Larme : yes, it is a Navigation Controller, but, please, could you expand a little? I'm not sure I understood your solution.

    – user5273262
    Jan 3 at 10:35








  • 1





    I have deleted my answer. It would not work really because the protocol method could not be an @IBAction.

    – Sulthan
    Jan 3 at 10:57








1




1





UIBarButtonItem, isn't that a NavigationController button? What about subclassing the NavigationController, add that method, and on each ViewController, add an protocol extension to retrieve the parameters?

– Larme
Jan 3 at 10:02







UIBarButtonItem, isn't that a NavigationController button? What about subclassing the NavigationController, add that method, and on each ViewController, add an protocol extension to retrieve the parameters?

– Larme
Jan 3 at 10:02















Why not create a BaseViewController, have all your ViewControllers inherit from that one, Create IBActions in each VC, call the same parent class method from all the children. That way the code is reused, you don't have to write it everywhere and you don't have to worry about creating a generic IBAction. Make the method generic, keep the IBActions separate. You can provide the parameters in the base class method's signature too, which can vary from VC to VC

– NSNoob
Jan 3 at 10:16







Why not create a BaseViewController, have all your ViewControllers inherit from that one, Create IBActions in each VC, call the same parent class method from all the children. That way the code is reused, you don't have to write it everywhere and you don't have to worry about creating a generic IBAction. Make the method generic, keep the IBActions separate. You can provide the parameters in the base class method's signature too, which can vary from VC to VC

– NSNoob
Jan 3 at 10:16






2




2





Also any reason why you are specifically doing it in the main thread? IBActions are by default performed on the main thread.

– NSNoob
Jan 3 at 10:17





Also any reason why you are specifically doing it in the main thread? IBActions are by default performed on the main thread.

– NSNoob
Jan 3 at 10:17













@Larme : yes, it is a Navigation Controller, but, please, could you expand a little? I'm not sure I understood your solution.

– user5273262
Jan 3 at 10:35







@Larme : yes, it is a Navigation Controller, but, please, could you expand a little? I'm not sure I understood your solution.

– user5273262
Jan 3 at 10:35






1




1





I have deleted my answer. It would not work really because the protocol method could not be an @IBAction.

– Sulthan
Jan 3 at 10:57





I have deleted my answer. It would not work really because the protocol method could not be an @IBAction.

– Sulthan
Jan 3 at 10:57












1 Answer
1






active

oldest

votes


















0














Create a BaseViewController and add the generic method there.



class BaseViewController: UIViewController {



override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

func genericMethod(starter: String, helpSubtitle: String){
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = starter
helpVC.helpSubtitle = helpSubtitle
self.present(helpVC, animated: true, completion: nil)
}

@IBAction func onHelp(_ sender: UIButton?) {
//You can use this method as generic IBaction if you want. It can be connected to buttons of all child View Controllers. But doing so will limit your param sending ability. On the plus side though, you won't have to define an IBAction everywhere and you can simply connect your child VC's button to Parent Class' IBAction.
}

}


Now inherit your ViewControllers from this class like:



import UIKit

class ViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller", helpSubtitle: "I was triggered from VC1")
}

}


and



import UIKit

class SecondViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

@IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller 2", helpSubtitle: "I was triggered from VC2")
}

}


That's it. Both your ViewControllers can call the parent method. If you still want to use the generic IBAction, you can do that too but I'd not recommend that course given that you want to pass params that can vary. If you wanted to do it though, it would look like this:



enter image description here



Bear in mind, the ViewController here has been inherited from the base ViewController which is why it can access the IBActions defined in the parent class. All you have to do is drag and connect.






share|improve this answer


























  • I like it, but, even after researching, I hoped that something could be done to have a sort of generic IBAction too. Thanks :-)

    – user5273262
    Jan 3 at 10:44













  • IBAction has nothing to do with "generics" or "generic approach", it is still a function, it's just become visible in IB.

    – Injectios
    Jan 3 at 11:22













  • @Injectios Not sure what gave the impression that I was implying otherwise. If it helps, English is my second language so it is possible that I may not have been clear enough. What I meant by generic was as in a single method that can be used by multiple objects.

    – NSNoob
    Jan 3 at 11:24











  • So these objects must be same type/class in IB. Otherwise, if you declare different types/classes then your function is not visible, you can try to use protocols/or inheritance and conform all your view-controllers to protocol where IBAction is declared, never tried though.

    – Injectios
    Jan 3 at 11:28













  • @Injectios If all those objects inherited from the same parent class then yes, it works. Other types won't be able to access it but then again, in OP's scenario, they don't have to. Only the VCs need to access it. So what would be the whole point? As for your other suggestion, OP will have to provide definitions for the method signatures of the protocol in all the VCs, which is precisely what they want to avoid. You could try the route Sulthan took by creating an extension of UIViewController but as he says, protocol methods cannot be IBActions.

    – NSNoob
    Jan 3 at 11:35











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019960%2fuiviewcontrollers-sharing-generic-ibaction%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Create a BaseViewController and add the generic method there.



class BaseViewController: UIViewController {



override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

func genericMethod(starter: String, helpSubtitle: String){
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = starter
helpVC.helpSubtitle = helpSubtitle
self.present(helpVC, animated: true, completion: nil)
}

@IBAction func onHelp(_ sender: UIButton?) {
//You can use this method as generic IBaction if you want. It can be connected to buttons of all child View Controllers. But doing so will limit your param sending ability. On the plus side though, you won't have to define an IBAction everywhere and you can simply connect your child VC's button to Parent Class' IBAction.
}

}


Now inherit your ViewControllers from this class like:



import UIKit

class ViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller", helpSubtitle: "I was triggered from VC1")
}

}


and



import UIKit

class SecondViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

@IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller 2", helpSubtitle: "I was triggered from VC2")
}

}


That's it. Both your ViewControllers can call the parent method. If you still want to use the generic IBAction, you can do that too but I'd not recommend that course given that you want to pass params that can vary. If you wanted to do it though, it would look like this:



enter image description here



Bear in mind, the ViewController here has been inherited from the base ViewController which is why it can access the IBActions defined in the parent class. All you have to do is drag and connect.






share|improve this answer


























  • I like it, but, even after researching, I hoped that something could be done to have a sort of generic IBAction too. Thanks :-)

    – user5273262
    Jan 3 at 10:44













  • IBAction has nothing to do with "generics" or "generic approach", it is still a function, it's just become visible in IB.

    – Injectios
    Jan 3 at 11:22













  • @Injectios Not sure what gave the impression that I was implying otherwise. If it helps, English is my second language so it is possible that I may not have been clear enough. What I meant by generic was as in a single method that can be used by multiple objects.

    – NSNoob
    Jan 3 at 11:24











  • So these objects must be same type/class in IB. Otherwise, if you declare different types/classes then your function is not visible, you can try to use protocols/or inheritance and conform all your view-controllers to protocol where IBAction is declared, never tried though.

    – Injectios
    Jan 3 at 11:28













  • @Injectios If all those objects inherited from the same parent class then yes, it works. Other types won't be able to access it but then again, in OP's scenario, they don't have to. Only the VCs need to access it. So what would be the whole point? As for your other suggestion, OP will have to provide definitions for the method signatures of the protocol in all the VCs, which is precisely what they want to avoid. You could try the route Sulthan took by creating an extension of UIViewController but as he says, protocol methods cannot be IBActions.

    – NSNoob
    Jan 3 at 11:35
















0














Create a BaseViewController and add the generic method there.



class BaseViewController: UIViewController {



override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

func genericMethod(starter: String, helpSubtitle: String){
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = starter
helpVC.helpSubtitle = helpSubtitle
self.present(helpVC, animated: true, completion: nil)
}

@IBAction func onHelp(_ sender: UIButton?) {
//You can use this method as generic IBaction if you want. It can be connected to buttons of all child View Controllers. But doing so will limit your param sending ability. On the plus side though, you won't have to define an IBAction everywhere and you can simply connect your child VC's button to Parent Class' IBAction.
}

}


Now inherit your ViewControllers from this class like:



import UIKit

class ViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller", helpSubtitle: "I was triggered from VC1")
}

}


and



import UIKit

class SecondViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

@IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller 2", helpSubtitle: "I was triggered from VC2")
}

}


That's it. Both your ViewControllers can call the parent method. If you still want to use the generic IBAction, you can do that too but I'd not recommend that course given that you want to pass params that can vary. If you wanted to do it though, it would look like this:



enter image description here



Bear in mind, the ViewController here has been inherited from the base ViewController which is why it can access the IBActions defined in the parent class. All you have to do is drag and connect.






share|improve this answer


























  • I like it, but, even after researching, I hoped that something could be done to have a sort of generic IBAction too. Thanks :-)

    – user5273262
    Jan 3 at 10:44













  • IBAction has nothing to do with "generics" or "generic approach", it is still a function, it's just become visible in IB.

    – Injectios
    Jan 3 at 11:22













  • @Injectios Not sure what gave the impression that I was implying otherwise. If it helps, English is my second language so it is possible that I may not have been clear enough. What I meant by generic was as in a single method that can be used by multiple objects.

    – NSNoob
    Jan 3 at 11:24











  • So these objects must be same type/class in IB. Otherwise, if you declare different types/classes then your function is not visible, you can try to use protocols/or inheritance and conform all your view-controllers to protocol where IBAction is declared, never tried though.

    – Injectios
    Jan 3 at 11:28













  • @Injectios If all those objects inherited from the same parent class then yes, it works. Other types won't be able to access it but then again, in OP's scenario, they don't have to. Only the VCs need to access it. So what would be the whole point? As for your other suggestion, OP will have to provide definitions for the method signatures of the protocol in all the VCs, which is precisely what they want to avoid. You could try the route Sulthan took by creating an extension of UIViewController but as he says, protocol methods cannot be IBActions.

    – NSNoob
    Jan 3 at 11:35














0












0








0







Create a BaseViewController and add the generic method there.



class BaseViewController: UIViewController {



override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

func genericMethod(starter: String, helpSubtitle: String){
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = starter
helpVC.helpSubtitle = helpSubtitle
self.present(helpVC, animated: true, completion: nil)
}

@IBAction func onHelp(_ sender: UIButton?) {
//You can use this method as generic IBaction if you want. It can be connected to buttons of all child View Controllers. But doing so will limit your param sending ability. On the plus side though, you won't have to define an IBAction everywhere and you can simply connect your child VC's button to Parent Class' IBAction.
}

}


Now inherit your ViewControllers from this class like:



import UIKit

class ViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller", helpSubtitle: "I was triggered from VC1")
}

}


and



import UIKit

class SecondViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

@IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller 2", helpSubtitle: "I was triggered from VC2")
}

}


That's it. Both your ViewControllers can call the parent method. If you still want to use the generic IBAction, you can do that too but I'd not recommend that course given that you want to pass params that can vary. If you wanted to do it though, it would look like this:



enter image description here



Bear in mind, the ViewController here has been inherited from the base ViewController which is why it can access the IBActions defined in the parent class. All you have to do is drag and connect.






share|improve this answer















Create a BaseViewController and add the generic method there.



class BaseViewController: UIViewController {



override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

func genericMethod(starter: String, helpSubtitle: String){
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = starter
helpVC.helpSubtitle = helpSubtitle
self.present(helpVC, animated: true, completion: nil)
}

@IBAction func onHelp(_ sender: UIButton?) {
//You can use this method as generic IBaction if you want. It can be connected to buttons of all child View Controllers. But doing so will limit your param sending ability. On the plus side though, you won't have to define an IBAction everywhere and you can simply connect your child VC's button to Parent Class' IBAction.
}

}


Now inherit your ViewControllers from this class like:



import UIKit

class ViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller", helpSubtitle: "I was triggered from VC1")
}

}


and



import UIKit

class SecondViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

@IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller 2", helpSubtitle: "I was triggered from VC2")
}

}


That's it. Both your ViewControllers can call the parent method. If you still want to use the generic IBAction, you can do that too but I'd not recommend that course given that you want to pass params that can vary. If you wanted to do it though, it would look like this:



enter image description here



Bear in mind, the ViewController here has been inherited from the base ViewController which is why it can access the IBActions defined in the parent class. All you have to do is drag and connect.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 10:41

























answered Jan 3 at 10:35









NSNoobNSNoob

4,34152946




4,34152946













  • I like it, but, even after researching, I hoped that something could be done to have a sort of generic IBAction too. Thanks :-)

    – user5273262
    Jan 3 at 10:44













  • IBAction has nothing to do with "generics" or "generic approach", it is still a function, it's just become visible in IB.

    – Injectios
    Jan 3 at 11:22













  • @Injectios Not sure what gave the impression that I was implying otherwise. If it helps, English is my second language so it is possible that I may not have been clear enough. What I meant by generic was as in a single method that can be used by multiple objects.

    – NSNoob
    Jan 3 at 11:24











  • So these objects must be same type/class in IB. Otherwise, if you declare different types/classes then your function is not visible, you can try to use protocols/or inheritance and conform all your view-controllers to protocol where IBAction is declared, never tried though.

    – Injectios
    Jan 3 at 11:28













  • @Injectios If all those objects inherited from the same parent class then yes, it works. Other types won't be able to access it but then again, in OP's scenario, they don't have to. Only the VCs need to access it. So what would be the whole point? As for your other suggestion, OP will have to provide definitions for the method signatures of the protocol in all the VCs, which is precisely what they want to avoid. You could try the route Sulthan took by creating an extension of UIViewController but as he says, protocol methods cannot be IBActions.

    – NSNoob
    Jan 3 at 11:35



















  • I like it, but, even after researching, I hoped that something could be done to have a sort of generic IBAction too. Thanks :-)

    – user5273262
    Jan 3 at 10:44













  • IBAction has nothing to do with "generics" or "generic approach", it is still a function, it's just become visible in IB.

    – Injectios
    Jan 3 at 11:22













  • @Injectios Not sure what gave the impression that I was implying otherwise. If it helps, English is my second language so it is possible that I may not have been clear enough. What I meant by generic was as in a single method that can be used by multiple objects.

    – NSNoob
    Jan 3 at 11:24











  • So these objects must be same type/class in IB. Otherwise, if you declare different types/classes then your function is not visible, you can try to use protocols/or inheritance and conform all your view-controllers to protocol where IBAction is declared, never tried though.

    – Injectios
    Jan 3 at 11:28













  • @Injectios If all those objects inherited from the same parent class then yes, it works. Other types won't be able to access it but then again, in OP's scenario, they don't have to. Only the VCs need to access it. So what would be the whole point? As for your other suggestion, OP will have to provide definitions for the method signatures of the protocol in all the VCs, which is precisely what they want to avoid. You could try the route Sulthan took by creating an extension of UIViewController but as he says, protocol methods cannot be IBActions.

    – NSNoob
    Jan 3 at 11:35

















I like it, but, even after researching, I hoped that something could be done to have a sort of generic IBAction too. Thanks :-)

– user5273262
Jan 3 at 10:44







I like it, but, even after researching, I hoped that something could be done to have a sort of generic IBAction too. Thanks :-)

– user5273262
Jan 3 at 10:44















IBAction has nothing to do with "generics" or "generic approach", it is still a function, it's just become visible in IB.

– Injectios
Jan 3 at 11:22







IBAction has nothing to do with "generics" or "generic approach", it is still a function, it's just become visible in IB.

– Injectios
Jan 3 at 11:22















@Injectios Not sure what gave the impression that I was implying otherwise. If it helps, English is my second language so it is possible that I may not have been clear enough. What I meant by generic was as in a single method that can be used by multiple objects.

– NSNoob
Jan 3 at 11:24





@Injectios Not sure what gave the impression that I was implying otherwise. If it helps, English is my second language so it is possible that I may not have been clear enough. What I meant by generic was as in a single method that can be used by multiple objects.

– NSNoob
Jan 3 at 11:24













So these objects must be same type/class in IB. Otherwise, if you declare different types/classes then your function is not visible, you can try to use protocols/or inheritance and conform all your view-controllers to protocol where IBAction is declared, never tried though.

– Injectios
Jan 3 at 11:28







So these objects must be same type/class in IB. Otherwise, if you declare different types/classes then your function is not visible, you can try to use protocols/or inheritance and conform all your view-controllers to protocol where IBAction is declared, never tried though.

– Injectios
Jan 3 at 11:28















@Injectios If all those objects inherited from the same parent class then yes, it works. Other types won't be able to access it but then again, in OP's scenario, they don't have to. Only the VCs need to access it. So what would be the whole point? As for your other suggestion, OP will have to provide definitions for the method signatures of the protocol in all the VCs, which is precisely what they want to avoid. You could try the route Sulthan took by creating an extension of UIViewController but as he says, protocol methods cannot be IBActions.

– NSNoob
Jan 3 at 11:35





@Injectios If all those objects inherited from the same parent class then yes, it works. Other types won't be able to access it but then again, in OP's scenario, they don't have to. Only the VCs need to access it. So what would be the whole point? As for your other suggestion, OP will have to provide definitions for the method signatures of the protocol in all the VCs, which is precisely what they want to avoid. You could try the route Sulthan took by creating an extension of UIViewController but as he says, protocol methods cannot be IBActions.

– NSNoob
Jan 3 at 11:35




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019960%2fuiviewcontrollers-sharing-generic-ibaction%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas