iOS-Parent-Child Controller

Interface:

Why do I need to use a parent-child controller?

Click the Social-Headline-Hot Spot button:
    1. The corresponding controller needs to be created each time
    2. The view of the corresponding controller is added each time
    3. The corresponding controller cannot process business logic and the controller is destroyed
 Note:
    Why is the viewController not there (because the viewController was destroyed it cannot process business logic)?
    1. view with controller added only does not add controller, controller without strong reference destroyed

Solution:
1. Set strong references in the top-level ViewController (strongly discouraged)
2. Use the parent-child controller (UIViewController manages the child UIViewController)

ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    //Add Child Controller to Parent Controller
    // Sociology
    ScoietyViewController *scoietyVc = [[ScoietyViewController alloc] init];
    scoietyVc.title = @"Sociology";
    [self addChildViewController:scoietyVc];

    // Headlines
    TopLineViewController *topLineVc = [[TopLineViewController alloc] init];
    topLineVc.title = @"Headlines";
    [self addChildViewController:topLineVc];

    // Hotspot
    HotViewController *hotVc = [[HotViewController alloc] init];
    hotVc.title = @"Hotspot";
    [self addChildViewController:hotVc];

    //Set button contents
    NSInteger count = self.titleContainView.subviews.count;
    for (int i = 0; i < count; i++) {
        UIButton *btn = self.titleContainView.subviews[i];
        //Get the current controller object
        UIViewController *vc = self.childViewControllers[i];
        [btn setTitle:vc.title forState:UIControlStateNormal];
    }
}


// Click the Title button
- (IBAction)showChildVcView:(UIButton *)sender {

    // Remove view from previous controller
    [self.containView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    for (UIView *vcView in self.containView.subviews) {
//        [vcView removeFromSuperview];
//    }

    // Add a view to the corresponding subcontroller
    UIViewController *vc = self.childViewControllers[sender.tag];
    vc.view.backgroundColor = sender.backgroundColor;
    vc.view.frame = self.containView.bounds;
    [self.containView addSubview:vc.view];
}

push

Interface:

ViewController.m
@implementation ViewController
// UIViewController is also able to manage subcontrollers
- (void)viewDidLoad {
    [super viewDidLoad];

    // Add a subcontroller
    ChildViewController *childVc = [[ChildViewController alloc] init];
    childVc.view.backgroundColor = [UIColor greenColor];
    childVc.view.frame = CGRectMake(50, 50, 200, 200);
    [self.view addSubview:childVc.view];
    [self addChildViewController:childVc];   
}
@end
ChildViewController.m
@implementation ChildViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Clicked on the subcontroller's view %@",self.navigationController);
    //Clicked on the view of the subcontroller <UINavigationController: 0x7f8adc029a00>

    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor redColor];
    /* Get Navigation Controller
    1.First, determine if you are a sub-controller of the navigation controller.
    2.No, determine if the parent controller is a child of the navigation controller.
    3.No, until there is no parent controller
    */
    [self.navigationController pushViewController:vc animated:YES];

}
@end

modal

Interface:

ViewController.m
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    ModalViewController *vc = [[ModalViewController alloc] init];
    vc.view.backgroundColor = [UIColor redColor];
    [self presentViewController:vc animated:YES completion:nil];
}

ModalViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];

    ChildViewController *childVc = [[ChildViewController alloc] init];
    childVc.view.backgroundColor = [UIColor blueColor];
    childVc.view.frame = CGRectMake(50, 50, 200, 200);
    [self.view addSubview:childVc.view];
    [self addChildViewController:childVc];
}

ChildViewController.m
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Click on the subcontroller's view");
    // Determine if the current method caller is modal,
    //If not, determine if the parent controller is modal
    [self dismissViewControllerAnimated:YES completion:nil];
}

Posted by fleabel on Sat, 04 Jan 2020 00:48:09 -0800