NSLog printing (null) for superview

Discussion in 'iOS Programming' started by moonman239, Jan 11, 2016.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    I have UILabels that are dragged and dropped by the user from one UIView to another. More specifically:
    -Upon being touched, each such label moves from its parent view to the main view of the view controller, but in didMoveToSuperview a reference is created to the original UIView if the variable is nil.
    -Upon being released, the view controller then decides what to do. If the view controller decides that the label is close enough to where it should now be, it copies the text into a "destination" UIView and then tells the label to go back to where it was originally. Otherwise, the label just goes back to where it was originally.

    Because of a problem my client was having - wherein the UILabel seemed to have been put back I put an NSLog statement in my UILabel subclass's didMoveToSuperview method. That NSLog now seems to be logging (null) for the superview.

    Here's the code:
    Code:
    -(void)didMoveToSuperview
    
    {
    
        NSLog(@"Label with text %@ moved to superview %@",[selftext],[selfsuperview]);
    
        if (originalSuperview == nil) {
    
            // Label is going to be dragged to another location.
    
            [selfsetUserInteractionEnabled:true];
    
            originalSuperview = [selfsuperview];
    
        }
    
    }
    
     
  2. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #2
    I think we're going to need the to and from view code.

    So your moving a label to from one view to another view. When it gets to the destination view, it has all the information but it gets lost (nil) when it returns to the source view?

    Can you also show the code where the label gets assigned it's data in the source?
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    This behavior occurs whenever the label pops back up to the top. You see, I've overridden removeFromSuperview as a convenience for myself. When the label is told to remove itself from the superview, it goes back to the original superview, but not before calling the superclass method (which, I now think, is a dumb idea).
     
  4. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #4
    Ok so the problem is upon return from the source.
    So:
    1. you remove the label from super
    2. you add the label to sub
    3. you return the label to super
    The problem is when you return to super, you have nil.

    Can you post the code for both super and sub?

    It looks like it might be a scope issue. How are you storing the data?
    What about weak vs strong, could it be losing the reference and thereby returning to nil?
    Can you nslog it out before returning to super?
     
  5. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #5
    I remember a tutorial about passing data from one view to another and it looked involved. I don't remember all the details but it looked like a pain just to move data around. Maybe that's the problem here. Someone posted about it last year, seems there was an agreement about the method unless you wanted to store data external in a dictionary or file.

    I don't know if this applies to what you're doing or not. Maybe someone remembers that thread about moving data around different objects.
     
  6. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #6
    If you don't mind, can you post a follow up once you figure out the issue. It's a good reference for other people that search these threads.
    Thanks.
     

Share This Page