具有 ContainerView 的 UIViewController 的自定义委托

Custom Delegates For UIViewController Having ContainerView

希望大家一切顺利,享受快乐的编码生活。

我想在 ParentView 中使用 ChlidView 的委托。

ChatHomeViewController(父视图)

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(智利视图)

ChatActionsViewController.h #导入

@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

我面临的问题是委托无法正常工作。最初,containerView 是隐藏的。当用户单击 "+" 按钮时,containerView 将变为可见。 如果我单击 New ChatNew Group 按钮,我希望代理工作。

请帮助我 运行 这位代表。 提前致谢。

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

您创建了 class 的新实例,但从未使用它。

containerViewActions

此嵌入式视图控制器具有从情节提要中加载的 class 的 另一个 实例。您应该在 Interface builder 中给 embed segue 一个标识符,然后使用 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;

在父视图控制器中。您可以使用 [segue identifier] 找到 segue 并从那里存储指向目标视图控制器的指针。