菜鸟教程小白 发表于 2022-12-12 08:52:11

ios - 在另一个 XIB 中指定类时如何使用 Custom View.xib 加载自定义 View ?


                                            <p><p>我使用 CustomView.xib 文件创建 CustomView:UIView。
现在我想通过将 View 拖到另一个 XIB(例如:UIViewController.xib)并选择类:customView 来使用它。</p>

<p>我初始化CustomView并将SubView添加到另一个 View 时可以加载成功:</p>

<pre><code>- (void)viewDidLoad{
    //Success load NIB
    CustomView *aView = [ initWithFrame:CGRectMake(40, 250, 100, 100)];
    ;
}
</code></pre>

<p>//自定义 View .m</p>

<pre><code>- (id)initWithFrame:(CGRect)frame
{
    self = ;
    if (self) {
      NSLog(@&#34;INIT&#34;);
      NSArray *nib = [ loadNibNamed:@&#34;CustomCell&#34; owner:self options:nil];
      [ setFrame:frame];
      self = ;
    }
    return self;
}
</code></pre>

<p>在这种情况下,通过将 CustomCell 绘制到另一个 XIB 中来重用它,然后将类指定为 CustomView。我知道 awakeFromNib 被调用,但不知道如何加载 CustomView.xib。
该怎么做?</p>

<p>*编辑:</p>

<p>initWithCoder 在指定类时也会被调用,但它会创建一个循环并与 loadNibNamed 一起崩溃。为什么会这样?</p>

<pre><code>- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = ) {
      NSLog(@&#34;Coder&#34;);
      NSArray *nib = [ loadNibNamed:@&#34;QSKey&#34; owner:nil options:nil];
      [ setFrame:self.bounds];
      self = ;
    }
    return self;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在您的 ViewController 中直接拖动自定义 xib 是不可能的。但是您可以做一件事,拖动其父类(super class) View 并将其类设置为您的自定义类。并将其属性连接到您将根据您的自定义放置的对象类。</p>

<p>您可以使用以下代码直接加载您的自定义 xib:</p>

<p>//使用代码加载xib ....</p>

<pre><code>   QSKey *keyView=[ init];
   NSArray *topObjects=[ loadNibNamed:@&#34;QSKey&#34; owner:self options:nil];

for (id view in topObjects) {
    if (]) {
      keyView=(QSKey*)view;
    }
}

keyView.label.text=@&#34;CView&#34;;
;
];
;

here label is UILabel object you have used in your custom class as its property.


//Load custom View using drag down objects of type Custom Class Super class.

for (QSKey *aView in self.view.subviews) {
    if (]) {
      ;
      aView.label.text=@&#34;whatever!&#34;;
    }
}

Here label is object of UILabel you have to place on your dragged View to view controller&#39;s xib view.Change that view&#39;s class to your Custom Class.Then it will will show its label property in outlet connect it to your UILabel object placed over dragged view.
</code></pre>

<p>这是一个工作代码 <a href="https://github.com/ipreencekmr/TestTouchOnView" rel="noreferrer noopener nofollow">TestTouchonView</a> </p>

<p>屏幕截图将显示如下。</p>

<p> <img src="/image/CA047.png" alt="enter image description here"/> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在另一个 XIB 中指定类时如何使用 Custom View.xib 加载自定义 View ?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/14954523/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/14954523/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在另一个 XIB 中指定类时如何使用 Custom View.xib 加载自定义 View ?