- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试了很多方法,还是没有结果。
因此,我在 UIViewController 的子类中以编程方式创建了以下按钮:
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0.0, 0.0, 110.0, 40.0);
rightButton.titleLabel.font = [UIFont fontWithName:GAME_FONT_NAME_STRING size:20.0];
[rightButton setTitle:@"MyTitle" forState:UIControlStateNormal];
rightButton.backgroundColor = [UIColor clearColor];
[rightButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[rightButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightButton];
选择器所在位置:
- (void)myButton;
我尝试了一切:
- (void)myButton;
- (void)myButton:(id)sender;
- (void)myButton:(id)sender forEvent:(UIEvent *)event;
- (IBAction)myButton;
- (IBAction)myButton:(id)sender;
- (IBAction)myButton:(id)sender forEvent:(UIEvent *)event;
当然还有相应的选择器:
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:forEvent:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:forEvent:) forControlEvents:UIControlEventTouchUpInside];
结果始终是未捕获的异常 - [NSObject doesNotRecognizeSelector:]。然而,程序通常的回溯是:
#0 0x92a6bedb in objc_msgSend ()
#1 0x03b0a430 in ?? ()
#2 0x00306b4e in -[UIControl sendAction:to:forEvent:] ()
#3 0x00308d6f in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#4 0x00307abb in -[UIControl touchesEnded:withEvent:] ()
#5 0x002bcddf in -[UIWindow _sendTouchesForEvent:] ()
#6 0x002a67c8 in -[UIApplication sendEvent:] ()
#7 0x002ad061 in _UIApplicationHandleEvent ()
#8 0x02498d59 in PurpleEventCallback ()
#9 0x01cabb80 in CFRunLoopRunSpecific ()
#10 0x01caac48 in CFRunLoopRunInMode ()
#11 0x02497615 in GSEventRunModal ()
#12 0x024976da in GSEventRun ()
#13 0x002adfaf in UIApplicationMain ()
那么这个按钮有什么问题呢?
PS:我使用的是iPhone SDK 3.1.3
<小时/>更新!
AppDelegate 中的以下代码(界面中没有声明):
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIButton *test = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)];
[test setTitle:@"Title" forState:UIControlStateNormal];
UIImage *bg = [UIImage imageNamed:...];
[test setBackgroundImage:bg forState:UIControlStateNormal];
[test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:test];
[test release];
[window makeKeyAndVisible];
}
- (void)testAction {
NSLog(@"Write something...");
}
完美运行!
但是如果我使用相同的代码创建一个空的 UIViewController:
- (void)viewDidLoad {
UIButton *test = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)];
[test setTitle:@"Title" forState:UIControlStateNormal];
UIImage *bg = [UIImage imageNamed:...];
[test setBackgroundImage:bg forState:UIControlStateNormal];
[test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:test];
[test release];
[window makeKeyAndVisible];
}
- (void)testAction {
NSLog(@"Write something...");
}
我收到这个神秘的错误。 :-( 救命!
最佳答案
一篇旧帖子,现在可能已经全部排序,但还有另一个问题会导致崩溃:
[test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside]
不正确。 action:@selector(testAction)
必须有一个冒号,因此:
action:@selector(testAction:)
否则你会遇到无法识别的选择器崩溃 - 这就是你所得到的..
关于iphone - UIButton添加目标:action:forControlEvents: results in [NSObject doesNotRecognizeSelector:],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3046335/
我已向按钮添加了一些目标。他们工作得很好。但在我的 imageMoved... 方法中,如何确定两个事件中哪一个触发了它?这是我添加目标的位置: [button addTarget:self
代码只不过是按钮的目标操作: [subButton addTarget:self action:@selector(pickerviewSubButtonClicked:)
我知道我写的代码是错误的。但我想要这样的东西。怎么做? UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; int tot
我正在像这样使用 addTarget:action:forControlEvents: [newsButton addTarget:self action:@selector(switchToNews
当按下按钮时,我想推送一个新 View ,并且需要将 NSDictionary 对象传递给新的 DetailViewController ,因此该方法如下所示: -(void)displayCours
@selector(updateStuff:) 这是自动发送参数:(id)sender。是否可以将其覆盖为其他内容?在我的特定用法中,实际的发件人是没有意义的。 我正在使用 UITableView 并
尝试了很多方法,还是没有结果。 因此,我在 UIViewController 的子类中以编程方式创建了以下按钮: rightButton = [UIButton buttonWithType:
我环顾四周,但在互联网上和苹果文档中都找不到这个,所以我猜它不存在。 但是 iOS4 是否有一个等效的 API 可以: [button addTarget:self action:@selector(
我已经将UIButton子类化以绘制图形,但是“addTarget”将无法工作。 - (void)addTarget:(id)target action:(SEL)action forControlE
我知道这个问题已被问过很多次,但我想在单击按钮时将自定义对象作为参数传递。 UIButton addTarget:action:forControlEvents: 不允许我们这样做,但这对我来说很重要
我想要一个父 ViewController 来处理由它的一个子 ViewController 生成的 Target-Actions。 根据 Apple 文档,这应该可以通过将 target 设置为 n
我正在尝试将我的 indexPath.row 移动到不同的 View Controller - pageView 标签。这是我尝试过的: [cell.nav addTarget:self action
我打算在我的页面上添加一个“空白”星形图标,当用户点击它时该图标将变为“实心”星形。 我尝试设置 UIImage 如下所示: UIImage *hotIcon = [UIImage imageName
我的按钮如下 创建标签 1 创建标签 2 创建自定义 View (UIView) 在自定义 View 上添加了 label1 和 label2 创建了 myCustomButton(UIButton)
我正在尝试在 UIPicker 的数据更改(实时更新)后立即更新表格单元格中显示的日期和时间。我实现了以下代码。尽管更改了选择器中的值,但我的“更新”方法没有被调用。谁能建议?谢谢! 郑 - (voi
这是我的问题: 我想在 UIButton 上分配一个事件。我用这个方法: - (void)addTarget:(id)target action:(SEL)action forControlEvent
* 编辑:抱歉,刚刚意识到 - 这段代码工作正常,直到我将表格分为三个部分。当它是带有节标题的单个节时,滚动时一切正常。现在我有三个部分,它在滚动时崩溃。* 我正在向 uitablecell 添加一个
我有一个包含几个 subview 的 View ,每个 subview 上都有一个透明按钮。我将这些 View 放在 for 循环中的主 UIView 上,在此循环中,我还为 subview 按钮属性
利用这个论坛中的精彩帖子,我在 tableView 中创建了一个开关作为 accessoryView。当触摸开关时,我的 Action (switchChanged) 被调用。只有发送方有有效值,事件
如果我们使用 Interface Builder 在 .xib 文件中创建一个 UIButton,什么是更好的方法,是使用 IBAction 方法来定义它的 Action 方法和所需的控制事件更好,还
我是一名优秀的程序员,十分优秀!