disable UIButton with a shared Instace Method

Viewed 71

I am working on this Projekt. My app is able to store some data in my Firebase Database. Once this is done the button to get to this exact ViewController should be blocked by the Button which makes the segue. I don't know where and how to declare the Button so it will be accessible from another ViewController.

My Idea: I just learned to use shared Instances to activate a haptic feedback Generator and trigger it in any place I wanted to give haptic feedback

My Code in my implementation file with :

+ (buttonHandler *)sharedInstace{

   static buttonHandler *_sharedInstace = nil;
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
    _sharedInstace = [[self alloc] init];});
return _sharedInstace;} -(id)init {
self = [super init];
if (self)
{
    [self.MyButtonIWantToDisable setEnabled: NO];
}
return self;}

My Problem: I cant reach the Button to turn it off

Best regards Roman

1 Answers

Read about protocols and delegates. Let's say you have VC1 with the button and VC1 segues to VC2, and you want to disable the button in VC1 when something in VC2 happens.

You can create a protocol (with it's own set of functions) in VC2 that VC1 conforms to, which will allow VC1 to know when it should disable the button.

The process is creating a protocol in the VC2 file, with the functions pertaining to this protocol, creating a weak delegate variable with it's type being the protocol, making VC1 the delegate of VC2, and then it's about implementing the functions that are specified in the protocol. With this way, VC2 can tell it's delegate (VC1) that it should disable the button, and VC1 would disable the button when it conforms to the protocol.

Another option you have is creating and listening to custom notifications.

Related