• <legend id='COD6v'><style id='COD6v'><dir id='COD6v'><q id='COD6v'></q></dir></style></legend>
      <bdo id='COD6v'></bdo><ul id='COD6v'></ul>

  • <tfoot id='COD6v'></tfoot>
  • <i id='COD6v'><tr id='COD6v'><dt id='COD6v'><q id='COD6v'><span id='COD6v'><b id='COD6v'><form id='COD6v'><ins id='COD6v'></ins><ul id='COD6v'></ul><sub id='COD6v'></sub></form><legend id='COD6v'></legend><bdo id='COD6v'><pre id='COD6v'><center id='COD6v'></center></pre></bdo></b><th id='COD6v'></th></span></q></dt></tr></i><div id='COD6v'><tfoot id='COD6v'></tfoot><dl id='COD6v'><fieldset id='COD6v'></fieldset></dl></div>

      <small id='COD6v'></small><noframes id='COD6v'>

      1. UIButton - alloc initWithFrame: 与 buttonWithType:

        时间:2023-07-08
          • <tfoot id='Pvhk8'></tfoot>
            <legend id='Pvhk8'><style id='Pvhk8'><dir id='Pvhk8'><q id='Pvhk8'></q></dir></style></legend>

              <small id='Pvhk8'></small><noframes id='Pvhk8'>

              • <bdo id='Pvhk8'></bdo><ul id='Pvhk8'></ul>
                <i id='Pvhk8'><tr id='Pvhk8'><dt id='Pvhk8'><q id='Pvhk8'><span id='Pvhk8'><b id='Pvhk8'><form id='Pvhk8'><ins id='Pvhk8'></ins><ul id='Pvhk8'></ul><sub id='Pvhk8'></sub></form><legend id='Pvhk8'></legend><bdo id='Pvhk8'><pre id='Pvhk8'><center id='Pvhk8'></center></pre></bdo></b><th id='Pvhk8'></th></span></q></dt></tr></i><div id='Pvhk8'><tfoot id='Pvhk8'></tfoot><dl id='Pvhk8'><fieldset id='Pvhk8'></fieldset></dl></div>
                    <tbody id='Pvhk8'></tbody>
                • 本文介绍了UIButton - alloc initWithFrame: 与 buttonWithType:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  给定(任意):

                  CGRect frame = CGRectMake(0.0f, 0.0f, 100.0f, 30.0f);
                  

                  以下两个代码片段有什么区别?

                  What's the difference between the following two code snippets?

                  1.

                      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                      button.frame = frame;
                  

                  2.

                      UIButton *button = [[[UIButton alloc] initWithFrame:frame] autorelease];
                  

                  推荐答案

                  我认为它们是等价的.哈哈!诡计问题你这个鬼鬼祟祟的小混混!

                  I think they're equivalent. Haha! Trick question you sneaky little punk!

                  1. -buttonWithType: 返回一个自动释放的 UIButton 对象.

                  1. -buttonWithType: returns an autoreleased UIButton object.

                  +[NSObject alloc]默认标量实例变量为0,所以buttonType应该是0,或 UIButtonTypeCustom.

                  +[NSObject alloc] defaults scalar instance variables to 0, so buttonType should be 0, or UIButtonTypeCustom.

                  优点 &缺点

                  1. 您可能会争辩说,使用 -buttonWithType: 并明确设置 buttonType 会更清晰,并且在 Apple 更改 UIButtonTypeCustom1 而不是 0 (这绝对不会发生).

                  1. You could argue that it's clearer to use -buttonWithType: and set buttonType explicitly and that it's safer in case Apple changes UIButtonTypeCustom to be 1 instead of 0 (which will most certainly never happen).

                  另一方面,你也可以争辩说它很清楚 &使用 -initWithFrame 足够安全.此外,还有许多 Xcode 示例项目,例如TheElements"和BubbleLevel"使用这种方法.一个优点是您可以在应用程序主线程的运行循环耗尽其自动释放池之前显式释放 UIButton.而且,这就是为什么我更喜欢选项 2.

                  On the other hand, you could also argue that it's clear & safe enough to use -initWithFrame. Plus, many of the Xcode sample projects, such as "TheElements" & "BubbleLevel," use this approach. One advantage is that you can explicitly release the UIButton before the run loop for your application's main thread has drained its autorelease pool. And, that's why I prefer option 2.

                  这篇关于UIButton - alloc initWithFrame: 与 buttonWithType:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:当另一个按钮被隐藏时使一个按钮居中的约束 下一篇:将按钮添加到 UIPickerView - Swift 1.2

                  相关文章

                  <tfoot id='bMN0a'></tfoot>
                    <bdo id='bMN0a'></bdo><ul id='bMN0a'></ul>

                    <small id='bMN0a'></small><noframes id='bMN0a'>

                      <i id='bMN0a'><tr id='bMN0a'><dt id='bMN0a'><q id='bMN0a'><span id='bMN0a'><b id='bMN0a'><form id='bMN0a'><ins id='bMN0a'></ins><ul id='bMN0a'></ul><sub id='bMN0a'></sub></form><legend id='bMN0a'></legend><bdo id='bMN0a'><pre id='bMN0a'><center id='bMN0a'></center></pre></bdo></b><th id='bMN0a'></th></span></q></dt></tr></i><div id='bMN0a'><tfoot id='bMN0a'></tfoot><dl id='bMN0a'><fieldset id='bMN0a'></fieldset></dl></div>
                    1. <legend id='bMN0a'><style id='bMN0a'><dir id='bMN0a'><q id='bMN0a'></q></dir></style></legend>