Clone/Copy an UI object

Discussion in 'iOS Programming' started by mpramodjain, Nov 17, 2011.

  1. mpramodjain macrumors regular

    Joined:
    Nov 20, 2008
    Location:
    Banglore
    #1
    Can it be possible to clone/copy an UIView object, without using Archivers.

    for a scenario, I have the same kind of view that needs to be shown on the different controllers. So, what I thought was if there is any other way to clone it before initializing then, it would be better to play without creating alloc for each UIView of same replicaa.
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    You could put the UIView into its own .xib, preload the .xib using UINib's nibWithNibName:bundle: and then call instantiateWithOwner:options: as necessary. Would that work for you?
     
  3. mpramodjain thread starter macrumors regular

    Joined:
    Nov 20, 2008
    Location:
    Banglore
    #3
    Jimi,,, thanks for your quick reply..

    Am not using IB.. through programmatically, am creating a UIView and want to use it..
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    UIView doesn't conform to the NSCopying protocol to the copy method inherited from NSObject isn't going to be functional AFAIK.

    It does implement UICoding, so I think using archivers might be the only way.

    However, could you archive once and unarchive multiple times?
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    Look up NSCopying and copyWithZone. If you implement these for your UIView subclass then that should work. Alternatively you can implement a simple utility method that returns a copy of your UIView subclass. It will copy those properties that you think are important. The code would be about the same as an implementation of copyWithZone.
     

Share This Page