Search code examples
iosobjective-cibaction

Work with CustomViewController as top bar of views


I have made a class with xib file called TopHeaderViewController now i use another class named iShared that as a Shared instance method. In iShared i have a @property called topHeader now when i first call sharedInstance on iShared i call: topHeader = [[TopHeaderViewController alloc]init]; and every time i navigate through another viewController i put this view on the top of the view controller. The view appear correctly but the button doesn't fire event.

Code for viewController:

- (void)viewDidLoad {
[super viewDidLoad];
ishared = [iShared sharedInstance];
// Do any additional setup after loading the view.
UIView *v = [ishared initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, headerView.frame.size.height)];

[headerView addSubview:v];

}

Code for iShared:

- (id)init
{
self = [super init];

topHeaderVC = [[TopHeaderViewController alloc]init];

}

code for TopHeaderViewController:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
mainMenuController =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"mainMenuController"];
mainMenuController.superViewController = superViewController;
mainMenuController.view.frame = CGRectMake(0, self.view.frame.size.height, mainMenuController.view.frame.size.width, mainMenuController.view.frame.size.height);
[topView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"sfondo_nero_sfumato"]]];
[topView setFrame:CGRectMake(topView.frame.origin.x , topView.frame.origin.y, superViewController.view.frame.size.width, topView.frame.size.height)];
ishared = [iShared sharedInstance];
[self aggiornaTicketNotificationValue_betCoupon:ishared.betCoupon];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveTestNotification:)
                                             name:NOTIFICATION_AGGIORNA_TOPHEADER_COUPON_LABEL
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveTestNotification:)
                                             name:NOTIFICATION_AGGIORNA_TOPHEADER_ACCOUNT_DETAIL
                                           object:nil];
}
- (IBAction)mnClick:(id)sender {
iShared *ishared = [iShared sharedInstance];
if (![ishared menuAperto]){
    [UIView animateWithDuration:0.5
                          delay: 0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         mainMenuController.view.alpha = 1.0;
                     }
                     completion:nil];
    [UIView commitAnimations];
    [superViewController.view addSubview:mainMenuController.view];
    ishared.menuAperto = true;
}else{
    [UIView animateWithDuration:0.5
                          delay: 0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         mainMenuController.view.alpha = 0;
                     }
                     completion:nil];
    [UIView commitAnimations];
    [mainMenuController.view removeFromSuperview];

    ishared.menuAperto = false;
}

}

anyone knows why i'm getting this trouble to get my button work?

Thanks


Solution

  • The problem is that what you're doing is totally illegal and incoherent. You cannot just shove a view controller's view into your interface. View controllers have a structural hierarchy that parallels their views, and you are completely disregarding that. The result is that you end up with the view without the view controller.

    So what should you do? Well, we can look at it from two directions. One is that you might want to know how to insert a view controller's view correctly into your interface. The answer is that you have to make that view controller a child of your current view controller, and do a careful "custom container controller" dance, as I describe here.

    However, I would suggest that that is not what you want to do here. The fact is, it seems to me, that your situation is just an inappropriate use of a view controller. You don't need a view controller here at all. You are just using the view controller in effect to go "dumpster diving" to retrieve the view designed the xib file. But you do not need a view controller to do that! You can do everything you want to do here just using a xib file and view subclasses that embody the desired functionality.