简体   繁体   中英

Setup/instantiation of IB subview with custom class

I have a .xib file for my viewController. It contains a TableView and a UIView. The UIView is backed by a custom class - CommentsBarView (trying to position a small bar with a comments field underneath my tableView).

So in my Document Outline list I have:

  1. view
    • tableView
    • comments bar view
      • UITextView
      • UILabel

Custom class for "comments bar view" is CommentsBarView.

I have outlets connected from within CommentsBarView to the textfield and label. (and from the ViewController to the tableView).

When I load my with controller with:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

I can access the tableView property and change the appearance of the tableVIew, however, from my commentsBarView initWithCoder I can not set the text value on my textView and label:

- (id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {

        [self.commentTextView setText:@"Helo, World!"];  
        [self.characterCountLabel setText:@"200"];
    }

    return self;    
}

It seems as if these properties are not available at initWithCoder time.

If I manually, from my controllers initWithNibName, accesses self.commentsBar.label.text = 200, there is no problem.

Am I experiencing a timing issue where the views are not ready yet or can I not nest a view inside a viewControllers view and have it backed by a custom UIView subclass?

IB is confusing me a bit.

Thanks for any help given.

When loading from a XIB file, the IBOutlets are not ready in the init methods of the objects being unarchived.

You need to override awakeFromNib in your CommentsBarView to have access to the ready and connected IBOutlets.

Once you get used to IB it becomes better. Since Cocoa is a MVC (Model-View-Controller) you should probably not create a UIView subclass and set your UIView to it. You should probably put the UIView back to just a UIView. I generally subclass a UIView if I need to have a customized look. For example; a good time to subclass a UIView is if you want it to have a gradient. Then you can reuse your subclass for any UIView you wish to show the gradient.

In your case you are trying to "control" the UITextView and UILabel. You can instead wire-up outlets of your UITextView and UILabel directly to your UIViewController (File Owner). That is the "controller" of the MVC in this case. Think of your UIView as a container that is simply holding the two controls for this example. Now you can use the viewDidLoad method or some other method of you UIViewController to set the values of you UITextView and UILabel. It is generally the UIVeiwController that interprets the data from the Model in Cocoa and places the data where it needs to be. It is not a rock-solid rule, but a good one.

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