【问题标题】:Custom Delegates For UIViewController Having ContainerView具有 ContainerView 的 UIViewController 的自定义委托
【发布时间】:2017-04-09 02:05:52
【问题描述】:

我希望所有人都做得很好,享受愉快的编码生活。

我想在 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(ChileView)

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 按钮,我希望代理工作。

请帮我运行这个委托。 提前致谢。

【问题讨论】:

    标签: ios objective-c uiviewcontroller delegates uicontainerview


    【解决方案1】:
    ChatActionsViewController *chatActions = [[ChatActionsViewController alloc] initWithNibName:@"ChatActionsViewController" bundle:nil];
    chatActions.delegate = self;
    

    您创建了该类的一个新实例,但从未使用它。

    containerViewActions
    

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

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

    【讨论】:

    • 感谢您的回答。
    猜你喜欢
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多