user968597
user968597

Reputation: 1156

How to make UIView global with button actions

I have an application with 10 different viewcontrollers in navigation controller. And i have UIView which i added subview to keyWindow of sharedApplication instance.

                 UIWindow *window = [UIApplication sharedApplication].keyWindow;
                 UIView *myView = [UIView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.width - 80,80,90)];

                [window addSubView:myView];


 UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

    btn1.frame = CGRectMake(xPos, prevVideoUIView.frame.size.height-
    [btn1 addTarget:self action:@selector(button1Clicked:) forControlEvents:UIControlEventTouchUpInside];
    btn1.hidden = NO;
    [myView addSubview:btn1];

  UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];

    btn2.frame = CGRectMake(xPos, prevVideoUIView.frame.size.height-
    [btn2 addTarget:self action:@selector(button2Clicked:) forControlEvents:UIControlEventTouchUpInside];
    btn2.hidden = NO;
    [myView addSubview:btn2];

I want the access of myView throughtOut the application wherever I navigate and on action user can put this view on screen at any point of time. The issue is suppose user navigates to third level of navigation controller and from there user added this "myView" on window as subview. and comes back to prrevious view then on touching the buttons on myView. app crashes saying bad access.

please do reply to this thread.

Regards,

parag

Upvotes: 0

Views: 489

Answers (1)

Salman Zaidi
Salman Zaidi

Reputation: 9842

Whenever you add your desired view in any of your view controller, when that view controller dismisses/pops, your view will also be released with that view controller. That is the reason of receiving Bad Access Exception.

You can create a class of type UIView and make that class Singleton. Once initialised, you can then add or remove your view from any of your view controller. By using Singleton Pattern, your view will be global and static.

You can also create your view in your AppDelegate class and by getting app delegate object in any of your view controller, you can add or remove your view.

Upvotes: 1

Related Questions