2

我有一个从网络服务器提取的随机问卷表格。我需要执行一个按钮(setNeedsDisplay)来刷新页面......当我点击按钮时应用程序崩溃了:

*Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ExercisesViewController setNeedsDisplay]: unrecognized selector sent to instance 0x6a1e840' *** First throw call stack: (0x13f3022 0x1584cd6 0x13f4cbd 0x1359ed0 0x1359cb2 0x963054 0x974fc6 0x57c0 0x13f4e99 0x4014e 0x400e6 0xe6ade 0xe6fa7 0xe6266 0x301a1a 0x13c799e 0x135e640 0x132a4c6 0x1329d84 0x1329c9b 0x12dc7d8 0x12dc88a 0x3d626 0x1f96 0x1f05) 终止称为抛出异常*

//Add refreshing agenda
    scrollViewFrame = CGRectMake(0, 200, 80, 40);
    mark = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    mark.frame = scrollViewFrame;
    [mark setTitle:@"Get Score" forState:UIControlStateNormal];
    [mark setBackgroundColor:[UIColor clearColor]];

    [mark addTarget:self 
             action:@selector(markButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:mark];

...

- (IBAction)markButtonSelected:(id)sender{
[self performSelectorInBackground:@selector(setNeedsDisplay) withObject:nil];

}

这是语法问题吗?我在这里读到它与线程有关。有人可以解释一下线程还是有更直接的方法?提前谢谢...

4

4 回答 4

4

setNeedsDisplay方法是UIView的方法,而不是UIViewController。在您的markButtonSelected:方法中,只需添加:[self.view setNeedsDisplay];

于 2012-04-26T13:02:48.307 回答
0

利用:

[self.view setNeedsDisplay];
于 2012-04-26T13:01:39.553 回答
0

视图更新应该在主线程上完成。

利用

[self.view performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];
于 2012-04-26T13:17:09.917 回答
0

您需要在 UIView 而不是 UIViewController 上调用 setNeedsDisplay。异步执行此操作会产生更好的性能。使用 GCD 做异步工作是 Apple 的首选方式。

IE:

    dispatch_async(dispatch_get_main_queue(), ^{
      [self.view setNeedsDisplay];
    });
于 2012-04-26T13:40:29.040 回答