简体   繁体   中英

use a custom uiview inside a storyboard

So, I am using storyboard and I have dragged inside my UIViewController a UIView .

Let's call it customView, and it's class is called SPView . The class is set in the inspector window. Inside the SPView.h there are a number of properties

I have tried the following :

  1. If I drag a label inside the UIView (in the storyboard ), the label is shown, but I cannot connect it to one of my outlets in the SPView.h by drag and drop.

  2. If I create a new XIB file, with the label inside, I can do the connections as I like.

Then inside my UIViewController I have tried either of these:

self.customView =[[SPView alloc] initWithFrame:CGRectMake(60, 60, 200, 260)]
self.customView =[[SPView alloc]init];

and inside my SPView, if I use this:

 - (id)initWithCoder:(NSCoder *)aDecoder {
 NSLog(@"initWitchCoder called");
 if ((self = [super initWithCoder:aDecoder])) {
 //[self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"SPView" owner:self     options:nil] objectAtIndex:0]];
 [self baseInit];
 }

the label is not shown.

If I uncomment the comment, the initWithCoder is called for ever and the app eventually crashes.

What I want is :

to have a custom UIView inside a UIViewController , set either in the storyboard or programmatically (but it will be better if the graphic data are set in storyboard or in a separate .XIB file so as to inspect them more easily).

Can you help me on that?

You can have following method defined in your UIViewController class and have an XIB created for SPView.

- (SPView*) loadCustomViewFromNib
{
  SPView *spView = nil;

  NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"SPView" 
                                               owner:nil 
                                             options:nil];
  for (id object in array)
  {
    if ([object isKindOfClass:[SPView class]])
    {
        spView = (SPView*)object;
        break;
    }
 }

 return spView;
}

And have SPView initialized as shown below

self.customView = [self loadCustomViewFromNib];

you cannot drag outlets to custom class uiview class . only its allowed when using xib. you can drop outlets of that custom view in its parent viewcontroller only. you can set tags in subviews of your custom view. and can easily access them by using this below code . lets say you have a subview in your custom view with tag :2

- (id)initWithCoder:(NSCoder *)aDecoder {
 NSLog(@"initWitchCoder called");
 if ((self = [super initWithCoder:aDecoder])) {
 //[self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"SPView" owner:self     options:nil] objectAtIndex:0]];
        UILabel *lbl  = (UILabel *)[self viewWithTag:2];
 [self baseInit];
 }

so you had to set tags to subviews to access them

in your uiviewcontroller you had to drop outlet directly . no need for this like this it will be initialized automatically

@property (weak, nonatomic) IBOutlet SPView *customView; //right and easy way

X    self.customView =[[SPView alloc] initWithFrame:CGRectMake(60, 60, 200, 260)]
X    self.customView =[[SPView alloc]init];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM