简体   繁体   中英

access UIView In UIViewController Storyboard

I want to ask, who the Views in the Storyboard, which are attached to a UIViewController are accessable. Who to add them to the UIViewContoller to its views programmaticliy with Objective C. The appear in the Storyboard like this:

and are in the tree in the same hirgachy as the UIViewController Node.

You can take outlet of that view in respective view controller class as you you take outlet of view put in viewcontroller's default view.

Then in your viewDidload you can add that view to your default view!

For example your outlet is outterView then in viewDidload,

  [self.view addSubview:self.outerView];

Second thing if you are adding view in viewDidload and you need your view's size as screen size than in viewDidappear you can do like,

  self.outerView.frame = self.view.frame;

Ok it was my fault, sorry folks. I also need an IBOutlet to the to the ViewControllers view. So connect the them in InterfaceBuilder first and give the View the the customClass.

Referencing Outlets view->UIViewContoller

HelloUIClass *viewThis = [[HelloUIClass alloc] init];

[self.view addSubview:viewThis]

…this is a start not sure about that.

you said you have class of that view so you can do like this

Suppose your class name is View then, 1) in storyboard give name "View1" of class to view 2) you need to create Class if you want to give size programatically 3) for view size you can also use constraints instead of frame.


View1 *objView = [[View1 alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
viewObj.center = self.view.center;
viewObj.backgroundColor = [UIColor redColor];//so you can find view easily
[self.view addSubview: objView];

You just have to do it right in Interface Builder! DRAG AND DROP! the Reference Outlet in to your Headerfile under the @interface. Open both windows. The Storyboard and your Controller Class .h file. Grap an REFERENCING OUTLET from the View in File Inspector or right Mouse click and draw line into your Sourcecode. If you have done your class properly it will hook under your @interface line. AfterDroping you have give it a name "myViewInIB" and than you have just something like this:

@interface UIMainView : UIViewController;
@property (weak, nonatomic) IBOutlet UICoustomView *myViewInIB; 

than you can use it normaly in your Class (Obj C)

[self.view addSubview:self.myViewInIB]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM