Unhide buttons in VC1 from VC2 using delegate

2020-05-06 11:48发布

I am trying to Unhide some buttons in a ViewControl using a button in a secondary VC.

On my researches I found out that I have to use a "Delegation action".

I have created two classes named VC1 -> VC2

enter image description here

VC1.h contains:

#import <UIKit/UIKit.h>

@protocol CustomDelegate <NSObject>
-(void)hideUnhidebutton:(BOOL)value;
@end

@interface VC1 : NSObject <CustomDelegate>



@property (strong, nonatomic) IBOutlet UIButton *buttonToUnhide;

@end

in VC1.m I have implemented the function that unhide the button:

#import "VC1.h"

@interface VC1 ()

@end

@implementation VC1

-(void)hideUnhidebutton:(BOOL)value
{
    [self.buttonToUnhide setHidden:value];

}

After this I have added add delegate variable as property in VC2.h

#import <UIKit/UIKit.h>
#import "VC1.h"


@interface VC2 : UIViewController

@property (nonatomic, strong) id<CustomDelegate> delegatePpty;

@end

And in the end I called the delegate function in VC2.m

#import "VC2.h"


@interface VC2 ()

@end

@implementation VC2


-(void)someAction
{
    [self.delegatePpty hideUnhidebutton:NO];//Call the delegate method to execute
}

there are no issue but when I try to launch the project it just crash after loading showing this issue:

enter image description here

Here the project file:

http://salvonostrato.com//ex/xcode5/TEST2.zip

I am not sure what to do next... please help.

//EDITED

IT keeps crashing showing:

enter image description here

2条回答
冷血范
2楼-- · 2020-05-06 12:08

Your VC1 should extend UIViewController.

@interface VC1 : UIViewController <CustomDelegate>

Make your connections again in the interface builder after that.

查看更多
贪生不怕死
3楼-- · 2020-05-06 12:08

Hi i review your code & i'll changes some things. i'll share this one . please check it

Replace @interface VC1 : UIViewController <CustomDelegate> instead of @interface VC1 : NSObject <CustomDelegate>

And add a navigation controller in story board like below image enter image description here

Now its running perfectly :)

查看更多
登录 后发表回答