开发者

.Net UserControl XCode equivalent

开发者 https://www.devze.com 2023-04-11 12:15 出处:网络
I\'m used to create \"user controls\" in Visual Studio... and i was wondering if there 开发者_Go百科is a way to do that in XCode...

I'm used to create "user controls" in Visual Studio... and i was wondering if there 开发者_Go百科is a way to do that in XCode...

I've been looking around and some people was telling me to build a plugin... that sound rather too complicated to build a simple reusable "text box -> button -> label"... In VS i can just create -> User Control, program and drag/drop the control as if it was a simple button... and it really is as simple as that...

Any ideas?


Either a XIB (interface builder view) or a class that inherits from UIView are equivalent to a user control. They are ways to aggregate sub views (think views and sub-views - not controls).

Inside the XIB in interface builder, you can add other controls (drag from the library) and place in the view. File, New File, under iOS, User Interface, View. This SO question covers that:

How do I associate a nib (.xib) file with a UIView?

If you want to do it programmatically, create a new objective-c class that inherits from UIView. In viewDidLoad, you can programmatically create other controls and place them.

Here's a programmatic tutorial:

http://www.raywenderlich.com/1768/how-to-make-a-custom-uiview-a-5-star-rating-view

Finally, a common type of 'aggregate control' is a UITableViewCell for use in tables. Here's a tutorial on creating custom UITableView cells both programmatically and with IB.

http://www.e-string.com/content/custom-uitableviewcells-interface-builder

These guides from Apple are good to read:

http://developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/Conceptual/ViewPG_iPhoneOS/Introduction/Introduction.html

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html

EDIT:

I just tried this ...

Add a XIB to your project as described above. Add a few controls to it that you wish to aggregate as a re-usable view.

Now - in one of your other Views/view controllers drag out a UIView to claim the space where your aggregate control will be used.

Create an IBOutlet UIView *_customView and in your view, bind that outlet to that UIview you just dragged out. This is just a container at this point and you have an iVar/IBOutlet so you can add things to it.

Up to now, we've create a UIView layout which will contain our aggregate control.

Use the UINib class to stamp out your Xib 'usercontrol'. Here's the code I put in viewDidLoad:

UINib *nib = [UINib nibWithNibName:@"TestView" bundle:nil];
[_customView addSubview:[[nib instantiateWithOwner:self options:nil] objectAtIndex:0]];

Not exactly just dragging out but realise the UINib caches the layouts so you can quickly stamp out new instances and add it to your form wherever you want while leveraging the layout control of IB with the UIview.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号