Unhide buttons in VC1 from VC2 using delegate

浪子不回头ぞ 提交于 2019-12-20 06:37:33

问题


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

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:

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:


回答1:


Your VC1 should extend UIViewController.

@interface VC1 : UIViewController <CustomDelegate>

Make your connections again in the interface builder after that.




回答2:


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

Now its running perfectly :)



来源:https://stackoverflow.com/questions/23629681/unhide-buttons-in-vc1-from-vc2-using-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!