0

当按下我创建的按钮时,此代码会导致“无效选择器”错误。函数从哪里test获取?

主程序

mainScreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
[self.view addSubview:mainScreen];

TaskButtons *tB = [[TaskButtons alloc] init];
[mainScreen addSubview:[tB TaskStart]]; 

任务按钮.m

- (UIButton*)TaskStart {
   CGRect buttonFrame = CGRectMake(500, 206, 400, 35);
   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   button.frame = buttonFrame;
   [button setTitle:@"Task Button" forState:UIControlStateNormal];
   button.backgroundColor = [UIColor clearColor];
   button.titleLabel.textAlignment = UITextAlignmentLeft;
   button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
   [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
   [button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
   return button;
 }

- (void)test{
   NSLog(@"test line");
}

似乎test没有调用该函数。将按钮设置targetself此处是否意味着它应该在TaskButtons类中查找调用的函数test

4

2 回答 2

0

问题是 ARC 过早地释放实例化对象。所以要解决这个问题,我需要保留更长时间。

主文件

#import "TaskButtons.m"
@interface ViewController : UIViewController {
     TaskButtons *tB;
}

@property (nonatomic, retain) TaskButtons *tB;
于 2011-11-29T04:15:15.693 回答
0
[button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];

- (void)test:(id)sender{
NSLog(@"test line");
}

语法问题:) 在您的代码中替换这些行。

于 2011-11-29T05:17:54.033 回答