Вопрос

Lets say I have a popup. When a button is tapped the popup has to be allocated and presented. When the close button delegate is called it has to be closed and removed properly. I don't think I am doing this correctly for some reason:

In .h file:

@interface MainViewControoler : UIViewController
{
PopupViewController*            popupView;     
}

@property (retain, nonatomic) PopupViewController* popupView;
@end

In .m file:

..
-(void)openButtonPressed
{
  if (!popupView)
  {  
    popupView = [[PopupViewController alloc] init];
    popupView.delegate = self;
  }
  [self.view addSubview:popupView.view];
  popupView.view.frame = self.view.frame;
}

..
-(void)closePopup
{
  [popupView.view removeFromSuperview];
}

I don't want the this to have any leaks where if the popup is opened and closed 1000 times, it would not crash....Is this the correct way?

Это было полезно?

Решение

Your code does not generate leaks, as you are allocating the popover once. However, you do not need to create an instance variable manually, or to set the popover property to nil, ARC will take care of this, unless you want to also deallocate the popover before your VC is deallocated.

Just make sure you are not adding the popover view multiple times to the parent view (i.e. not calling openButtonPressed again before calling closePopup.

.h:

@interface MainViewControoler : UIViewController

@property (nonatomic) PopupViewController *popupView;

@end

.m:

-(void)openButtonPressed
{
  if (!_popupView)
  {  
    _popupView = [[PopupViewController alloc] init];
    _popupView.delegate = self;
  }
  [self.view addSubview:_popupView.view];
  _popupView.view.frame = self.view.frame;
}

-(void)closePopup
{
  [_popupView.view removeFromSuperview];
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top