2

I'm new in iOS Programming, and I had this problem. Let's say I have these two function:

class BaseViewController: UIViewController, ErrorMessageDelegate {
    var uiView =  UIView();
    var viewErrorMessage:ErrorMessage!

    func refresh(_sender: AnyObject) {
        print("testing")
    }

    func getErrorMessage(message:String) {
        super.viewDidLoad()
        Dialog.dismiss()
        ErrorMessage.message = message
        viewErrorMessage = Bundle.main.loadNibNamed("ErrorMessage", owner: self, options: nil)?.first as! ErrorMessage
        viewErrorMessage.delegate = self
        self.view.addSubview(viewErrorMessage)

        func removeSubView() {
            viewErrorMessage.removeFromSuperview()
        }
    }
}

I want to call function removeSubView inside function refresh. I had to do that because I need to override refresh function to my subclass. And I need to put the function removeSubView in getErrorMessage because I should to. Does anyone know how to do that?

2
  • Have you tried it by writing the method outside the getErrorMessage method? Commented Apr 22, 2019 at 9:32
  • 1
    This is not Swift only. This is because you don't have access to other scopes. I would suggest moving removeSubView() to an accessible scope inside your BaseViewController(), you can make it private to prevent others from calling it. Commented Apr 22, 2019 at 9:33

3 Answers 3

4

Please refer below code. It will help you to resolve the issue.

Code:

class BaseViewController: UIViewController, ErrorMessageDelegate {
    var uiView =  UIView();
    var viewErrorMessage:ErrorMessage!

    func refresh(_sender: AnyObject) {
        removeSubView()
    }

    func getErrorMessage(message:String) {
        super.viewDidLoad()
        Dialog.dismiss()
        ErrorMessage.message = message
        viewErrorMessage = Bundle.main.loadNibNamed("ErrorMessage", owner: self, options: nil)?.first as! ErrorMessage
        viewErrorMessage.delegate = self
        self.view.addSubview(viewErrorMessage)
    }
    func removeSubView() {
        viewErrorMessage.removeFromSuperview()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Yes, that is possible

func a() {
    c()
}

func b() {
    c()
}

func c() {
    print("hello world")
}

Comments

1

no it is not possible - but I wonder why you would do that?

3 Comments

What does removeSubview() have to do with getErrorMessage()? Nothing. So there is no reason for it to be at the scope of getErrorMessage
viewErrorMessage has to be called inside getErrrorMessage because it added there
@FirdaSahidi Well move it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.