How do I replace a UIView defined in my storyboard before the view is shown?

Discussion in 'iOS Programming' started by moonman239, Aug 14, 2015.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    I have a menu view in my app. Because the layout of the menu is non-uniform and I like being able to use Interface Builder for these items, I have decided to create a UIView subclass called "MenuView" and plaster blank UIViews where the items will go.

    Now, I'd like to know if there's a way to swap each UIView as they're loaded from the UIView.
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    Add outlets for the dummy views into MenuView and connect them up in IB. At run time you'll have access to do what you want, like remove them and add new ones to replace them.

    Alternatively you could just loop through the MenuView subviews and remove them and then add a new varying number of views.
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    Thanks for the suggestion.

    Actually, come to think of it, perhaps I could try overriding addSubview and doing something like this:
    Code:
    -(void)addSubview:(UIView *)view
    {
        if (viewIsCell)
        {
          MenuViewCell *cell = [[MenuViewCell alloc] initWithFrame:[view frame]];
          [super addSubview:cell];
        }
        else
        {
           [super addSubview:view];
        }
    }
    
    However, I have also been thinking about simply going with a UICollectionView and implementing a custom layout. Either way, I'll have to figure out how to make sure the app knows where the data goes.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    You can add your MenuView objects using IB. They just won't look like their real appearance. Add UIViews and then set their custom class to your custom class.
     

Share This Page