刚开始使用iPhone开发和Objective-C.
昨天我试图在我的视图中添加Observer以获取通知,并且我一直收到此错误:
unrecognized selector sent to instance
我追踪到这个事实,我需要将尾随冒号包含在我的selector参数中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameOfMySelector:) name:@"BBLocationServicesAreDisabled" object:nil];
今天,我觉得我很聪明,因为在设置按钮的动作参数时,我记得我昨天的错误,并将冒号添加到动作参数中.action参数采用a @selector,就像设置一个观察者的选择器参数一样NSNotification,所以我认为我做的是正确的.
但是,使用以下代码:
[self.callToActionButton addTarget:self action:@selector(nameOfMySelector:) forControlEvents:UIControlEventTouchUpInside];
我得到完全相同的错误:
unrecognized selector sent to instance
是什么赋予了?为什么@selector需要一个尾随冒号,另一个不需要?我应该遵循哪些规则应该包含哪些内容以及什么时候应该保留,以及为什么我不能总是只做一个或另一个?
谢谢!
我是iPhone技术的新手.请有人告诉我如何为UIButton添加动作.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 25);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:@"Play" forState:UIControlStateNormal];
[btn addTarget:self action:(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
btn.center = self.center;
[self addSubview:btn];
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个按钮,一旦点击将显示另一个UIView的弹出窗口.为了测试这一点,我在viewDidLoad部分中有以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.hard1 = [UIButton buttonWithType:UIButtonTypeCustom];
[self.hard1 setFrame:CGRectMake(884, 524, 105, 60)]; // set the x,y,width and height based on your specs
UIImage *buttonImage = [UIImage imageNamed:@"green.jpg"];
hard1.layer.cornerRadius = 10;
hard1.clipsToBounds = YES;
[hard1 addTarget: self
action: @selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];
[self.hard1 setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:self.hard1];
}
Run Code Online (Sandbox Code Playgroud)
进一步向下:
- (IBAction) buttonClicked: (id)sender
{
NSLog(@"Tap");
}
Run Code Online (Sandbox Code Playgroud)
但是,当我按下按钮时,控制台不会记录"Tap".有任何想法吗?