[ACCEPTED]-Duplicate, clone or copy UIView-ios
The following category might not be particularly 9 efficient, but worked for me in one project:
@implementation UIView (OPCloning)
- (id) clone {
NSData *archivedViewData = [NSKeyedArchiver archivedDataWithRootObject: self];
id clone = [NSKeyedUnarchiver unarchiveObjectWithData:archivedViewData];
return clone;
}
@end
I'd 8 not implement -copy or -copyWithZone: as 7 Apple might do so in the future. Note, that 6 not all views implement archiving to the 5 same extent. You'll definitely need to implement 4 the NSCoding methods for custom properties 3 of your NSView subclasses to be cloned (will 2 turn to nil in the cloned view, otherwise). Still 1 easier than writing custom cloning code.
Here is a new method you can use: Use UIView's 2 method:
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates
This is the fastest way to draw a 1 view. Available in iOS 7.
You can try with Swift 3.0.1 below:
extension UIView{
func copyView() -> AnyObject{
return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self))! as AnyObject
}
}
0
Sure. The documentation has a good example of how to achieve that; it's for 11 UITableViewCell
, but is a good approach to use here as 10 well.
Depending on the complexity of your 9 view, you might want to make it a custom 8 view class, and give it its own IBOutlet
properties 7 for whatever subviews it has; in that case, you'd 6 set the “Class Identity” of the view in 5 Interface Builder to that class. Then your 4 view controller could access those views 3 on any given XIB-loaded view via, for instance, myLoadedView.someLabel
, rather 2 than having to use, e.g., [myLoadedView viewWithTag:3]
as suggested 1 by the aforelinked documentation.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.