菜鸟教程小白 发表于 2022-12-11 22:27:40

objective-c - drawRect 查看空数组


                                            <p><p>drawtestViewController.m 中的按钮链接到 ChalkBoard.m 中的 pushPointAction 。它用 CGpoint 对象填充 pointArray。这个数组到处都是对象,除非从 drawRect 中查看。似乎从 drawRect 我以某种方式引用了原始数组的空副本,但我找不到任何错误...</p>

<p>我已将整个项目上传至 <a href="http://dl.dropbox.com/u/42099382/drawtest.zip" rel="noreferrer noopener nofollow">http://dl.dropbox.com/u/42099382/drawtest.zip</a> </p>

<hr/>

<p>drawtestViewController.m 中的按钮操作</p>

<pre><code>- (IBAction)myDo {
   NSLog(@&#34;-Button was pressed&#34;);   
;
;
;
}
</code></pre>

<hr/>

<pre><code>//ChalkBoard.h
#import &lt;UIKit/UIKit.h&gt;

@interface ChalkBoard : UIView
-(void)pushPoint:(CGPoint)point;
@property (nonatomic,retain) NSMutableArray *pointArray;
@end
</code></pre>

<hr/>

<pre><code>//ChalkBoard.m
#import &#34;ChalkBoard.h&#34;

@implementation ChalkBoard
@synthesize pointArray;

- (id)initWithFrame:(CGRect)frame
{
self = ;
    if (self) {
      self.backgroundColor = ;
    }
    return self;
}

-(NSMutableArray *)pointArray
{
    if(pointArray == nil) pointArray = [initWithCapacity:10];
    return pointArray;
}

-(void)pushPoint:(CGPoint)point{
    // this is called from drawtestViewController.m by button press
    // in here the array is full and growing by each button press
    ];
   NSLog(@&#34;-pushPoint executed, size is %u, array: %@&#34;,, pointArray);
}

- (void)drawRect:(CGRect)rect{
// here array is always empty
NSLog(@&#34;-DrawRect executed, size is %u, array: %@&#34;,, pointArray);
    if ( &gt;1){   
       ... some irrelevant code
    }
}

@end
</code></pre>

<hr/>

<pre><code>NSLog output

2012-04-09 11:34:39.145 drawtest -DrawRect executed, size is 0, array: (null)
2012-04-09 11:34:41.261 drawtest -Button was pressed
2012-04-09 11:34:41.262 drawtest -pushPoint executed, size is 1, array: (
    &#34;NSPoint: {100, 100}&#34;
)
2012-04-09 11:34:41.264 drawtest -pushPoint executed, size is 2, array: (
    &#34;NSPoint: {100, 100}&#34;,
    &#34;NSPoint: {10, 100}&#34;
)
2012-04-09 11:34:41.266 drawtest -DrawRect executed, size is 0, array: (null)
2012-04-09 11:34:42.825 drawtest -Button was pressed
2012-04-09 11:34:42.826 drawtest -pushPoint executed, size is 3, array: (
    &#34;NSPoint: {100, 100}&#34;,
    &#34;NSPoint: {10, 100}&#34;,
    &#34;NSPoint: {100, 100}&#34;
)
2012-04-09 11:34:42.827 drawtest -pushPoint executed, size is 4, array: (
    &#34;NSPoint: {100, 100}&#34;,
    &#34;NSPoint: {10, 100}&#34;,
    &#34;NSPoint: {100, 100}&#34;,
    &#34;NSPoint: {10, 100}&#34;
)
2012-04-09 11:34:42.829 drawtest -DrawRect executed, size is 0, array: (null)
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题实际上不在您的 <code>Chalkboard</code> 类中,而是在您的 <code>drawtestViewcontroller</code> 中。您的 <code>myDo</code> 操作将点添加到 <code>self.board</code>,但将 <code>setNeedsDisplay</code> 消息发送到 <code>self.graphicView</code>,即也是一个 <code>Chalkboard</code>,但不是您添加点的那个。</p>

<p>所以您看到的第二条日志消息实际上来自一个完全不同的对象。</p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - drawRect 查看空数组,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10070872/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10070872/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - drawRect 查看空数组