appFormation
appFormation

Reputation: 253

UIViewController's view can contain another UIView object?

There are 4 items on MyViewController.xib window:

  1. File's Owner (of type MyViewController)
  2. First Responder (of type UIResponder)
  3. View (of type UIScrollView)
  4. FloatingView (of type UIView)

Both View and FloatingView contain some controls (such as labels and text fields...), and are designed using IB. The FloatingView must NOT occupy the full screen; and my app functionality requires the FloatingView to move to a newly tapped location when user taps on the screen.

An instance variable UIView *myFloatingView; is defined as a property/IBOutlet and synthesized, which is connected to the FloatingView view on IB.

On viewDidLoad method, I am adding myFloatingView to my view controller's view as its subview as:

-(void)viewDidLoad {
    [super viewDidLoad];

    CGRect myFrame = CGRectMake(50, 50, 150, 150); //initial location
    self.myFloatingView.frame = myFrame;
    [self.view addSubview:self.myFloatingView];
}

Apple documentation says each UIViews should have a corresponding view controller. However, I am using only one view controller here. I am treating the floating UIView object as a regular control, or say as a panel. I might in future allow user to hide/unhide this floating view.

Is this a good practice, or is there a better way (any example) to do what I am trying to do?

Thank you very much for your help.

Upvotes: 0

Views: 245

Answers (1)

D.C.
D.C.

Reputation: 15588

What you are doing is fine. In fact, a viewController can often control a whole hierarchy. Also, you said your floatingView was in the same nib as your root view? If so, you can probably make it a subview there, instead of adding as a subview in viewdidLoad.

Upvotes: 1

Related Questions