UIViewController: set self.view to my view or add my view as a subtitle?

I have a question about the subview of the UIViewController, I created a subclass of the UIView MainView that has the exact screen size, I wonder which one is the best to use MainView, consider the following factors:

1 Since MainView is the same size as the entire screen, MainView itself can have subviews, but at the save level there are no views as MainView (i.e. I do not need to add other subviews to self.view).

2 If I use self.view = mainView, do I put the code in loadView (since the viewDidLoad method means that view (self.view) is already loaded)? I see that the loadView method is commented out by default, if I add code to this method, what other code do I need to collect (for example, initialize other aspects of the application)?

3 If I add mainView via [self addSubview: mainView], are there really two buffers from the screen? One for self.view, one for mainView, is the same size as the screen, and one overlays on top of the other (so it takes memory)?

Thanks a lot!

+3
source share
5 answers

I'm not sure I fully understand what you are asking, but I will try to answer a few questions that you have.

, UIViews, . -removeFromSuperview , .

UIView UIViewController. :

MainView *mainView = [[MainView alloc] initWithFrame:CGRectMake(320.0, 480.0)];
self.view = mainView;
[mainView release]; //since the .view property is a retained property

-init. :

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
              //initializations
        }
        return self;
}
+2

, xib! UIView xib, ( UIView " " * - "MainView" !)

,

myViewController = [[MainViewController alloc] initWithNibName:@"MyNibName" bundle:nil]; 

; ( self.view), , :)

NB * Click → Identity Inspector. , , !

+1

loadView, NIB.

UIViewController , "" . , :

- (void)loadView
{
    UIView* mainView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    self.view = mainView;
}
+1

, "" , AFAIK, (!) NIB, .. loadView.

, , , , :

UIView *myView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; 
self.view = myView; 
[myView release];

, [myView release], , , self.view( UIView) .

, -dk

0

, - , :

self.view.userInteractionEnabled = YES;

, , self.view . .

0

Source: https://habr.com/ru/post/1717703/


All Articles