Muhammad Arslan Muhammad Arslan - 11 days ago 7
Objective-C Question

Custom Delegates For UIViewController Having ContainerView

I hope all are doing well and enjoying happy coding life.

I want to use delegates of ChlidView in ParentView.

ChatHomeViewController(ParentView)

enter image description here

ChatHomeViewController.h

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

@interface ChatHomeViewController:UIViewController<UITableViewDataSource,UITableViewDelegate, ChatActionsViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableViewChatHome;

@property (weak, nonatomic) IBOutlet UIButton *cmdBackMenu;


@property (weak, nonatomic) IBOutlet UIView *containerViewActions;
@property (weak, nonatomic) IBOutlet UIButton *cmdShowActions;
@property (weak, nonatomic) IBOutlet UIView *viewHeadin;

@end


ChatHomeViewController.m

@interface ChatHomeViewController ()

@property (strong, nonatomic) NSMutableArray *tableData;

@end

@implementation ChatHomeViewController
@synthesize tableViewChatHome;
@synthesize cmdShowActions;
@synthesize containerViewActions;

- (void)viewDidLoad {
[super viewDidLoad];
[self setNeedsStatusBarAppearanceUpdate];

[cmdShowActions addTarget:self action:@selector(displayChatActions) forControlEvents:UIControlEventTouchUpInside];

[containerViewActions setHidden:YES];

}

-(void)displayChatActions{

ChatActionsViewController *chatActions = [[ChatActionsViewController alloc] initWithNibName:@"ChatActionsViewController" bundle:nil];
chatActions.delegate = self;

[containerViewActions setHidden:NO];
}

#pragma mark - ChatActions Delegates
-(void)creatNewChat:(NSString *)newChatOrGroup{

NSLog(@"creatNewChat:(NSString *)newChatOrGroup");
[containerViewActions setHidden:YES];

}


ChatActionsViewController(ChileView)

enter image description here

ChatActionsViewController.h
#import

@protocol ChatActionsViewControllerDelegate <NSObject>

@optional
-(void)creatNewChat: (NSString *)newChatOrGroup;

@end

@interface ChatActionsViewController : UIViewController

@property (assign,nonatomic) id<ChatActionsViewControllerDelegate> delegate;

@property (weak, nonatomic) IBOutlet UIView *viewBG1;
@property (weak, nonatomic) IBOutlet UIView *viewBG2;

@property (weak, nonatomic) IBOutlet UIButton *cmdCreatNewGroup;
@property (weak, nonatomic) IBOutlet UIButton *cmdCreatNewChat;

- (IBAction)actionNewChat:(id)sender;
- (IBAction)actionNewGroup:(id)sender;

@end


ChatActionsViewController.m

#import "ChatActionsViewController.h"

@interface ChatActionsViewController ()

@end

@implementation ChatActionsViewController
@synthesize viewBG1, viewBG2;
@synthesize cmdCreatNewChat, cmdCreatNewGroup;
@synthesize delegate;

- (void)viewDidLoad {
[super viewDidLoad];

[[viewBG1 layer] setCornerRadius:4];
[[viewBG1 layer] setBorderColor:[UIColor lightTextColor].CGColor];
[[viewBG1 layer] setBorderWidth:1];
viewBG1.layer.masksToBounds = NO;
viewBG1.layer.shadowOffset = CGSizeMake(1, 1);
viewBG1.layer.shadowRadius = 3;
viewBG1.layer.shadowOpacity = 0.2;

[[viewBG2 layer] setCornerRadius:4];
[[viewBG2 layer] setBorderColor:[UIColor lightTextColor].CGColor];
[[viewBG2 layer] setBorderWidth:1];
viewBG2.layer.masksToBounds = NO;
viewBG2.layer.shadowOffset = CGSizeMake(1, 1);
viewBG2.layer.shadowRadius = 3;
viewBG2.layer.shadowOpacity = 0.2;

}

- (IBAction)actionNewChat:(id)sender {

NSLog(@"actionNewChat");
[[self delegate] creatNewChat:@"1"];
}

- (IBAction)actionNewGroup:(id)sender {

NSLog(@"actionNewGroup");
[[self delegate] creatNewChat:@"2"];
}
@end


The issue I am facing is delegate is not working propoerly. Initially, the containerView is hidden. When user click "+" Button then containerView becomes visible.
I want delegate worked if I click on New Chat or New Group Buttons.

Please help me running this delegate.
Thanks in advance.

Answer
ChatActionsViewController *chatActions = [[ChatActionsViewController alloc] initWithNibName:@"ChatActionsViewController" bundle:nil];
chatActions.delegate = self;

You create a new instance of the class but don't ever use it.

containerViewActions

This embedded view controller has another instance of the class that is loaded from the storyboard. You should give the embed segue an identifier in Interface builder and then use - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;

in the parent view controller. You can find the segue with [segue identifier] and store a pointer to the destination view controller from there.

Comments