UIViewController的自定义代理具有ContainerView

时间:2016-11-24 17:28:05

标签: ios objective-c uiviewcontroller delegates uicontainerview

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

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

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

我面临的问题是委托不是正常工作。最初,隐藏了containerView。当用户单击“+”按钮时,ContainerView变为可见。 如果我点击新聊天新群组按钮,我希望代表工作。

请帮助我运行此代表。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

您创建了该课程的新实例,但无法使用它。

containerViewActions

此嵌入式视图控制器具有从故事板加载的类的另一个实例。您应该在“接口”构建器中为嵌入设置一个标识符,然后使用- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;

父视图控制器中的

。您可以找到带有[segue identifier]的segue,并从那里存储指向目标视图控制器的指针。